[英]how to separate touching shapes in a image using python
您可以嘗試擴張和侵蝕您的圖像,如下所示:
import cv2
import numpy as np
img = cv2.imread("image.jpg")
def process(img):
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_blur = cv2.GaussianBlur(img_gray, (11, 11), 1)
img_canny = cv2.Canny(img_blur, 200, 0)
kernel = np.ones((7, 7))
img_dilate = cv2.dilate(img_canny, kernel, iterations=1)
return cv2.erode(img_dilate, kernel, iterations=1)
def get_contours(img):
contours, _ = cv2.findContours(process(img), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
for cnt in contours:
area = cv2.contourArea(cnt)
if 500 < area < 10000:
cv2.drawContours(img, [cnt], -1, (0, 0, 255), -1)
get_contours(img)
cv2.imshow("Image", img)
cv2.waitKey(0)
Output:
您可以在for
循環中單獨訪問每個輪廓。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.