Opencv[cv2.findcontours resolving image data type = 0 is not supported]

Source: Internet
Author: User

When looking for the outline of a two-value image, give the Cv2.findcontours function a second-value picture, throw an exception:

(_, CNTs, _) = Cv2.findcontours (Mask, Cv2. Retr_external, Cv2. chain_approx_simple)
typeerror:image Data type = 0 is not supported

So simplify the example, get an example and run normally:

Import Cv2

If __name__ = = ' __main__ ':
    img = cv2.imread ('/home/xzchuang/download/book/1692612937.jpg ')
    Imgray = Cv2.cvtcolor (IMG, Cv2. Color_bgr2gray)
    ret, thresh = Cv2.threshold (Imgray, 1, 255, 0)
    print (thresh.dtype) for
    I in range ( Thresh.shape[0]): For
        J in range (Thresh.shape[1]):
            if THRESH[I][J]! = 0 and thresh[i][j]! = 255:
                Print (' exception: {} '. Format (Thresh[i][j]))
    im2, contours, hierarchy = Cv2.findcontours (Thresh, Cv2. Retr_tree, Cv2. Chain_approx_simple)
    cv2.drawcontours (img, contours,-1, (0, 0, 255), 3)

    cv2.imshow ("img", IMG)
    Cv2.waitkey (500)

Compare the original image, all pixel values: 255/0

My own example is 255/0.

The shape of the image also returns the same type to look at the type.
Image.dtype. Sure enough, the problem is here, the dtype in the streamlined example is uint8, and the type I get in my project is int64, That is, the Cv2.findcontours function accepts the picture parameter (each pixel value must be uint8), so the solution is naturally to convert the int64 type of picture to Uint8: Image.astype (np.uint8)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.