OpenCV图像校正以填充图像中的空白和孔

时间:2020-01-27 07:41:42

标签: python image opencv image-processing computer-vision

我目前正在研究一个带有平面图图像的项目。我正在处理一个有一定输出的问题,但通常需要进行一些校正。这就是我所拥有的:

上面的图像是预测的输出,下面的图像是基本事实。例如,纠正缺少黑色填充物的斑点的最佳方法是什么?

enter image description here

1 个答案:

答案 0 :(得分:2)

一个想法是使用形态转换巧妙的技巧。如果仅使用普通变形封闭来填充孔,则由于墙的弯曲末端而无法使用。因此,为了解决这个问题,我们可以通过先检测所有水平墙,然后一次检测垂直墙,将墙分为两部分。隔离每个方向后,我们将找到矩形轮廓,该轮廓将有效地创建墙的拐角点。这是可视化效果:

输入图片

由矩形填充绘制的水平和垂直垂直墙

组合口罩

输入图像上的颜色蒙版以获得结果

这是带有第二个输入图像的结果

代码

import cv2
import numpy as np

# Load image, create mask, grayscale, and Otsu's threshold
image = cv2.imread('2.png')
mask = np.zeros(image.shape, dtype=np.uint8)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Perform morph operations
open_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, open_kernel, iterations=1)
close_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9,9))
close = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, close_kernel, iterations=3)

# Find horizontal sections and draw rectangle on mask 
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (25,3))
detect_horizontal = cv2.morphologyEx(close, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
cnts = cv2.findContours(detect_horizontal, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    cv2.rectangle(mask, (x, y), (x + w, y + h), (255,255,255), -1)
    cv2.rectangle(mask, (x, y), (x + w, y + h), (255,255,255), 2)

# Find vertical sections and draw rectangle on mask 
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,25))
detect_vertical = cv2.morphologyEx(close, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
cnts = cv2.findContours(detect_vertical, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    cv2.rectangle(mask, (x, y), (x + w, y + h), (255,255,255), -1)
    cv2.rectangle(mask, (x, y), (x + w, y + h), (255,255,255), 2)

# Color mask onto original image
mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
image[mask==255] = [0,0,0]

cv2.imshow('opening', opening)
cv2.imshow('close', close)
cv2.imshow('image', image)
cv2.imshow('thresh', thresh)
cv2.imshow('mask', mask)
cv2.waitKey()
相关问题