我试图提取图像中一个大的白色区域的坐标,如下所示: 这是原始图像:
使用一个小正方形内核,我应用了一个闭合操作来填充小孔,并帮助识别图像中的较大结构,如下所示:
import cv2
import numpy as np
import imutils
original = cv2.imread("Plates\\24.png")
original = cv2.resize(original, None, fx=3, fy=3, interpolation=cv2.INTER_CUBIC)
gray = cv2.cvtColor(original, cv2.COLOR_BGR2GRAY)
# next, find regions in the image that are light
squareKern = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
light = cv2.morphologyEx(gray, cv2.MORPH_CLOSE, squareKern)
light = cv2.threshold(light, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
生成的图像如下所示:
下面是另一个例子:
我希望能够做到的是检测板中较大的白色区域,如下所示:
请记住,在许多示例中,等高线不会很好地工作
使用您提供的一个图像:
对于如何解决这个问题,我提出了两种方法:
方法1
等高线面积比较
如您所见,图像中有3个大轮廓;顶部的矩形和下面的两个矩形,您希望将其作为一个整体进行检测
所以我在你的图像上使用了一个阈值,检测了阈值图像的轮廓,并对第二大轮廓和第三大轮廓(最大的是你想忽略的顶部矩形)进行了索引
以下是阈值图像:
我将两个轮廓堆叠在一起,并检测到两个轮廓的边界框:
输出:
方法2
阈值掩蔽
由于底部的两个矩形比板的顶部矩形更白,我使用了一个阈值来遮住板的顶部:
我在上面显示的面具上使用了canny边缘检测器
输出:
当然,如果板的顶部不比底部亮,这种方法可能无法正常工作
相关问题 更多 >
编程相关推荐