Ask Your Question
1

Approximating a set of points from a binary image

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

exzamp gravatar image

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

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

Comments

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 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

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

LBerger gravatar image

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

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

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

mc=imread("14282855632031481.jpg",cv::IMREAD_UNCHANGED);
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 (m.at<unsigned char>(i,j)!=0)
            p.push_back(Point2d(j,i));
cout<<"Coordinates : \n";
cout<<p;
fitLine(p, line_fit, CV_DIST_L2, 0, 0.01, 0.01);
double a,b,c;
b = -line_fit.at<float>(0, 0);
a = line_fit.at<float>(1, 0);
c = -(a*line_fit.at<float>(2, 0) + b*line_fit.at<float>(3, 0));
line(mc,Point2d(0,-c/b),Point2d(m.cols,(-a*m.cols-c)/b),Scalar(0,255,0),2);

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

Question Tools

1 follower

Stats

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

Seen: 1,074 times

Last updated: Apr 05 '15