How do I find a distance between the contour pixels of an irregular image and the center of mass of that 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)
{

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 close merge delete

Image, do you mean shape?

( 2018-08-05 11:48:25 -0500 )edit

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

( 2018-08-05 11:51:19 -0500 )edit

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

( 2018-08-05 12:02:10 -0500 )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.

( 2018-08-05 12:04:29 -0500 )edit
1

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

( 2018-08-05 12:09:32 -0500 )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.

( 2018-08-06 08:03:21 -0500 )edit

Sort by ยป oldest newest most voted

@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

more

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;
}


( 2018-08-07 20:17:26 -0500 )edit

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

( 2018-08-07 20:34:44 -0500 )edit

sorry my program was wrong check previous comment.

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

( 2018-08-08 02:50:06 -0500 )edit

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

( 2018-08-08 08:00:58 -0500 )edit