首页 > 解决方案 > 使用 OpenCV 获得完整的形状图案

问题描述

我正在编写一个脚本,使用不同的 OpenCV 操作来处理带有屋顶太阳能电池板的图像。我的原始图像如下:

原始图像

处理图像后,我得到面板的边缘如下:

处理后的图像

可以看到由于图片中太阳的反射,一些矩形是如何被打破的。

我想知道是否有可能修复那些破损的矩形,也许可以使用那些没有破损的图案。

我的代码如下:

# Load image
color_image = cv2.imread("google6.jpg")
cv2.imshow("Original", color_image)
# Convert to gray
img = cv2.cvtColor(color_image, cv2.COLOR_BGR2GRAY)

# Apply various filters
img = cv2.GaussianBlur(img, (5, 5), 0)
img = cv2.medianBlur(img, 5)
img = img & 0x88 # 0x88
img = cv2.fastNlMeansDenoising(img, h=10)

# Invert to binary
ret, thresh = cv2.threshold(img, 127, 255, 1) 

# Perform morphological erosion
kernel = np.ones((5, 5),np.uint8)
erosion = cv2.morphologyEx(thresh, cv2.MORPH_ERODE, kernel, iterations=2)

# Invert image and blur it
ret, thresh1 = cv2.threshold(erosion, 127, 255, 1)
blur = cv2.blur(thresh1, (10, 10))

# Perform another threshold on blurred image to get the central portion of the edge
ret, thresh2 = cv2.threshold(blur, 145, 255, 0)

# Perform morphological erosion to thin the edge by ellipse structuring element
kernel1 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
contour = cv2.morphologyEx(thresh2, cv2.MORPH_ERODE, kernel1, iterations=2)

# Get edges
final = cv2.Canny(contour, 249, 250)
cv2.imshow("final", final)

我试图修改我正在使用的所有滤镜,以尽可能减少原始图片中太阳的影响,但这是我所能做到的。

我对所有这些过滤器的结果总体上很满意(尽管欢迎任何建议),所以我想处理我展示的黑白图像,这对于我需要的后期处理来说已经足够平滑了做。

谢谢!

标签: pythonopencvimage-processingimagefilter

解决方案


原始图像中的图案没有被破坏,因此在您的二值化结果中被破坏一定意味着您的二值化不是最佳的。

您申请二值threshold()化图像,然后Canny()二值化图像。这里的问题是:

  1. 阈值删除了很多信息,这应该始终是任何处理管道的最后一步。你在这里失去的任何东西,你已经永远失去了。
  2. Canny()应该应用于灰度图像,而不是二值图像。
  3. Canny 边缘检测器是一个边缘检测器,但您想要检测的是线条,而不是边缘。看到这里的区别。

所以,我建议从头开始。

高斯拉普拉斯算子是一个非常简单的线检测器。我采取了这些步骤:

  1. 读入图像,转换为灰度。
  2. 应用 sigma = 2 的高斯拉普拉斯算子。
  3. 反转(否定)结果,然后将负值设置为 0。

这是输出:

过程的输出

从这里开始,识别网格图案应该相对简单。

我没有发布代码,因为我为此使用了 MATLAB,但是您可以在 Python 中使用 OpenCV 完成相同的结果,这是在 OpenCV 中应用高斯拉普拉斯算子的演示


这是复制上述内容的 Python + OpenCV 代码:

import cv2

color_image = cv2.imread("/Users/cris/Downloads/L3RVh.jpg")
img = cv2.cvtColor(color_image, cv2.COLOR_BGR2GRAY)
out = cv2.GaussianBlur(img, (0, 0), 2)  # Note! Specify size of Gaussian by the sigma, not the kernel size
out = cv2.Laplacian(out, cv2.CV_32F)
_, out = cv2.threshold(-out, 0, 1e9, cv2.THRESH_TOZERO)

但是,当从 BGR 转换为灰度时,OpenCV 似乎没有线性化(应用伽马校正),就像我在创建上面的图像时使用的转换函数一样。我认为这种伽马校正可能会通过减少对屋顶瓦片的响应来稍微改善结果。


推荐阅读