I have a simple case of a rotated rectangle, and I expect houghline function to give me the borders of that rectangle. But it only give me one border. I cannot make the threshold too low or it will get too many noise line on other cases. I also think this case is simple enough for algorithm to find borders. Do any one have some advices? Thank you
- original image
I expect to get both border of two axis
def line(img_):
dst = cv.Canny(img_, 50, 200, None, 3)
print("canny")
plt.imshow(dst)
plt.show()
# Copy edges to the images that will display the results in BGR
cdst = cv.cvtColor(dst, cv.COLOR_GRAY2BGR)
cdstP = np.copy(cdst)
lines = cv.HoughLines(dst, 4, np.pi / 90, 60)
thetas_h = []
thetas_v = []
if lines is not None:
for i in range(0, len(lines)):
rho = lines[i][0][0]
theta = lines[i][0][1]
print(theta)
if (theta > np.pi / 4) and (theta < np.pi*3 / 4):
thetas_h.append(theta)
a = math.cos(theta)
b = math.sin(theta)
x0 = a * rho
y0 = b * rho
pt1 = (int(x0 + 1000*(-b)), int(y0 + 1000*(a)))
pt2 = (int(x0 - 1000*(-b)), int(y0 - 1000*(a)))
cv.line(cdst, pt1, pt2, (255,0,0), 3, cv.LINE_AA)
else:
theta = np.pi/2 - abs(theta - np.pi/2)
thetas_v.append(theta)
a = math.cos(theta)
b = math.sin(theta)
x0 = a * rho
y0 = b * rho
pt1 = (int(x0 + 1000*(-b)), int(y0 + 1000*(a)))
pt2 = (int(x0 - 1000*(-b)), int(y0 - 1000*(a)))
cv.line(cdst, pt1, pt2, (0,255,0), 3, cv.LINE_AA)
print("thetas_h",thetas_h)
print("thetas_v",thetas_v)
plt.imshow(cdst)
plt.show()
return (thetas_h, thetas_v)
img = cv2.imread("img.jpg")
(thetas_h, thetas_v) = line(img)