9

In openCV after applying canny edge detection I'd like to further process the result (show only horizontal lines, remove short lines, etc..). But the result of canny is just another image. I'd like to get an array of lines describing the detected edges

I'm aware of the famous Hough Line Transform, but the result is not always good, that's why I'd like to manually process canny result. input:

enter image description here

output canny only:

enter image description here

output canny then Hough line transform

enter image description here

This is Hough line transform result(red lines) for detecting edges of stairs. 4th line from below is not detected correctly, although canny edge detected an edge.

Any idea how to extract edges from canny image?

RAM
  • 2,257
  • 2
  • 19
  • 41
Moataz Elmasry
  • 2,509
  • 4
  • 27
  • 37
  • 1
    Your question not so clear. Everyone uses HoughLines to find lines. So better expand your question with sample images. And tell why you can't apply HoughLines with help of images. – Abid Rahman K Jun 05 '12 at 08:51
  • I added an image of not so ideal result of houghman and a description. mainly canny detects an edge, but houghman sometimes only detects part of this edge – Moataz Elmasry Jun 05 '12 at 09:30
  • 1
    Can you please upload the result of canny, just before applying houghtransform, if possible upload original image also. – Abid Rahman K Jun 05 '12 at 09:36
  • so I updated original post to include original image and canny without houghman – Moataz Elmasry Jun 05 '12 at 14:19
  • steps at the top is not captured well, they are not straight line as below steps, is that the problem? – Abid Rahman K Jun 05 '12 at 14:24
  • By the way what kind of camera is this? Why the image doesn't look normal? – Abid Rahman K Jun 05 '12 at 14:25
  • This is actually a 3d point cloud. normals of each point is calculated then a "normal image" is created, then color is caluclated so that each component of the normal corresponds to a color of the rgb, then an opencv image is created – Moataz Elmasry Jun 05 '12 at 14:32
  • The fourth line from below is the problem. in canny it looks pretty well as a line segment, but in houghman only as half a line – Moataz Elmasry Jun 05 '12 at 14:33
  • 1
    BTW: The Hough transform gets it's name from Paul Hough - not Houghman. – Chris Bennet Jul 11 '12 at 10:22

2 Answers2

15

A few things you can try to improve your results:

Apply a Region of Interest

Your image looks to have some bordering window effects. I removed them with a region of interest resulting in an image that looks like this (I tweaked it until it looked right, but if you're using some kind of kernel operator it's window size probably better defines this ROI):

enter image description here

Use standard Hough transform

It also seems you're using the probabilistic Hough transform. So, you're only getting line segments instead of an interpolated line. Consider using the standard transform to get the full theoretical line (rho, theta). Doing this I got an image like shown below:

enter image description here

Here is a code snippet I used to generate the lines (from Python interface):

(mu, sigma) = cv2.meanStdDev(stairs8u)
edges = cv2.Canny(stairs8u, mu - sigma, mu + sigma)
lines = cv2.HoughLines(edges, 1, pi / 180, 70)

Filter lines based on angle

You can probably filter out poor lines by taking the most frequently occurring line angles, and throwing away outliers. This should narrow it down to the most visible steps.

Hope that helps!

mevatron
  • 13,911
  • 4
  • 55
  • 72
  • Thanks alot for the reply. The problem with standard houghman, that it detects too long edges, i.e. edges are detected where's actually no edges. I believe detecting too few edges and then reconstructing the actual edges is better than too many false positives. Even if I take lines with dominant orientation and remove outliers I'll have too many lines, so I'll probably use the probabilistic houghman and reconstrct the missing lines. Best regards – Moataz Elmasry Jun 06 '12 at 13:57
  • 1
    Well, the length of the line is an artifact of the drawing process (since lines proceed to +/- infinity). You can limit the length of lines since you have a Canny mask. After you eliminate the poor lines by angle (theta), then you'll probably need to cluster them by their radial parameter (rho) this should get you down to the desired number of lines. You'll need to do this clustering regardless of standard or probabilistic since you have multiple lines per step edges. – mevatron Jun 06 '12 at 14:20
  • Hello. Unfortuantly I can't remove the poor lines, since I also need to detect the boundaries of the stairs(left/right). But it can be tolrated that some edges are undetected (better than false positives).What I'm doing right now is 1- detect edges with probabilistic houghman.2- build a 2d histogram of edges based on rho and theta. 3- merge almost colinear line segments. in each bin -4 remove short lines -5-due to discritization problem of histogram, lines almost on the boundary of a bin but could be merged with a line from the neighbor bin is then migrated to the other bin and lines are merg – Moataz Elmasry Jun 13 '12 at 09:39
  • Setting the Canny thresholds with the `meanStdDev` really improved my code doing something similar. Thanks! – Itay Jan 04 '15 at 21:45
8

I recommend using LSWMS (Line Segment detection using Weighted Mean-Shift) method. It's results is better than HT and PPHT.

See http://marcosnietoblog.wordpress.com/2012/04/28/line-segment-detection-opencv-c-source-code and http://www.youtube.com/watch?v=YYeX8IGOAxw

hamed
  • 597
  • 4
  • 15