opencv 从图像中删除直线

ktecyv1j  于 2023-03-30  发布在  其他
关注(0)|答案(1)|浏览(254)

我想从我的形象删除线,这将是很容易的,我可以只是侵 eclipse 他们死亡,然后扩大,但有些字母是空的,所以我不能这样做。

"""Importing the modules"""
import cv2
import numpy as np

"""Importing the image and resizing it"""
BASE_IMG = cv2.imread(r"C:\Users\usr\Documents\Python\Projects\CaptchaSolver\captcha.png")
BASE_IMG = cv2.resize(BASE_IMG, (480, 360))
img = cv2.imread(r"C:\Users\usr\Documents\Python\Projects\CaptchaSolver\captcha.png", 
cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (480, 360))

"""Converting it to binary image and trying my best to get rid of the lines"""
_, img = cv2.threshold(img, 100, 255, cv2.THRESH_BINARY)
img = ~img
img = cv2.erode(img, np.ones((2, 2), np.uint8), iterations=2)
img = cv2.dilate(img, (5, 5), iterations = 3)
img = cv2.Canny(img, 100, 100, edges=1)

cv2.imshow("Base", BASE_IMG)
cv2.imshow("Output", img)
cv2.waitKey(0)

如果你想知道更多细节可以问

ohfgkhjo

ohfgkhjo1#

其中一种方法可以是进行边缘检测,应用Hough变换,然后为cv2.inpaint方法创建掩码。下面是这种方法的代码:

import cv2
import numpy as np

image = cv2.imread('input.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)[:,:,2]
gray = cv2.GaussianBlur(gray, (3, 3), 0)
edged = cv2.Canny(gray, 50, 200)

lines = cv2.HoughLines(edged, 1, np.pi/180, 90)

height, width = image.shape[:2]
mask = np.zeros((height,width), np.uint8)

for line in lines:
    rho, theta = line[0]
    a = np.cos(theta)
    b = np.sin(theta)
    x0 = a*rho
    y0 = b*rho
    x1 = int(x0 + 1000*(-b))
    y1 = int(y0 + 1000*(a))
    x2 = int(x0 - 1000*(-b))
    y2 = int(y0 - 1000*(a))
    cv2.line(mask,(x1,y1),(x2,y2),(255,255,255),3)

output = cv2.inpaint(image, mask, 3, flags=cv2.INPAINT_NS)

cv2.imshow("Output", output)
cv2.waitKey(0)

并且输出:

相关问题