Python 如何仅获取ROI中的像素?
我正在尝试编写脚本,到目前为止,我有一个函数可以返回通过阈值化得到的ROIPython 如何仅获取ROI中的像素?,python,jython,image-segmentation,imagej,roi,Python,Jython,Image Segmentation,Imagej,Roi,我正在尝试编写脚本,到目前为止,我有一个函数可以返回通过阈值化得到的ROI PA.setRoiManager(roim) paOpt = PA.CLEAR_WORKSHEET +PA.SHOW_NONE +PA.INCLUDE_HOLES +PA.EXCLUDE_EDGE_PARTICLES + PA.ADD_TO_MANAGER # measurement options: clear the results, show nothing, include holes, exclude par
PA.setRoiManager(roim)
paOpt = PA.CLEAR_WORKSHEET +PA.SHOW_NONE +PA.INCLUDE_HOLES +PA.EXCLUDE_EDGE_PARTICLES + PA.ADD_TO_MANAGER
# measurement options: clear the results, show nothing, include holes, exclude particles on edges and add ROIs to manager
measOpt = PA.AREA + PA.MEAN + PA.MIN_MAX # measure the minimum, maximum, mean and area of particles
pa = PA(paOpt, measOpt, rt, minSize, maxSize)
pa.analyze(imp)
roi_list = roim.getRoisAsArray()
print roi_list[0]
这将向控制台提供一个输出:Roi[tracked,x=,y=,width=,height=]
因此,我有信心找到投资回报率。roim是RoiManager的一个实例
现在的问题是当我去看ROI中的像素时
def pixelStats(roiPatch, imp):
'''
get the pixel value distribution for the patch ROI
return the number of Lo pixels and the number of Ld pixels
roi_Patch returns handle to the patch ROI and imp is the
instance of current ImagePlus to be measured
'''
mask = roiPatch.getMask() # mask of roiPatch, do you need this?
ip = imp.getProcessor()
ip.setRoi(roiPatch)
cal = imp.getCalibration()
# options = IS.MEAN | IS.STD_DEV | IS.MODE
stats = IS.getStatistics(ip, M.MEAN | M.MODE | M.STD_DEV, cal) # what does getCalibration do??? stats for ROI only applied to imp, why?
# see the following --> http://fiji.sc/Jython_Scripting#Obtain.2FView_histogram_and_measurements_from_an_image
domThreshold = stats.mode - stats.STD_DEV
backGround = stats.MEAN - stats.STD_DEV
# define the threshold for Lo/Ld phase
pixels = ip.getPixels()
above = filter(lambda i: pixels[i] > backGround, xrange(len(pixels)))
below = filter(lambda i: above[i] < domThreshold, xrange(len(above)))
# calculate the length of the arrays containing pixels of Lo/Ld phase
return len(above), len(below), len(pixels)
查看返回的测量值,我确信ip.getPixels只是从原始图像处理器返回像素,而不仅仅是ROI。换句话说,我只是得到原始矩形图像中的像素数
有人能提供这是为什么的原因吗?或者建议一种更好的方法来做同样的事情。我熟悉Java API,但不熟悉python,所以我的答案没有经过测试 方法
ip.getPixels()
正在执行我期望的操作并返回所有像素。有没有投资回报率并不影响这一点。您的测量考虑了ROI,因此平均值和模式是根据您设置的ROI计算的
为了解决这个问题,我将裁剪ip。应该有一个
ip.crop()
方法,该方法将返回一个考虑了ROI的ip,然后您可以调用该ip
ip.getPixels()
仅获取ROI中的像素 我熟悉javaapi,但不熟悉python,所以我的答案没有经过测试 方法
ip.getPixels()
正在执行我期望的操作并返回所有像素。有没有投资回报率并不影响这一点。您的测量考虑了ROI,因此平均值和模式是根据您设置的ROI计算的
为了解决这个问题,我将裁剪ip。应该有一个
ip.crop()
方法,该方法将返回一个考虑了ROI的ip,然后您可以调用该ip
ip.getPixels()
仅获取ROI中的像素