获得二值图像后,您可以找到轮廓并使用轮廓近似和轮廓区域进行过滤。如果近似轮廓的长度为四,那么它一定是正方形,如果它在上下区域范围内,那么我们检测到了一个标记。我们保留一个标记计数器,如果图像中有三个标记,我们可以拍照。这是该过程的可视化。
我们Otsu's threshold 获得二值图像,其中要检测的对象为白色。
从这里我们使用cv2.findContours 找到轮廓,并使用轮廓近似cv2.approxPolyDP 以及轮廓区域cv2.contourArea 进行过滤。
检测到的以蓝绿色突出显示的标记
我用 Python 实现了它,但你可以采用相同的方法
代码
import cv2
# Load image, grayscale, Otsu's threshold
image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Find contours and filter using contour approximation and contour area
marks = 0
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
area = cv2.contourArea(c)
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.04 * peri, True)
if len(approx) == 4 and area > 250 and area < 400:
x,y,w,h = cv2.boundingRect(c)
cv2.rectangle(image, (x, y), (x + w, y + h), (200,255,12), 2)
marks += 1
# Sheet has 3 marks
if marks == 3:
print('Take photo')
cv2.imshow('thresh', thresh)
cv2.imshow('image', image)
cv2.waitKey()