I'm trying to detect background lines of a pre-processed binary image of a newspaper article using Hough lines transform.
The code I used is given below and it detects only one vertical background line, but I want to detect all the vertical background lines.
How can I improve my code to detect all the vertical background lines only as I marked in the expected output image?
import cv2 as cv
import numpy as np
import os
#binary image
image = cv.imread('../outputs/contour.jpg')
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY) # convert2grayscale
(thresh, binary) = cv.threshold(gray, 150, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
#cv.imshow('binary',binary)
#cv.waitKey(0)
minLineLength = 10
maxLineGap = 40
lines=np.array([])
lines = cv.HoughLinesP(binary,rho=np.pi/180,theta=np.pi/180,threshold=10,lines=lines,minLineLength=minLineLength,maxLineGap=maxLineGap)
for x1,y1,x2,y2 in lines[0]:
cv.line(image,(x1,y1),(x2,y2),(0,255,0),2)
cv.imshow('lines',image)
path='../outputs'
cv.imwrite(os.path.join(path , 'line.jpg'), image)
cv.waitKey(0)
The expected Output is like this:
This is a brute-force solution, you might want to optimize the parameters to make it better: