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;
}
Image, do you mean shape?
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.
C++ Tutorials are here and you can find many c++ examples here tooand https://docs.opencv.org/trunk/example...
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.