Ask Your Question
0

How do I find a distance between the contour pixels of an irregular image and the center of mass of that image?

asked 2018-08-05 10:40:19 -0600

ggrayce gravatar image

updated 2018-08-06 10:53:27 -0600

LBerger gravatar image

I have a binarized image by Otsu Threshold. I'm new in C++ and Opencv. I try this, but don't work.

      #include <iostream>
    #include "opencv2/imgproc/imgproc.hpp"
   #include "opencv2/highgui/highgui.hpp"
   #include <stdlib.h>
   #include <stdio.h>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    cv::Mat img = cv::imread("melanoma_abertura_cortada_NAOINVERT.png");

    if (img.empty())
    {
        std::cout << "!!! imread() failed to open target image" << std::endl;
        return -1;
    }
    cv::Mat img_canny;

    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    Canny(img, img_canny, 80, 20);


    findContours(img_canny, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_NONE, Point(0, 0));

// Get the moments
    vector<Moments> mu(contours.size());
    for (int i = 0; i < contours.size(); i++)
    {
        mu[i] = moments(contours[i], false);
    }

    ///  Get the mass centers:
    vector<Point2f> mc(contours.size());
    for (int i = 0; i < contours.size(); i++)
    {
        mc[i] = Point2d(mu[i].m10 / mu[i].m00, mu[i].m01 / mu[i].m00);


    }

    for (size_t cC = 0; cC < contours.size(); ++cC)  
        for (size_t cP = 0; cP < contours[cC].size(); cP++)
        {
            Point currentContourPixel = contours[cC][cP];
        }

        Mat  magnitudee;

        magnitude(mc, contours.size(), magnitudee);

    cv::waitKey(0);
    return 0;
}
edit retag flag offensive close merge delete

Comments

Image, do you mean shape?

LBerger gravatar imageLBerger ( 2018-08-05 11:48:25 -0600 )edit

In the case, it would just be the outline of an irregular shape. Thanks.

ggrayce gravatar imageggrayce ( 2018-08-05 11:51:19 -0600 )edit

in Opencv a contour is a vector<point> : calculate distance between gravity center and contour point using a loop

LBerger gravatar imageLBerger ( 2018-08-05 12:02:10 -0600 )edit

How would I do that? I'm starting in C ++ and I'm having difficulty creating my own program. If you could help me by showing an example, I would greatly appreciate it.

ggrayce gravatar imageggrayce ( 2018-08-05 12:04:29 -0600 )edit
1

C++ Tutorials are here and you can find many c++ examples here tooand https://docs.opencv.org/trunk/example...

LBerger gravatar imageLBerger ( 2018-08-05 12:09:32 -0600 )edit

Basically create a for loop. Your iterator will go through the contours, which is a vector of points. At each position, calculate the L2 distance between the existing point and the gravity point using norm(). That info and some googling should get you quite far.

StevenPuttemans gravatar imageStevenPuttemans ( 2018-08-06 08:03:21 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-08-06 10:59:48 -0600

LBerger gravatar image

updated 2018-08-08 02:49:45 -0600

@ggrayce try something like this :

for (int i = 0; i < contours.size(); i++)
{
    mu[i] = moments(contours[i], false);
    Point g(mu.m10/mu.m00,mu01/mu.m00);
    double dMin=1e8;
    for (size_t cP = 0; cP < contours[i].size(); cP++)
    {
        // NO            double d=norm(mu[i],contours[i][cP]);
         double d=norm(g,contours[i][cP]);
        if (d<dMin)
             dMin=d;
    }
}

dMin should be minimum distance between contour and gravity center

edit flag offensive delete link more

Comments

I wuld replace this part:

for (size_t cC = 0; cC < contours.size(); ++cC)  
        for (size_t cP = 0; cP < contours[cC].size(); cP++)
        {
            Point currentContourPixel = contours[cC][cP];
        }
    Mat  magnitudee;

    magnitude(mc, contours.size(), magnitudee);

cv::waitKey(0);
return 0;
}

for your response?

ggrayce gravatar imageggrayce ( 2018-08-07 20:17:26 -0600 )edit

If yes, when i try shows an erro in oparetor "norm" and yours arguments.

ggrayce gravatar imageggrayce ( 2018-08-07 20:34:44 -0600 )edit

sorry my program was wrong check previous comment.

magnitude you would get distance from origin. first and second parameter must have same size

LBerger gravatar imageLBerger ( 2018-08-08 02:50:06 -0600 )edit

Shows an erro when i compile: OpenCV Error: Assertion failed (src1.size() == src2.size() && type == src2.type() && (depth == CV_32F || depth == CV_64F))

ggrayce gravatar imageggrayce ( 2018-08-08 08:00:58 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-08-05 10:40:19 -0600

Seen: 1,155 times

Last updated: Aug 07 '18