I don't know if this is the best way, but the below algorithm provides one way - based on IDL's LABEL_REGION function - that does not require many code lines:
; Make some simple dummy 400 x 400 image data
seed = 1
temp = byte(randomu(seed,10,10) * 10)
image = rebin(temp, 400, 400)
window, XSIZE=400, YSIZE=400, /FREE, TITLE='Original Image'
tvscl, image
; Let's make a threshold that considers ~10% of image to be dead pixels
threshold = 1b
deadPixelIndices = where(image le threshold)
; Make a binary image from these indices
; LABEL_REGION will need dead pixels to be the only values gt 0
deadPixelMask = bytarr(400, 400)
deadPixelMask[deadPixelIndices] = 255b
window, XSIZE=400, YSIZE=400, /FREE, TITLE='Dead Pixels In White'
tvscl, deadPixelMask
; This example produces 12 larger regions. Let's add one small 1-pixel
; region at the edge of the mask
deadPixelMask[150,0] = 255b
tvscl, deadPixelMask
; LABEL_REGION does not find regions or parts of regions that are
; on any outside edge of the image. That is why we make this extra
; image object
maskWithExtraBorder = bytarr(402,402)
maskWithExtraBorder[1,1] = deadPixelMask ; Center mask in new image
result = label_region(maskWithExtraBorder, /ALL_NEIGHBORS)
nDeadPixelRegions = max(result)
; 13
Best of luck,
James Jones
|