1 | initial version |
You follow below method to achieve this.
2 | No.2 Revision |
You can follow below method to achieve this.
3 | No.3 Revision |
You can follow below method to achieve this.
Code:-
#include <iostream>
#include "opencv2/opencv.hpp"
#include <stdio.h>
using namespace std;
using namespace cv;
int main( int argc, char** argv )
{
Mat src=imread("img.png",1);
Mat hsv,thr;
cvtColor(src,hsv,CV_BGR2HSV);
// inRange(hsv,Scalar(160,0,120), Scalar(179,255,200), thr);
inRange(src,Scalar(20,0,120), Scalar(25,0,200), thr);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours( thr, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, 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] = Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 ); }
/// Draw contours
for( int i = 0; i< contours.size(); i++ )
{
circle( src, mc[i], 10, Scalar(0,0,255), 1,CV_AA, 0 );
}
imshow("src",src);
waitKey();
waitKey();
}
Result: