Error with findContours()
I'm getting the following error here:
error: (-215) scn == 3 || scn == 4 in function cv::cvtColor This is my code
def detectBoxes(img):
image = np.asarray(img)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gradient = cv2.Laplacian(gray, cv2.CV32F)
kernel = np.ones((15,15),np.uint8)
closing = cv2.morphologyEx(gradient, cv2.MORPH_CLOSE, kernel)
closing = cv2.cvtColor(closing, cv2.COLOR_BGR2GRAY)
im2, cnts, hier = cv2.findContours(closing, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
I think it's because findContours needs another type of color conversion, but I'm unsure about which one. Any clues here? Thanks
your image was either invalid (python noobs never seem to understand it), or already grayscale
I don't think that the image is invalid (I've done similar approaches with other functions in the same code and everything runs well) nor it's already grayscale (when I remove the second cvtColor, I've got another error: FindContours supports only CV_8UC1 images...)
the lesson is: you have to check , not think.
image = np.asarray(img)
<-- what is this for ? shouldn't it have a type, too ? what is the result type now ?again, why ? opencv's images are all numpy arrays already.
You can just use opencv's image reading mechanism. It is already a numpy array. Try removing that line.