[python]代码库
import cv2
import numpy as np
def process(img):
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_blur = cv2.GaussianBlur(img_gray, (3, 3), 2)
img_canny = cv2.Canny(img_blur, 50, 9)
img_dilate = cv2.dilate(img_canny, np.ones((4, 2)), iterations=11)
img_erode = cv2.erode(img_dilate, np.ones((13, 7)), iterations=4)
return cv2.bitwise_not(img_erode)
def get_contours(img):
contours, _ = cv2.findContours(process(img), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cnt = max(contours, key=cv2.contourArea)
cv2.drawContours(img, [cv2.convexHull(cnt)], -1, (0, 0, 255), 2)
img = cv2.imread("image2.png")
get_contours(img)
cv2.imshow("result", img)
cv2.waitKey(0)
cv2.destroyAllWindows()