how to find the coordinate of top/bottom/left and right points
how do I find the coordinate of these 4 points in a detected object(which does not have any specific shape)?
You can follow below method to achieve this.
Use OpenCV moments to find the centre point of your contour
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:
what if there are no dots how to find the extreme points of an object in an image? topmost, leftmost, rightmost, bottom
Then you should create a black-white mask of your retrieved object. Loop over the image, at each white pixel, see if the current pixel has a value that is better then a previous found candidate for topmost leftmost rightmost or bottom pixel. If so replace.
Asked: 2013-12-09 06:39:08 -0600
Seen: 11,636 times
Last updated: Oct 07 '14