gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 00:11:34 24 4
gpt4 key购买 nike

我有一个带有几个矩形的嘈杂图像。从视觉上来说,矩形是非常明显的。它们都是垂直或水平的,不会以不同的角度出现。它们的颜色/强度也相当恒定。 我想检测这些矩形,如果可能的话,检测其他矩形内部或顶部/交叉的矩形。

原始图像:

Original Image

具有预期矩形/正方形的图像(可能有细垂直线,无论它们是否被检测为矩形并不重要):

Image with expected rectangles

据我所知,这是一项相当手动的工作

  1. 将图像转换为灰度 8 位(我的图像已经是灰度)
  2. 添加高斯噪声以平滑图像
  3. 将结果转换为黑白,例如使用 adaptiveThreshold
  4. 扩大结果以尝试连接不再接触的部分
  5. 侵 eclipse 结果以消除不需要的小噪音
  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()

最佳答案

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

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

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

关于python - 检测噪声图像中几乎恒定颜色的垂直矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59606415/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com