使用 Roipoly 进行图像分析。获取值错误

Image analysis with Roipoly. Getting ValueError

我一直在尝试使用RoiPoly绘制ROI并找到图像中ROI内的平均像素灰度值。但是,我不断收到以下错误:

Traceback (most recent call last):
  File "example.py", line 22, in <module>
    ROI1.displayMean(img)
  File "/home/ruven/Downloads/Rupesh images/roipoly.py", line 74, in displayMean
    mask = self.getMask(currentImage)
  File "/home/ruven/Downloads/Rupesh images/roipoly.py", line 48, in getMask
    ny, nx = np.shape(currentImage)
ValueError: too many values to unpack

这是我的代码:

import pylab as pl
from roipoly import roipoly 

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

# create image
img=mpimg.imread('5.jpg')


# show the image
pl.imshow(img, interpolation='nearest', cmap="Greys")
pl.colorbar()
pl.title("left click: line segment         right click: close region")

# let user draw first ROI
ROI1 = roipoly(roicolor='r') #let user draw first ROI

# show the image with the first ROI
pl.imshow(img, interpolation='nearest', cmap="Greys")
pl.colorbar()
ROI1.displayROI()
ROI1.displayMean(img)
'''
# show the image with both ROIs and their mean values
pl.imshow(img, interpolation='nearest', cmap="Greys")
pl.colorbar()
ROI1.displayMean(img)
pl.title('The ROI')
pl.show()
'''
# show ROI masks
pl.imshow(ROI1.getMask(img),
          interpolation='nearest', cmap="Greys")
pl.title('ROI masks of ROI')
pl.show()

这是图片:enter image description here

这是投资回报率:

我也对使用 ROI 来查找平均像素灰度值的其他方法持开放态度

您看到的错误是由于您的输入是 3 通道图像 (RGB) 而不是 1 通道。因此,当 roipoly 尝试解包形状时,它试图将 3 个值放入 2 个变量中。

应该通过将 3 通道图像转换为 1 通道来解决此问题。这是一种方法:

# create image
img = mpimg.imread('5.jpg')

print(img.shape)   # (992, 1024, 3)
img = img[:,:,0]
print(img.shape)   # (992, 1024)

或者您可以阅读此 q/answer 了解其他方法:

  • How can I convert an RGB image into grayscale in Python?