Subroutine which finds neighboring pixels

Useful hints
Post Reply
User avatar
Posts: 1120
Joined: Mon Jan 19, 2009 7:16 pm

Subroutine which finds neighboring pixels

Post by joleenf »


I have a subroutine which others may find useful. It accepts of mask of a field (values of 1/NaN or 1/0) and finds neighboring pixels which have passed the mask criteria. This clusters pixels within a certain pixel radius into separate groups. The subroutine needs an input radius, which defines the maximum pixel distance between pixels for grouping and a threshold, which defines the minimum size a cluster must reach before being assigned a group number. I have run this efficiently on a computer with 8 GB of ram. This code returns: the flatField object of groups (which could be displayed in McV), the number of groups, the indices of the groups (from the 1D array), the number of pixels which fell below the threshold, the indices of the noise.

The procedure:

(4.3 KiB) Downloaded 533 times

1.) Select the original field (e.g 3.9 micron temperature data (tempField))
2.) apply a mask (see JPythonMethods for documentation of mask, maskWithinRange, etc ... thods.html): maskField = mask(tempField, '<', 320, 1)
3.) call the subroutine: newGrid, nGroups, groupIndices, nNoise, theNoise = searchNeighbors(maskField, threshold=10)

The indices of the groups and the indices of the noise are saved as dictionary keys. This is a confusing use of a python dictionary, and it means that to find the indicies in the 1D array which belong to group N, the keys must be retrieved:

Code: Select all

indiciesToGroupX = groupIndices[[i]N[/i].keys()]  where n=-2,0,1-N 

Subroutine documentation included in the jar file:

clusterFlatField, numGroups, groupPoints = searchNeighbors(maskPixels, threshold=10)

searchNeigbors: accept pixel, find neighbors within a radius=1,
find out those neighbors are in the list of badPixelss
group the pixels.

threshold: Threshold of clustered pixels to filter. If
below threshold, throw group in a noise category.

maskPixels: The array which contains the mask on input.

Returns: assignGrid: The array which contains the grouping numbers.
NaN: lat/lon nan, -1: never considered for grouping,
-2: isolated,
-5: below cluster threshold,
1-n: cluster groups

numGroups: number of clusters found
groupPoints: the indices contained in each cluster group. Returned
as a list
nNoise: number of clusters below threshold
noise: indices of the noise

Other Notes:
There is a group 0, which should have no value. This group is "skipped" so that no
cluster group gets formally classified as 0. Since masks and ADDE areas can contain
zeros for values, it was not replicated in the assignment clusters to reduce possible
confusion. However, it does create confusion since jython arrays normally range from 0:N.
Post Reply