def thread_geod(mask_p, index, pos, results): shortWeights = binary.ChamferWeights.CHESSKNIGHT.getShortWeights() gt = binary.geodesic.GeodesicDistanceTransformShort5x5(shortWeights, False) marker = ByteProcessor(dimentions[0], dimentions[1]) marker.putPixel(pos[0][index], pos[1][index], 255) proc = gt.geodesicDistanceMap(marker, mask_p) points = [] for w in range(len(X)): points.append(proc.getPixel(pos[0][w], pos[1][w])) results[index] = points
def getGrayScaleImage(currIP, c, chanName, cfg): if (cfg.hasValue(ELMConfig.upperLeftExclusionX)): ulExclusionX = cfg.getValue(ELMConfig.upperLeftExclusionX) else: ulExclusionX = 0 if (cfg.hasValue(ELMConfig.upperLeftExclusionY)): ulExclusionY = cfg.getValue(ELMConfig.upperLeftExclusionY) else: ulExclusionY = 0 if (cfg.hasValue(ELMConfig.lowerRightExclusionX)): lrExclusionX = cfg.getValue(ELMConfig.lowerRightExclusionX) else: lrExclusionX = currIP.getWidth() if (cfg.hasValue(ELMConfig.lowerRightExclusionY)): lrExclusionY = cfg.getValue(ELMConfig.lowerRightExclusionY) else: lrExclusionY = currIP.getHeight() imgType = currIP.getType() if (chanName in cfg.getValue( ELMConfig.chansToSkip)): # Don't process skip channels currIP.close() return None elif imgType == ImagePlus.COLOR_RGB or imgType == ImagePlus.COLOR_256: if (chanName == ELMConfig.BRIGHTFIELD): toGray = ImageConverter(currIP) toGray.convertToGray8() elif (chanName == ELMConfig.BLUE) \ or (cfg.getValue(ELMConfig.chanLabel)[c] == ELMConfig.RED) \ or (cfg.getValue(ELMConfig.chanLabel)[c] == ELMConfig.GREEN): # chanIdx = 2 if (cfg.getValue(ELMConfig.chanLabel)[c] == ELMConfig.RED): chanIdx = 0 elif (cfg.getValue(ELMConfig.chanLabel)[c] == ELMConfig.GREEN): chanIdx = 1 imgChanns = ChannelSplitter.split(currIP) currIP.close() currIP = imgChanns[chanIdx] # Clear the Exclusion zone, so it doesn't mess with thresholding imgProc = currIP.getProcessor() imgProc.setColor(Color(0, 0, 0)) imgProc.fillRect(lrExclusionX, lrExclusionY, currIP.getWidth(), currIP.getHeight()) imgProc.fillRect(0, 0, ulExclusionX, ulExclusionY) elif (chanName == ELMConfig.YELLOW): # Clear the Exclusion zone, so it doesn't mess with thresholding imgProc = currIP.getProcessor() imgProc.setColor(Color(0, 0, 0)) imgProc.fillRect(lrExclusionX, lrExclusionY, currIP.getWidth(), currIP.getHeight()) imgProc.fillRect(0, 0, ulExclusionX, ulExclusionY) # Create a new image that consists of the average of the red & green channels title = currIP.getTitle() width = currIP.getWidth() height = currIP.getHeight() newPix = ByteProcessor(width, height) for x in range(0, width): for y in range(0, height): currPix = currIP.getPixel(x, y) newPix.putPixel(x, y, (currPix[0] + currPix[1]) / 2) currIP.close() currIP = ImagePlus(title, newPix) else: print "ERROR: Unrecognized channel name! Name: " + chanName currIP.close() return None elif imgType == ImagePlus.GRAY16 or imgType == ImagePlus.GRAY32 or imgType == ImagePlus.GRAY8: if not imgType == ImagePlus.GRAY8: toGray = ImageConverter(currIP) toGray.convertToGray8() else: print "ERROR: Unrecognized channel name & image type! Channel: " + chanName + ", imgType: " + str( imgType) currIP.close() return None return currIP
def getThresholdedMask(currIP, c, z, t, chanName, cfg, wellPath, dbgOutDesc): if (cfg.hasValue(ELMConfig.upperLeftExclusionX)): ulExclusionX = cfg.getValue(ELMConfig.upperLeftExclusionX) else: ulExclusionX = 0 if (cfg.hasValue(ELMConfig.upperLeftExclusionY)): ulExclusionY = cfg.getValue(ELMConfig.upperLeftExclusionY) else: ulExclusionY = 0 if (cfg.hasValue(ELMConfig.lowerRightExclusionX)): lrExclusionX = cfg.getValue(ELMConfig.lowerRightExclusionX) else: lrExclusionX = currIP.getWidth() if (cfg.hasValue(ELMConfig.lowerRightExclusionY)): lrExclusionY = cfg.getValue(ELMConfig.lowerRightExclusionY) else: lrExclusionY = currIP.getHeight() imgType = currIP.getType() if (chanName in cfg.getValue( ELMConfig.chansToSkip)): # Don't process skip channels currIP.close() return None elif imgType == ImagePlus.COLOR_RGB or imgType == ImagePlus.COLOR_256: if (chanName == ELMConfig.BRIGHTFIELD): toGray = ImageConverter(currIP) toGray.convertToGray8() if cfg.params[ELMConfig.imgType] == "png": darkBackground = True else: darkBackground = False elif (chanName == ELMConfig.BLUE) \ or (cfg.getValue(ELMConfig.chanLabel)[c] == ELMConfig.RED) \ or (cfg.getValue(ELMConfig.chanLabel)[c] == ELMConfig.GREEN): # chanIdx = 2 if (cfg.getValue(ELMConfig.chanLabel)[c] == ELMConfig.RED): chanIdx = 0 elif (cfg.getValue(ELMConfig.chanLabel)[c] == ELMConfig.GREEN): chanIdx = 1 imgChanns = ChannelSplitter.split(currIP) currIP.close() currIP = imgChanns[chanIdx] # Clear the Exclusion zone, so it doesn't mess with thresholding imgProc = currIP.getProcessor() imgProc.setColor(Color(0, 0, 0)) imgProc.fillRect(lrExclusionX, lrExclusionY, currIP.getWidth(), currIP.getHeight()) imgProc.fillRect(0, 0, ulExclusionX, ulExclusionY) darkBackground = True elif (chanName == ELMConfig.YELLOW): # Clear the Exclusion zone, so it doesn't mess with thresholding imgProc = currIP.getProcessor() imgProc.setColor(Color(0, 0, 0)) imgProc.fillRect(lrExclusionX, lrExclusionY, currIP.getWidth(), currIP.getHeight()) imgProc.fillRect(0, 0, ulExclusionX, ulExclusionY) # Create a new image that consists of the average of the red & green channels title = currIP.getTitle() width = currIP.getWidth() height = currIP.getHeight() newPix = ByteProcessor(width, height) for x in range(0, width): for y in range(0, height): currPix = currIP.getPixel(x, y) newPix.putPixel(x, y, (currPix[0] + currPix[1]) / 2) currIP.close() currIP = ImagePlus(title, newPix) darkBackground = True else: print "ERROR: Unrecognized channel name! Name: " + chanName currIP.close() return None elif imgType == ImagePlus.GRAY16 or imgType == ImagePlus.GRAY32 or imgType == ImagePlus.GRAY8: if (chanName == ELMConfig.BRIGHTFIELD): if cfg.params[ELMConfig.imgType] == "png": darkBackground = True else: darkBackground = False else: darkBackground = True if not imgType == ImagePlus.GRAY8: toGray = ImageConverter(currIP) toGray.convertToGray8() else: print "ERROR: Unrecognized channel name & image type! Channel: " + chanName + ", imgType: " + str( imgType) currIP.close() return None WindowManager.setTempCurrentImage(currIP) if cfg.getValue(ELMConfig.debugOutput): IJ.saveAs('png', os.path.join(wellPath, "Processing_" + dbgOutDesc + ".png")) upperThreshImg = currIP.duplicate() # If threshold value is set, use it if (cfg.hasValue(ELMConfig.imageThreshold)): thresh = cfg.getValue(ELMConfig.imageThreshold) if (darkBackground): currIP.getProcessor().setThreshold(thresh, 255, ImageProcessor.NO_LUT_UPDATE) else: currIP.getProcessor().setThreshold(0, thresh, ImageProcessor.NO_LUT_UPDATE) else: # Otherise, automatically compute threshold threshMethod = "Default" if cfg.hasValue(ELMConfig.thresholdMethod): threshMethod = cfg.getValue(ELMConfig.thresholdMethod) currIP.getProcessor().setAutoThreshold(threshMethod, darkBackground, ImageProcessor.NO_LUT_UPDATE) threshRange = currIP.getProcessor().getMaxThreshold( ) - currIP.getProcessor().getMinThreshold() #print "\t\tZ = " + str(z) + ", T = " + str(t) + ", chan " + chanName + ": Using default threshold of minThresh: " + str(currIP.getProcessor().getMinThreshold()) + ", maxThresh: " + str(currIP.getProcessor().getMaxThreshold()) if currIP.getType() != ImagePlus.GRAY8: print "\tChannel " + chanName + " is not GRAY8, instead type is %d" % currIP.getType( ) if threshRange > cfg.getValue(ELMConfig.maxThreshRange): if (cfg.hasValue(ELMConfig.defaultThreshold)): thresh = cfg.getValue(ELMConfig.defaultThreshold) print "\t\tZ = " + str(z) + ", T = " + str( t ) + ", chan " + chanName + ": Using default threshold of " + str( thresh) + ", minThresh: " + str(currIP.getProcessor( ).getMinThreshold()) + ", maxThresh: " + str( currIP.getProcessor().getMaxThreshold()) if (darkBackground): currIP.getProcessor().setThreshold( thresh, 255, ImageProcessor.NO_LUT_UPDATE) else: currIP.getProcessor().setThreshold( 0, thresh, ImageProcessor.NO_LUT_UPDATE) else: print "\t\tZ = " + str(z) + ", T = " + str( t ) + ", chan " + chanName + ": Ignored Objects due to threshold range! minThresh: " + str( currIP.getProcessor().getMinThreshold( )) + ", maxThresh: " + str( currIP.getProcessor().getMaxThreshold()) currIP.close() return None IJ.run(currIP, "Convert to Mask", "") # Clear out exclusion zones imgProc = currIP.getProcessor() imgProc.fillRect(lrExclusionX, lrExclusionY, currIP.getWidth(), currIP.getHeight()) imgProc.fillRect(0, 0, ulExclusionX, ulExclusionY) IJ.run(currIP, "Close-", "") # Brightfield has an additional thresholding step if cfg.getValue(ELMConfig.chanLabel)[c] == ELMConfig.BRIGHTFIELD: if cfg.getValue(ELMConfig.debugOutput): IJ.saveAs( 'png', os.path.join(wellPath, "OrigMask_" + dbgOutDesc + ".png")) upperThresh = 255 * 0.95 upperThreshImg.getProcessor().setThreshold( upperThresh, 255, ImageProcessor.NO_LUT_UPDATE) IJ.run(upperThreshImg, "Convert to Mask", "") IJ.run(upperThreshImg, "Close-", "") if cfg.getValue(ELMConfig.debugOutput): WindowManager.setTempCurrentImage(upperThreshImg) IJ.saveAs( 'png', os.path.join(wellPath, "UpperThreshMask_" + dbgOutDesc + ".png")) ic = ImageCalculator() compositeMask = ic.run("OR create", currIP, upperThreshImg) IJ.run(compositeMask, "Close-", "") currIP.close() currIP = compositeMask WindowManager.setTempCurrentImage(currIP) if cfg.getValue(ELMConfig.debugOutput): WindowManager.setTempCurrentImage(currIP) IJ.saveAs('png', os.path.join(wellPath, "Binary_" + dbgOutDesc + ".png")) upperThreshImg.close() return currIP
def hyst(ima, T1, T2): la = ima.getWidth() ha = ima.getHeight() res = ByteProcessor(la, ha) for x in xrange(la): for y in xrange(ha): pix = ima.getPixelValue(x, y) if pix >= T1: res.putPixel(x, y, 255) elif pix >= T2: res.putPixel(x, y, 128) change = True while (change): change = False for x in xrange(1, la - 1): for y in xrange(1, ha - 1): if (res.getPixelValue(x, y) == 255): if (res.getPixelValue(x + 1, y) == 128): change = True res.putPixelValue(x + 1, y, 255) if (res.getPixelValue(x - 1, y) == 128): change = True res.putPixelValue(x - 1, y, 255) if (res.getPixelValue(x, y + 1) == 128): change = True res.putPixelValue(x, y + 1, 255) if (res.getPixelValue(x, y - 1) == 128): change = True res.putPixelValue(x, y - 1, 255) if (res.getPixelValue(x + 1, y + 1) == 128): change = True res.putPixelValue(x + 1, y + 1, 255) if (res.getPixelValue(x - 1, y - 1) == 128): change = True res.putPixelValue(x - 1, y - 1, 255) if (res.getPixelValue(x - 1, y + 1) == 128): change = True res.putPixelValue(x - 1, y + 1, 255) if (res.getPixelValue(x + 1, y - 1) == 128): change = True res.putPixelValue(x + 1, y - 1, 255) if (change): for x in xrange(la - 2, 0, -1): for y in xrange(ha - 2, 0, -1): if (res.getPixelValue(x, y) == 255): if (res.getPixelValue(x + 1, y) == 128): change = True res.putPixelValue(x + 1, y, 255) if (res.getPixelValue(x - 1, y) == 128): change = True res.putPixelValue(x - 1, y, 255) if (res.getPixelValue(x, y + 1) == 128): change = True res.putPixelValue(x, y + 1, 255) if (res.getPixelValue(x, y - 1) == 128): change = True res.putPixelValue(x, y - 1, 255) if (res.getPixelValue(x + 1, y + 1) == 128): change = True res.putPixelValue(x + 1, y + 1, 255) if (res.getPixelValue(x - 1, y - 1) == 128): change = True res.putPixelValue(x - 1, y - 1, 255) if (res.getPixelValue(x - 1, y + 1) == 128): change = True res.putPixelValue(x - 1, y + 1, 255) if (res.getPixelValue(x + 1, y - 1) == 128): change = True res.putPixelValue(x + 1, y - 1, 255) for x in xrange(la): for y in xrange(ha): if (res.getPixelValue(x, y) == 128): res.putPixelValue(x, y, 0) return res