# 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)? edit retag close merge delete

Sort by » oldest newest most voted You can follow below method to achieve this.

• Segment the four dotted region. Use inRange to segment the dots, better to convert to HSV color space before inRange.
• Use OpenCV Find contour get the contour in image.
• 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 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: more

what if there are no dots how to find the extreme points of an object in an image? topmost, leftmost, rightmost, bottom

more

1

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.

1

Or use contour approach like, find contour->bounding box for contour etc...See the tutorial here

1

Official site

GitHub

Wiki

Documentation