I have this image:
From that I want to detect only vertical lines, and don't want horizontal lines anyway.
I have written the following code which gives me this result with horizontal lines, too:
That's my code:
import sys
import math
import cv2 as cv
import numpy as np
def main(argv):
default_file = 'C:/Users/Rizwan/Desktop/amy_images/image2_43WqE0i.png'
filename = argv[0] if len(argv) > 0 else default_file
# Loads an image
src = cv.imread(cv.samples.findFile(filename), cv.IMREAD_GRAYSCALE)
img = cv.resize(src, (100, 40))
src = cv.medianBlur(img, 5)
# Check if image is loaded fine
if src is None:
print('Error opening image!')
print('Usage: hough_lines.py [image_name -- default ' + default_file + '] \n')
return -1
dst = cv.Canny(src, 10, 40, None, 3)
# 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, 1, np.pi / 180, 150, None, 0, 0)
if lines is not None:
for i in range(0, len(lines)):
rho = lines[i][0][0]
theta = lines[i][0][1]
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, 0, 255), 3, cv.LINE_AA)
linesP = cv.HoughLinesP(dst, 1, np.pi / 180, 50, None, 50, 10)
if linesP is not None:
for i in range(0, len(linesP)):
l = linesP[i][0]
cv.line(cdstP, (l[0], l[1]), (l[2], l[3]), (0, 0, 255), 3, cv.LINE_AA)
cv.imshow("Source", src)
cv.imshow("Detected Lines (in red) - Standard Hough Line Transform", cdst)
cv.imwrite("Source.png", cdst)
cv.imwrite("Source1.png", src)
# cv.imshow("Detected Lines (in red) - Probabilistic Line Transform", cdstP)
cv.waitKey()
return 0
if __name__ == "__main__":
main(sys.argv[1:])
And one thing more that this gives me two lines in first-line and two lines in the second line as in original image there are only two thick lines but in the second image it giving me 4 vertical lines.
Any help would be highly appreciated.




