使用 OpenCV 获取完整的形状模式

Mus*_*ord 1 python opencv image-processing imagefilter

我正在使用不同的 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)
Run Code Online (Sandbox Code Playgroud)

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

我总体上对所有这些过滤器的结果感到满意(尽管欢迎提出任何建议),所以我想处理我展示的黑白图像,它已经足够平滑,可以进行我需要的后期处理做。

谢谢!

Cri*_*ngo 5

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

您先申请threshold()对图像进行二值化,然后再Canny()对二值图像进行处理。这里的问题是:

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

所以,我建议从头开始。

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

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

这是输出:

过程的输出

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

我不发布代码,因为我为此使用了 MATLAB,但是您可以使用 OpenCV 在 Python 中完成相同的结果,这是在 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)
Run Code Online (Sandbox Code Playgroud)

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