Ask Your Question

# 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)
{
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 close merge delete

## Comments

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

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

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.

1

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.

## 1 answer

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

## 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?

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

sorry my program was wrong check previous comment.

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

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

Official site

GitHub

Wiki

Documentation

## Stats

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

Seen: 166 times

Last updated: Aug 07 '18