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)