Ask Your Question

Error with findContours()

asked 2018-10-15 04:46:29 -0500

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

edit retag flag offensive close merge delete


your image was either invalid (python noobs never seem to understand it), or already grayscale

berak gravatar imageberak ( 2018-10-15 05:02:35 -0500 )edit

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...)

norhther gravatar imagenorhther ( 2018-10-15 05:10:17 -0500 )edit

the lesson is: you have to check , not think.

berak gravatar imageberak ( 2018-10-15 05:10:59 -0500 )edit

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.

berak gravatar imageberak ( 2018-10-15 05:14:16 -0500 )edit

You can just use opencv's image reading mechanism. It is already a numpy array. Try removing that line.

opencv-newb gravatar imageopencv-newb ( 2018-10-15 06:54:48 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-11-19 19:06:36 -0500

supra56 gravatar image

This should work too. image = np.asarray(img).copy()

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-10-15 04:46:29 -0500

Seen: 81 times

Last updated: Nov 19