Ask Your Question

how to test - is line straight ?

asked 2014-11-17 16:01:42 -0500

sundayman gravatar image

updated 2015-09-04 04:18:50 -0500

Hello I'm quite newbie - and i have following question ; can i use opencv to check, is line straight ? I have laser line projected on white surface ( from some angle ). Now, if surface is flat, line will be stright. If surface will be curved - line will be also curved.

I would like to check, is this curvature in acceptable range. Can be it done with opencv ?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2014-11-18 10:33:50 -0500

Haris gravatar image

You could simply use contour analysis to decide whether the line is straight or not.

If the line is perfectly straight then you will get only two end points on contour list after find contour, but make sure that the thickness of the input line is 1, otherwise make it 1 using thinning algorithm as explained here.

In case if the input image is not straight while looking pixel-wise but it appeared straight then apply approxPolyDP on found contours and adjust epsilon till you get satisfactory result.

Here is some code snippets which will show how to implement above method, here I manually create a black image and draw a string line on it.

    //Create an empty Mat and draw a straight line, here make sure that thickness=1
    Mat tmp(480,640,CV_8UC1,Scalar(0));

    //find contours and store points to vactor
    vector< vector <Point> > contours;
    vector< Vec4i > hierarchy;
    findContours( tmp.clone(), contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); //Find contour

    //here consider only the first contour in the src image
      cout<<"No of Contour Points : "<<contours[0].size()<<endl; //here two means perfect line

      for( int i = 0; i< contours[0].size(); i++ )
      circle(tmp,Point(contours[0][i]),3,Scalar(255),1,CV_AA); // draw each points find in the line

edit flag offensive delete link more


According to your answer, contours[i].size() == 2 means line is perfectly straight. What if the line is not perfectly straight? I believe finding "not perfectly straight line" is more important in many cases. Could you show how to use approxPolyDP as well? Or recoomend any guide?

Scott_777 gravatar imageScott_777 ( 2020-04-09 19:42:40 -0500 )edit

Question Tools


Asked: 2014-11-17 16:01:42 -0500

Seen: 2,087 times

Last updated: Nov 18 '14