这是一种基于照片不会相互交叉的假设的方法
- 转换为灰度和高斯模糊
- 阈值图像
- 寻找轮廓并获得边界框轮廓
- 提取投资回报率
阈值图像
接下来我们使用cv2.findContours() 获取轮廓,并使用cv2.boundingRect() 获取边界框。然后我们可以使用
提取 ROI
x,y,w,h = cv2.boundingRect(c)
ROI = original[y:y+h, x:x+w]
这是结果
照片#1
照片#2
照片#3
照片#4
import cv2
image = cv2.imread('1.png')
original = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (3, 3), 0)
thresh = cv2.threshold(blurred, 230,255,cv2.THRESH_BINARY_INV)[1]
# Find contours
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
# Iterate thorugh contours and filter for ROI
image_number = 0
for c in cnts:
x,y,w,h = cv2.boundingRect(c)
cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)
ROI = original[y:y+h, x:x+w]
cv2.imwrite("ROI_{}.png".format(image_number), ROI)
image_number += 1
cv2.imshow('thresh', thresh)
cv2.imshow('image', image)
cv2.waitKey(0)