OpenCv Python failing with error (-215) on calcHist function

asked 2015-11-30 06:09:13 -0600

jmeunier28 gravatar image

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!

edit retag flag offensive close merge delete

Comments

mask = cv2.imread(maskPath) #load image as grayscale - imread(path, 0) reads as grayscale.

berak gravatar imageberak ( 2015-12-01 05:08:52 -0600 )edit

yes the cv2.cvtColor function below it does the same thing... but if I load the image using imread(maskpasth,0) i get the same error

jmeunier28 gravatar imagejmeunier28 ( 2015-12-01 07:32:03 -0600 )edit

I figured out that the size of some of my images were not the same so in order to fix it i wrote a script to resize all of my data.... stupid mistake but thought that all of my data was of the same size and therefore it was pretty hard to see. Thanks

jmeunier28 gravatar imagejmeunier28 ( 2015-12-01 15:28:34 -0600 )edit