I'm trying to fit lines from an binary image below. My main objective is to detect a stair model. Is there any way to do this in OpenCV ?
Thank for your help.

- 2,117
- 4
- 21
- 32

- 21
- 3
-
Add also the original image, the code you tried, actual results and expexted results. You said HT didn't work, can you show why to us? – Miki Oct 12 '15 at 11:45
4 Answers
The simplest methods I can think of are either the Hough Transform (HoughLines()
for lines and HoughLinesP()
for segments) or RANSAC (I couldn't find a RANSAC line detector in OpenCV. However there are interesting implementations in the MRPT and PCL).
EDIT: A method based on Canny()
and HoughLines()
is explained in extract lines from canny edge detection

- 3,737
- 5
- 29
- 51
Thank Miki, 865719 , Photon, and Humam Helfawi,
After using Hough-Transform, I see that the result is not good. You can see in Figure below:
Here is my code:
int main(int argc, char** argv){
Mat src, dst, color_dst;
src=imread("stairs.png");
Canny( src, dst, 50, 200, 3 );
cvtColor( dst, color_dst, CV_GRAY2BGR );
vector<Vec4i> lines;
HoughLinesP( dst, lines, 1, CV_PI/180, 80, 30, 10 );
for( size_t i = 0; i < lines.size(); i++ )
{
line( color_dst, Point(lines[i][0], lines[i][1]),
Point(lines[i][2], lines[i][3]), Scalar(0,0,255), 3, 8 );
}
namedWindow( "Source", 1 );
imshow( "Source", src );
namedWindow( "Detected Lines", 1 );
imshow( "Detected Lines", color_dst );
waitKey(0);
return 0;}
Thank for your support.
Hieu.

- 10,031
- 4
- 47
- 55

- 21
- 3
You can detect the lines in the image by using the hough transform.
http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_lines/hough_lines.html
(The question is not detailed enough for more.)

- 3,182
- 1
- 15
- 16
-
Thank you for your answer, but in this case, Hough Transform is not effective. – Huy Hiệu Phạm Oct 12 '15 at 10:22
-
You will have to describe what you're trying to accomplish with more detail then. – Photon Oct 12 '15 at 11:07
Get the contours of your images and apply the hough-transform on the contours. you will got some duplicate line (very near to each other).. just filter them on some distance criteria

- 19,566
- 15
- 85
- 160
-
-
Humam Helfawi, I don't understand how to filter based on some distance criteria. ? – Huy Hiệu Phạm Oct 13 '15 at 02:24