【发布时间】:2019-02-25 23:42:41
【问题描述】:
我正在尝试教我的测试自动化框架使用 opencv 检测应用程序中的选定项目(该框架从被测设备中抓取帧/屏幕截图)。选定的项目总是有一定的大小,总是有蓝色边框,这有帮助,但它们包含不同的缩略图。请参阅提供的示例图片。
我已经对该主题进行了很多谷歌搜索和阅读,并且我已经接近让它在示例图像中的图像 C 的一个场景中正常工作。 example image 这是选中项目上有播放符号的地方。
我的理论是 OpenCV 在这种情况下会感到困惑,因为播放符号基本上是圆形的,里面有一个三角形,我要求它找到一个矩形。
我发现这很有帮助:https://www.learnopencv.com/blob-detection-using-opencv-python-c/
我的代码如下所示:
import cv2
import numpy as np
img = "testimg.png"
values = {"min threshold": {"large": 10, "small": 1},
"max threshold": {"large": 200, "small": 800},
"min area": {"large": 75000, "small": 100},
"max area": {"large": 80000, "small": 1000},
"min circularity": {"large": 0.7, "small": 0.60},
"max circularity": {"large": 0.82, "small": 63},
"min convexity": {"large": 0.87, "small": 0.87},
"min inertia ratio": {"large": 0.01, "small": 0.01}}
size = "large"
# Read image
im = cv2.imread(img, cv2.IMREAD_GRAYSCALE)
# Setup SimpleBlobDetector parameters.
params = cv2.SimpleBlobDetector_Params()
# Change thresholds
params.minThreshold = values["min threshold"][size]
params.maxThreshold = values["max threshold"][size]
# Filter by Area.
params.filterByArea = True
params.minArea = values["min area"][size]
params.maxArea = values["max area"][size]
# Filter by Circularity
params.filterByCircularity = True
params.minCircularity = values["min circularity"][size]
params.maxCircularity = values["max circularity"][size]
# Filter by Convexity
params.filterByConvexity = False
params.minConvexity = values["min convexity"][size]
# Filter by Inertia
params.filterByInertia = False
params.minInertiaRatio = values["min inertia ratio"][size]
# Create a detector with the parameters
detector = cv2.SimpleBlobDetector(params)
# Detect blobs.
keypoints = detector.detect(im)
for k in keypoints:
print k.pt
print k.size
# Draw detected blobs as red circles.
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures
# the size of the circle corresponds to the size of blob
im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]), (0, 0, 255),
cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
# Show blobs
cv2.imshow("Keypoints", im_with_keypoints)
cv2.waitKey(0)
如何让 OpenCV 只查看由蓝色边框定义的外部形状而忽略内部形状(播放符号,当然还有缩略图)?我敢肯定它一定是可行的。
【问题讨论】:
-
我还有一个问题。如果图像周围没有边框怎么办?有什么方法可以检测白色背景下略带圆角的缩略图?
标签: python python-2.7 opencv image-processing computer-vision