1
votes

Problem: I want to detect lines in a given image using OpenCV in Python. Although there are multiple obvious vertical lines, neither normal HoughLines nor probabilistic HoughLines does find them. As I spent plenty of time playing around with the Parameters, I guess I am doing something fundamental wrong here. I am Aware of the fact, that hough-lines is usually applied on edges, e.g. after using canny. Due to canny´s non-maximum supression, canny does not give good results here.

Image, where detecting the vertical lines Fails :

enter image description here

Why: Given this (image of a water meter) :

enter image description here

I want to detect the rectangle around each digit. To detect the rectangles, I used sobel filters in x and y direction and calculated Magnitude and angle/Phase of the Gradient. As I assume the image to be rotated correctly in this step, I extract vertical and horizontal edges as shown in the image. My hope was to make use of houghLines to find the bounding boxes. Finding the horizontal lines works perfectly, as seen in the

Debug plot containing further insights on the Problem, where as I does not work on the vertical components (second row) :

enter image description here

Detecting the rectangles around each digit would help me to

  1. locate the Region of Interest
  2. cut out the region inside the rectangle, in other words the digit. Several other approches to detect the digits directly by using contours, all had the problem of the outer rectangles interfering with the digit.

Update: the Code for detecting the vertical lines:

#img is initialized with the binarized, vertical component image, as shown above
minLength = 30
maxGap = 7
angle_res = np.pi / 180
rad_res = 2
threshold_val = 100

linesP = cv2.HoughLinesP(img, rad_res, angle_res, threshold_val, minLineLength=minLength, maxLineGap=maxGap)

cdst = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
cdstP = np.copy(cdst)
if linesP is None:
    print("Error when finding lines (probabilistic hough transformation). No lines detected")
else:
    # Copy edges to the images that will display the results in BGR
    for i in range(0, len(linesP)):
        l = linesP[i][0]
        cv2.line(cdstP, (l[0], l[1]), (l[2], l[3]), (255,0,0), 3, cv2.LINE_AA)

plt.imshow(cdstP); plt.show()
1
Can you show your codes?Mario

1 Answers

1
votes

First apply canny edge with proper settings of threshold. Then apply probabilistic hough line transform. After applying hough transform filter the lines with slope. You want to filter the box so you need to filter horizontal and vertical lines. After filtering lines apply morphological dilation and erosion operation back to back to resultant image to get neat box around each digit. While applying hough transform select parameters minimum line length, maximum line length and maximum line gap appropriately. You can use trackbar function while selecting appropriate parameters. The sample code is given below for selection of threshold for canny edge.

import cv2
import numpy as np

cv2.namedWindow('Result')
img = cv2.imread('qkEuE.png')

v1 = 0
v2 = 0

def doEdges():
    edges = cv2.Canny(img,v1,v2)
    edges = cv2.cvtColor(edges,cv2.COLOR_GRAY2BGR)
    res = np.concatenate((img,edges),axis = 0)
    cv2.imshow('Result',res)
def setVal1(val):
    global v1
    v1 = val
    doEdges()
def setVal2(val):
    global v2
    v2 = val
    doEdges()

cv2.createTrackbar('Val1','Result',0,500,setVal1)
cv2.createTrackbar('Val2','Result',0,500,setVal2)

cv2.imshow('Result',img)
cv2.waitKey(0)
cv2.destroyAllWindows

Hope it helps you.