How to store points cordinats

asked 2019-02-25 04:46:49 -0500

krpa gravatar image


I'm new in openCV and c++ and I am stuck at one problem.

For now I would like to make program which will store centerpoints coordinates (x,y) of white blob and then after that draw straight line through them.

This code will calculate center of every white segment per column (po)

for (j = 0; j < slikica.cols; j++) {

    for (i = 0; i < slikica.rows; i++) {

        if (<uchar>(i,j) == 255) {
            tc = tc + i;
            stup = stup + 1;
            po = tc / stup;

            //HERE I NEED TO STORE  POINT (po,j)//

br = br + 1;


This is my picture and red line is what i'm looking for. picture


edit retag flag offensive close merge delete


How about a

vector<Point> myPoints;
gino0717 gravatar imagegino0717 ( 2019-02-25 20:56:04 -0500 )edit

Thanks, I managed to solve that yesterday but now the next problem is how to fitline? I'm trying something like this ...

fitLine(myPoints,line, CV_DIST_L2, 0, 0.01, 0.01);
krpa gravatar imagekrpa ( 2019-02-26 04:01:12 -0500 )edit

That should work. What is your problem now?

Grillteller gravatar imageGrillteller ( 2019-02-26 04:16:46 -0500 )edit

The thing is I don't know how to draw that line form start of the blob to the end. I'm using line function to draw it.

"Output line parameters. In case of 2D fitting, it should be a vector of 4 elements (like Vec4f) - (vx, vy, x0, y0), where (vx, vy) is a normalized vector collinear to the line and (x0, y0) is a point on the line. In case of 3D fitting, it should be a vector of 6 elements (like Vec6f) - (vx, vy, vz, x0, y0, z0), where (vx, vy, vz) is a normalized vector collinear to the line and (x0, y0, z0) is a point on the line"

    line(src, Point(??), Point(??), Scalar(140, 255, 0), 1, LINE_8, 0);
krpa gravatar imagekrpa ( 2019-02-26 06:01:30 -0500 )edit

Ok, you found your line as a ´cv::Vec4f´ where you have a starting point (x0, y0) and a vector collinear to the line (vx, vy). Now you can draw your line with (simple line equation):

cv::line(img, cv::Point(x0-m*vx[0], y0-m*vy[0]), cv::Point(x0+m*vx[0], y0+m*vy[0])), 1, LINE_8, 0)

Choose an appropriate "m".

Grillteller gravatar imageGrillteller ( 2019-02-26 07:59:17 -0500 )edit

maybe I don't use your code correctly but what I see "m" is parameter which defines length. My goal is to automatically draw line form the first white column to the last one, like I showed on img.

P.S but yes your code works fine for drawing.

krpa gravatar imagekrpa ( 2019-02-26 09:48:42 -0500 )edit

Ok, my bad. So the actual problem is finding the start and end point of the fitted line? If so, you could use findContours to get a contour line around your points. Then you look for the intersection points of the contour (stored as vector<point>) and your line.

Grillteller gravatar imageGrillteller ( 2019-02-27 02:27:01 -0500 )edit