user9270170
user9270170

Reputation:

The effect of using cv2.HoughLines() is wrong

I refer to this link for experiments.

This is the original picture:

Original

My test code:

import cv2
import numpy as np

img = cv2.imread('E:/image/sudoku.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize = 3)

lines = cv2.HoughLines(edges,1,np.pi/180,200)
for rho,theta in lines[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(img,(x1,y1),(x2,y2),(0,0,255),2)

cv2.imwrite('E:/image/myhoughlines.jpg',img)
cv2.imshow('1',img)
cv2.waitKey(0)

The result of my code running:

my

But I want this effect:

want

Where is wrong?

Upvotes: 1

Views: 288

Answers (1)

user9270170
user9270170

Reputation:

I know where is wrong! The code of the official website is less a loop. The code is changed to this:

import cv2
import numpy as np

img = cv2.imread('E:/image/sudoku.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
lines = cv2.HoughLines(edges,1,np.pi/180,200)
for i in range(len(lines)):
    for rho,theta in lines[i]:
        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),2)
cv2.imwrite('E:/image/myhoughlines.jpg',img)
cv2.imshow('1',img)
cv2.waitKey(0)

Upvotes: 1

Related Questions