@@ -149,6 +149,55 @@ def iterator(image, mask=None, index=None):
149149 return ImageIterator (image )
150150
151151
152+ def iterator_ij (image , mask = None , index = None ):
153+ '''
154+ Returns an iterator over image pixel coordinates for a given mask.
155+
156+ Arguments:
157+
158+ `image` (ndarray or :class:`spectral.Image`):
159+
160+ An image over whose pixels will be iterated.
161+
162+ `mask` (ndarray) [default None]:
163+
164+ An array of integers that specify over which pixels in `image`
165+ iteration should be performed.
166+
167+ `index` (int) [default None]:
168+
169+ Specifies which value in `mask` should be used for iteration.
170+
171+ Returns:
172+
173+ An iterator over image pixel coordinates. Each returned item is a
174+ 2-tuple of the form (row, col).
175+
176+ If neither `mask` nor `index` are defined, iteration is performed over all
177+ pixels. If `mask` (but not `index`) is defined, iteration is performed
178+ over all pixels for which `mask` is nonzero. If both `mask` and `index`
179+ are defined, iteration is performed over all pixels `image[i,j]` for which
180+ `mask[i,j] == index`.
181+ '''
182+
183+ if mask is None and index is None :
184+ # Iterate over all pixels
185+ (nrows , ncols ) = image .shape [:2 ]
186+ for r in range (nrows ):
187+ for c in range (ncols ):
188+ yield (r , c )
189+ else :
190+ if mask .shape != image .shape [:len (mask .shape )]:
191+ raise ValueError ('Mask shape does not match image.' )
192+
193+ if index is None :
194+ mask = mask != 0
195+ else :
196+ mask = mask == index
197+ for rc in np .argwhere (mask ):
198+ yield tuple (rc )
199+
200+
152201def mean_cov (image , mask = None , index = None ):
153202 '''
154203 Return the mean and covariance of the set of vectors.
0 commit comments