【问题标题】:Take photo when pattern is detected in image with Android OpenCV使用 Android OpenCV 在图像中检测到图案时拍照
【发布时间】:2020-05-29 22:00:31
【问题描述】:

你好 stackoverflow 社区我想如果有人能指导我一点关于我的下一个问题,我想制作一个应用程序,当它检测到一张带有 3 个标记(角落中的黑色方块)的纸张时拍照,类似于什么QR会有。我已经阅读了一些关于 opencv 的内容,我认为这可以帮助我更多,但我还不是很清楚。

这是我的例子

【问题讨论】:

  • 我建议您阅读有关 Camera2 Api 的内容,感谢它,您可以创建自己的 图像分析 用例,在其中您可以实现所需的任何逻辑。

标签: android image opencv image-processing camera


【解决方案1】:

获得二值图像后,您可以找到轮廓并使用轮廓近似和轮廓区域进行过滤。如果近似轮廓的长度为四,那么它一定是正方形,如果它在上下区域范围内,那么我们检测到了一个标记。我们保留一个标记计数器,如果图像中有三个标记,我们可以拍照。这是该过程的可视化。

我们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()

【讨论】:

    猜你喜欢
    • 2020-09-14
    • 2013-01-14
    • 1970-01-01
    • 2015-02-10
    • 1970-01-01
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多