Ask Your Question

Approximating a set of points from a binary image

asked 2015-04-05 21:02:59 -0500

exzamp gravatar image

updated 2015-08-25 17:01:55 -0500

My task is to plot a line that reppresents the approximation (linerar I guess) to a set of points. Example: image description -draw a line that best fits the white points from this image. I am aware of functions such as fitLine but how can I use them when I represent images as Mat?

edit retag flag offensive close merge delete


Thank you so much :)! for someone following your example and being as bad as me don't forget to change the the depth to fulfill fitline() assertions.

exzamp gravatar imageexzamp ( 2015-04-06 11:50:39 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-04-06 05:21:42 -0500

LBerger gravatar image

updated 2015-04-06 05:25:30 -0500

a sample without cheking (b, depth...):

int main(int ac, char** av) 
Mat m,mc;

cvtColor(mc, m, CV_BGR2GRAY);
vector<cv::Point2d> p;
Mat line_fit;
for (int i=0;i<m.rows;i++)
    for (int j=0;j<m.cols;j++)
        if (<unsigned char>(i,j)!=0)
cout<<"Coordinates : \n";
fitLine(p, line_fit, CV_DIST_L2, 0, 0.01, 0.01);
double a,b,c;
b =<float>(0, 0);
a =<float>(1, 0);
c = -(a*<float>(2, 0) + b*<float>(3, 0));

imshow("Fit", mc);
edit flag offensive delete link more

Question Tools

1 follower


Asked: 2015-04-05 21:02:59 -0500

Seen: 778 times

Last updated: Apr 05 '15