我运行代码时遇到此错误:
断言失败(mask.size() == imsize && mask.channels() == 1) in histPrepareImages, file /Users/jmeunier28/Downloads/opencv- 3.0.0/modules/imgproc/src/histogram.cpp, line 159
File "/usr/local/lib/python2.7/site-packages/pyimagesearch/rgbhistogram.py", line 16, in describe
mask, self.bins, [0, 256, 0, 256, 0, 256])
在pyimagesearch/rgbhistogram.py
我试图像这样使用calcHist:
def describe(self, image, mask = None):
hist = cv2.calcHist([image], [0, 1, 2],
mask, self.bins, [0, 256, 0, 256, 0, 256])
hist = cv2.normalize(hist,hist)
# return out 3D histogram as a flattened array
return hist.flatten()
然后在我的主脚本中运行:
desc = RGBHistogram([8, 8, 8])
for (imagePath, maskPath) in zip(imagePaths, masksPaths):
# load the images in the path
image = cv2.imread(imagePath)
mask = cv2.imread(maskPath,0)
features = desc.describe(image,mask)
data.append(features)
target.append(imagePath.split("_")[-2])
图像有形状(142,213,3),面具的形状是(142,213)这是问题的根源吗?我是python的新手,我卸载了numpy和opencv并重新安装它们,这并没有纠正它。我想不出为什么这不能正常工作的任何其他原因。如果有人能指出我正确的方向,我会非常感激!
修改 这些图像是不同动物的照片,我写了一个脚本,在这里创建动物的面具图像:
for i in range(imgnum):
im = cv2.imread(imagePaths[i],0)
ret,thresh = cv2.threshold(im,127,255,cv2.THRESH_BINARY)
target = imagePaths[i].split("_")[-2]
newImg= cv2.imwrite(os.path.join(dirname,target+"_"+str(i)+".jpg"),thresh)