I want to detect the division line in the scanned document, and instead of using cv2.Canny
, I used cv2.threshold
to get a pretty clean pre-processed image. However my parameter for cv2.HoughLines
may be improper and I got a chaos in the final output. And the lines didn`t present as the set color.
My code is:
import cv2
import numpy as np
from matplotlib import pyplot as plt
## (1) read
img = cv2.imread("q11.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
plt.figure(figsize=(17,17))
plt.imshow(gray,cmap='gray')
## (2) threshold
th, threshed = cv2.threshold(gray, 200, 20, cv2.THRESH_BINARY_INV|cv2.THRESH_OTSU)
plt.figure(figsize=(15,15))
plt.imshow(threshed,cmap='gray')
## (3) HoughLines
lines = cv2.HoughLines(threshed,rho=1,theta=np.pi/180,threshold = 800)
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(threshed,(x1,y1),(x2,y2),(0,0,255),2)
plt.figure(figsize=(10, 10))
plt.subplot(111),plt.imshow(threshed)
plt.title('hough'), plt.xticks([]), plt.yticks([])
and after cv2.threshold
with cv2.THRESH_BINARY_INV|cv2.THRESH_OTSU
I got:
What I actually got: