Ask Your Question

Revision history [back]

Hi Here is a Sample Implementation using c++:

#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

bool SortbyXaxis(const Point & a, const Point &b) 
{
    return a.x > b.x;
}
bool SortbyYaxis(const Point & a, const Point &b) 
{
    return a.y > b.y;
}

int main (int argc, char * const argv[]) 
{
    Mat mSrc,mDst;
    mSrc = imread("Sample.png", 0);
    if (mSrc.empty())
    {
        cerr << "No image supplied ..." << endl;
        return -1;
    }

    cvtColor(mSrc,mDst,COLOR_GRAY2BGR);


    /// Create Window
    const char* source_window = "Output Image";
    namedWindow( source_window, WINDOW_AUTOSIZE );

image description

    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    /// Find contours
    findContours( mSrc.clone(), contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0) );

    vector<Point> ptsContour;
    for( size_t i = 0; i< contours.size(); i++ )
    {
        drawContours( mDst, contours, (int)i, Scalar(0,255,0), 1);

        ptsContour= contours[i];

        sort( ptsContour.begin(), ptsContour.end(), SortbyYaxis );
        sort( ptsContour.begin(), ptsContour.end(), SortbyXaxis );

        circle(mDst, ptsContour[0],3,Scalar(0,255,255),-1);
    }

    imshow( source_window, mDst );

image description

    waitKey();

    return 0;
}

I'm sure that you can find similar in java like this

Hi You could sort the vectot<points> contours descending order to find the Min & max elements. Here is a Sample Implementation using c++:

#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

bool SortbyXaxis(const Point & a, const Point &b) 
{
    return a.x > b.x;
}
bool SortbyYaxis(const Point & a, const Point &b) 
{
    return a.y > b.y;
}

int main (int argc, char * const argv[]) 
{
    Mat mSrc,mDst;
    mSrc = imread("Sample.png", 0);
    if (mSrc.empty())
    {
        cerr << "No image supplied ..." << endl;
        return -1;
    }

    cvtColor(mSrc,mDst,COLOR_GRAY2BGR);


    /// Create Window
    const char* source_window = "Output Image";
    namedWindow( source_window, WINDOW_AUTOSIZE );

image description

    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    /// Find contours
    findContours( mSrc.clone(), contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0) );

    vector<Point> ptsContour;
    for( size_t i = 0; i< contours.size(); i++ )
    {
        drawContours( mDst, contours, (int)i, Scalar(0,255,0), 1);

        ptsContour= contours[i];

        sort( ptsContour.begin(), ptsContour.end(), SortbyYaxis );
        sort( ptsContour.begin(), ptsContour.end(), SortbyXaxis );

        circle(mDst, ptsContour[0],3,Scalar(0,255,255),-1);
    }

    imshow( source_window, mDst );

image description

    waitKey();

    return 0;
}

I'm sure that you can find similar in java like this

Hi You could sort the vectot<points> contours in descending order to find the Min & max elements. Here is a Sample Implementation using c++:

#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

bool SortbyXaxis(const Point & a, const Point &b) 
{
    return a.x > b.x;
}
bool SortbyYaxis(const Point & a, const Point &b) 
{
    return a.y > b.y;
}

int main (int argc, char * const argv[]) 
{
    Mat mSrc,mDst;
    mSrc = imread("Sample.png", 0);
    if (mSrc.empty())
    {
        cerr << "No image supplied ..." << endl;
        return -1;
    }

    cvtColor(mSrc,mDst,COLOR_GRAY2BGR);


    /// Create Window
    const char* source_window = "Output Image";
    namedWindow( source_window, WINDOW_AUTOSIZE );

image description

    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    /// Find contours
    findContours( mSrc.clone(), contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0) );

    vector<Point> ptsContour;
    for( size_t i = 0; i< contours.size(); i++ )
    {
        drawContours( mDst, contours, (int)i, Scalar(0,255,0), 1);

        ptsContour= contours[i];

        sort( ptsContour.begin(), ptsContour.end(), SortbyYaxis );
        sort( ptsContour.begin(), ptsContour.end(), SortbyXaxis );

        circle(mDst, ptsContour[0],3,Scalar(0,255,255),-1);
    }

    imshow( source_window, mDst );

image description

    waitKey();

    return 0;
}

I'm sure that you can find similar in java like this