我正在使用不同的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)
我试图修改我正在使用的所有滤镜,以尽可能减少原始图片中太阳的影响,但这是我所能做到的。总的来说,我对所有这些滤镜的结果感到满意(尽管欢迎提出任何建议),所以我想对显示的黑白图像进行处理,该图像对于我需要进行的后期处理已经足够平滑了。做。
谢谢!
最佳答案
图案在原始图像中没有破裂,因此在二值化结果中破裂,这意味着二值化不是最佳的。
您将threshold()
应用于二进制图像,然后将Canny()
应用于二进制图像。这里的问题是:
Canny()
应该应用于灰度图像,而不是二进制图像。 因此,我建议从头开始。
The Laplacian of Gaussian is a very simple line detector。我采取了以下步骤:
这是输出:
从这里开始,识别网格图案应该相对简单。
我没有发布代码,因为我为此使用了MATLAB,但是您可以使用OpenCV here is a demo for applying the Laplacian of Gaussian in OpenCV在Python中完成相同的结果。
这是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不会线性化(应用 Gamma 校正),因为转换功能与我在上面创建图像时使用的转换功能相同。我认为这种 Gamma 校正可能会通过减少对屋顶砖的响应来改善结果。
关于python - 使用OpenCV获得完整的形状图案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64170489/