hi everyone, I am trying to use the calcHist function to calculate the histogram of images of different animals. I have created masks of these images by implementing the following code:
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)
Then I use the RGBHistogram class calculate my histogram of the original images like so:
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)#,0,255,cv2.NORM_MINMAX)
# return out 3D histogram as a flattened array
return hist.flatten()
finally, I create an instance of this class and use it as a feature describer that is appended to my data array. This will ultimately be used as input to a random forest classifier. here is the code where i do this:
data = []
target = []
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) #load image as grayscale
mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
# describe the image
features = desc.describe(image,mask)
data.append(features)
target.append(imagePath.split("_")[-2])
When i run this code I am getting the following error:
File "myfile.py", line 53, in <module>
features = desc.describe(image,mask)
File "/usr/local/lib/python2.7/site-packages/pyimagesearch/rgbhistogram.py", line 16, in describe
mask, self.bins, [0, 256, 0, 256, 0, 256])
cv2.error: /Users/jmeunier28/Downloads/opencv-3.0.0/modules/imgproc/src/histogram.cpp:159: error: (-215) mask.size() == imsize && mask.channels() == 1 in function histPrepareImages
I have a working example of very similar code with a different dataset so I am thinking it might be a problem with the way I generate the mask of the animal images? I have checked that the mask.dtype and image.dtype are both 8 bit as well as the image and mask paths are correct. I can not think of what might be happening here. I tried printing the target and it appears it runs through the loop two times before it crashes. I am new to python and opencv and am doing this for a school project so I would really appreciate if someone might be able to give me some ideas!! thanks in advance!