我有一个带有多个矩形的嘈杂图像。从视觉上讲,这些矩形非常明显。它们都是垂直或水平,不会以不同的角度出现。它们的颜色/强度也很恒定。 我想检测那些矩形,并在可能的情况下检测其他矩形内部或顶部/交叉的矩形。
原始图片:
具有预期矩形/正方形的图像(可能会有细的垂直线,是否将其检测为矩形并不重要):
从我收集的资料来看,这是一项相当手动的工作,需要执行以下操作
adaptiveThreshold
将结果转换为黑白我目前正在计算以下图像:
关于结果的注释:
我的问题是
注意,我也尝试过Canny,但效果不佳。
我正在将OpenCV 4.1.2与Python 3.7.2结合使用。这是我当前的代码:
import cv2
import numpy
import platform
import sys
print("Python version: {}\nOpenCV version: {}".format(platform.python_version(), cv2.__version__))
# Used variables:
# For gaussian blur
gaussianBlur = 11
# For threshold
meanType = cv2.ADAPTIVE_THRESH_MEAN_C
meanTypeName = "Mean"
blockSize = 17
c = 3
# For close/open
growSize = 6
shrinkSize = 3
# Import image.
imageName = sys.argv[1]
image = cv2.imread(imageName)
# Convert to gray scale 8 bit, blur then take threshold.
grayscaled = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(grayscaled, (gaussianBlur, gaussianBlur), 0)
thresholded = cv2.adaptiveThreshold(blurred, 255, meanType, cv2.THRESH_BINARY_INV, blockSize, c)
# Close then Open to try to "close" the rectangles and remove noise.
rectClose = cv2.getStructuringElement(cv2.MORPH_RECT, (growSize,growSize))
rectOpen = cv2.getStructuringElement(cv2.MORPH_RECT, (shrinkSize,shrinkSize))
mask = cv2.morphologyEx(thresholded, cv2.MORPH_CLOSE, rectClose)
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, rectOpen)
result = mask
# Compute contours and display them on the gray scale image
contours, hierarchy = cv2.findContours(result, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
resultWithContours = grayscaled
cv2.drawContours(resultWithContours, contours, -1, (0,0,255), cv2.FILLED)
# Display threshold image and original with detected contours.
both = numpy.concatenate([result, resultWithContours], axis=0)
cv2.imshow("{} Block Size={} C={}".format(meanTypeName, blockSize, c), both)
# Save both threshold and original with detected contours.
cv2.imwrite("{}_result_{}_blockSize{}_c{}.jpg".format(imageName, meanTypeName, blockSize, c), result)
cv2.imwrite("{}_contours_{}_blockSize{}_c{}.jpg".format(imageName, meanTypeName, blockSize, c), resultWithContours)
cv2.waitKey(0)
cv2.destroyAllWindows()
如果颜色/强度恒定,则可以使用颜色分割技术(比自适应阈值法更精确)。当有几个对象的对比度/颜色差异明显且通常带有随机调色板时,我通常使用自适应阈值。
但是在您的情况下,由于颜色一致,我们可以对颜色范围进行硬编码。使用任何颜色选择器工具获取要检测的矩形的颜色,假设颜色为155
(灰度值)。然后,我们可以使用cv2.inRange()
,下限为150
,上限为160
。您将从cv2.inRange()
方法中获得二进制图像,该图像可用于找出轮廓。
您可能需要使用不同的矩形颜色多次执行上述步骤,以获取图像中的所有矩形。