Sharpen the edges

醉酒当歌 提交于 2019-12-06 22:57:26

I tried a quick check on the image you shared. And I got this result after I tweaked parameters of canny and hough lines a little. Not sure what kind of generalisation you need, but give it a try.

Here is my code:

import cv2 as cv2
import numpy as np

# fn = '2PeyG.jpg'
fn = 'qEFMj.jpg'
r_scale = 0.1
# OpenCV reads image with BGR format
img = cv2.imread(fn)
img = cv2.resize(img, (0, 0), fx=r_scale, fy=r_scale)
img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(img, 20, 30, apertureSize=3)


lines = cv2.HoughLines(edges, 1, np.pi/70, 35)

for rho, theta in lines[0]:
    if (np.pi/70 <= theta <= np.pi/7) or (2.056 < theta < 4.970) or (1.570 <= theta <= 1.600):

        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(img,(x1,y1),(x2,y2),(0,0,255),1)


cv2.imshow('edges', edges)
cv2.imshow('overlay', img)

# cv2.imwrite('lines_overlay.png', img)
cv2.waitKey(3000)

The result I got was

I agree this is not what you are looking exactly, but probably a good starting point. Hope this helps :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!