There seems to be a syntax error in the first line of the code. The correct syntax should be:

import cv2 import numpy as np

Also, the code is drawing contours on the same image repeatedly, which may not be desirable. It is better to create separate images for each contour and display them separately. Here is an updated version of the code:

import cv2 import numpy as np

img = cv2.imread('practice.png', 0)

contours, hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

Draw first contour

img1 = np.zeros_like(img) img1 = cv2.drawContours(img1, contours, 0, 128, -1) cv2.imshow('Contour 1', img1)

Draw second contour

img2 = np.zeros_like(img) img2 = cv2.drawContours(img2, contours, 1, 200, -1) cv2.imshow('Contour 2', img2)

Draw third contour

img3 = np.zeros_like(img) img3 = cv2.drawContours(img3, contours, 2, 200, -1) cv2.imshow('Contour 3', img3)

Draw fourth contour

img4 = np.zeros_like(img) img4 = cv2.drawContours(img4, contours, 3, 200, -1) cv2.imshow('Contour 4', img4)

cv2.waitKey(0)

import cv2 import numpy as np img = cv2imreadpracticepng 0 contours hierarchy = cv2findContoursimg cv2RETR_TREE cv2CHAIN_APPROX_SIMPLE print printhierarchy img = cv2drawContoursimg contours 0 128

原文地址: https://www.cveoy.top/t/topic/bqua 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录