检测噪点图像中几乎恒定颜色的垂直矩形

问题描述 投票:1回答:1

我有一个带有多个矩形的嘈杂图像。从视觉上讲,这些矩形非常明显。它们都是垂直或水平,不会以不同的角度出现。它们的颜色/强度也很恒定。 我想检测那些矩形,并在可能的情况下检测其他矩形内部或顶部/交叉的矩形。

原始图片:

Original Image

具有预期矩形/正方形的图像(可能会有细的垂直线,是否将其检测为矩形并不重要):

Image with expected rectangles

从我收集的资料来看,这是一项相当手动的工作,需要执行以下操作

  1. 将图像转换为8位灰度(我的图像已经为灰度)
  2. 添加高斯噪声以使图像平滑
  3. [例如,用adaptiveThreshold将结果转换为黑白
  4. 扩展结果以尝试合并不再接触的部分
  5. 侵蚀结果以消除小的不想要的噪声
  6. 运行一些算法来检测形状

我目前正在计算以下图像:

Black and white threshold image

关于结果的注释:

  • 没有噪音(尽管在其他一些图像上,到处还是有小片噪音)
  • 矩形未全部闭合
  • 现在可能很难在垂直线旁边发现左侧的一个小矩形/正方形

我的问题是

  • 有更好的方法吗?
  • 如何继续检测当前未全部封闭的矩形?知道它们始终是水平或垂直的并且几乎是恒定的颜色应该有帮助。

注意,我也尝试过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()
python opencv contour
1个回答
0
投票

如果颜色/强度恒定,则可以使用颜色分割技术(比自适应阈值法更精确)。当有几个对象的对比度/颜色差异明显且通常带有随机调色板时,我通常使用自适应阈值。

但是在您的情况下,由于颜色一致,我们可以对颜色范围进行硬编码。使用任何颜色选择器工具获取要检测的矩形的颜色,假设颜色为155(灰度值)。然后,我们可以使用cv2.inRange(),下限为150,上限为160。您将从cv2.inRange()方法中获得二进制图像,该图像可用于找出轮廓。

您可能需要使用不同的矩形颜色多次执行上述步骤,以获取图像中的所有矩形。

© www.soinside.com 2019 - 2024. All rights reserved.