0

我试图让斐济在一组图像上运行对比函数以进行分析。但是每当我尝试运行该文件时,当我尝试索引直方图对象时都会出错。对比代码应该模仿可通过斐济使用的自动阈值功能。我已经尝试过使用 IJ.run(imp, "Enhance Contrast", "aturated=0.02),但它并没有得到自动阈值那么好的结果。

dc = DirectoryChooser("Choose a folder")
folder = dc.getDirectory()

for filename in os.listdir(folder):  
  if filename.endswith(".tif"):  
    print "Processing", filename  
    imp = IJ.openImage(os.path.join(folder, filename))  
    if imp is None:  
      print "Could not open image from file:", filename  
      continue  

    IJ.run(imp,"8-bit","")


    at = 0
    AT = 5000
    cal = imp.getCalibration()
    imp.setCalibration(None)
    stats = imp.getStatistics()
    imp.setCalibration(cal)
    limit = int(stats.pixelCount/10)
    histogram = stats.histogram
    if at < 10:
      at = AT
    else:
      at /= 2
    threshold = int(stats.pixelCount/at)
    i = -1
    found = False
    count = 0
    while True:
      i += 1
      count = histogram[i]
      if count > limit:
        count = 0
      found = count > threshold
      if found or i >= 255:
        break
    hmin = i
    i = 256
    while True:
      i -= 1
      count = histogram[i]
      if count > limit:
        count = 0
      found = count > threshold
      if found or i < 1:
        break
    hmax = i

  else:  
    print "Ignoring", filename  
Traceback (most recent call last):
  File "C:\Users\jacob\Documents\School\PytonTest.py", line 41, in <module>
    count = histogram[i]
TypeError: 'instancemethod' object is unsubscriptable

    at org.python.core.Py.TypeError(Py.java:265)
    at org.python.core.PyObject.__finditem__(PyObject.java:687)
    at org.python.core.PyObject.__getitem__(PyObject.java:755)
    at org.python.pycode._pyx0.f$0(C:/Users/jacob/Documents/School/PytonTest.py:11)
    at org.python.pycode._pyx0.call_function(C:/Users/jacob/Documents/School/PytonTest.py)
    at org.python.core.PyTableCode.call(PyTableCode.java:171)
    at org.python.core.PyCode.call(PyCode.java:18)
    at org.python.core.Py.runCode(Py.java:1614)
    at org.python.core.__builtin__.eval(__builtin__.java:497)
    at org.python.core.__builtin__.eval(__builtin__.java:501)
    at org.python.util.PythonInterpreter.eval(PythonInterpreter.java:259)
    at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:57)
    at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:31)
    at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
    at org.scijava.script.ScriptModule.run(ScriptModule.java:160)
    at org.scijava.module.ModuleRunner.run(ModuleRunner.java:168)
    at org.scijava.module.ModuleRunner.call(ModuleRunner.java:127)
    at org.scijava.module.ModuleRunner.call(ModuleRunner.java:66)
    at org.scijava.thread.DefaultThreadService$3.call(DefaultThreadService.java:238)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
4

1 回答 1

0

将获取直方图的方式更改为以下 - 这应该可以解决您的问题:

test = imp.getProcessor()
histogram = test.getHistogram()
于 2019-08-20T20:57:33.443 回答