这里是主要思想:
- 将图像转换为灰度和模糊图像
- 执行精确边缘检测
- 查找图像的轮廓并查找每个轮廓的区域
- 过滤最大轮廓区域并裁剪 ROI 部分
Canny 边缘检测
现在我们遍历每个轮廓并过滤以绿色突出显示的最大轮廓。
从边界框坐标裁剪 ROI
import numpy as np
import cv2
original_image = cv2.imread("1.png")
image = original_image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (3, 3), 0)
canny = cv2.Canny(blurred, 120, 255, 1)
# Find contours in the image
cnts = cv2.findContours(canny.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
# Obtain area for each contour
contour_sizes = [(cv2.contourArea(contour), contour) for contour in cnts]
# Find maximum contour and crop for ROI section
if len(contour_sizes) > 0:
largest_contour = max(contour_sizes, key=lambda x: x[0])[1]
x,y,w,h = cv2.boundingRect(largest_contour)
cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)
ROI = original_image[y:y+h, x:x+w]
cv2.imshow("ROI", ROI)
cv2.imshow("canny", canny)
cv2.imshow("detected", image)
cv2.waitKey(0)