Ask Your Question
0

Android + OpenCV - Finding extreme points in contours

asked 2017-03-19 15:08:46 -0600

KicoSVK gravatar image

updated 2017-03-19 15:09:37 -0600

Good Evening,

I have a trouble with finding extreme points in frames. I am detecting all contours, but I want find there one extreme point, which is lowest from others (southernmost contour). Here is a preview of screen, what I have.

image description

And here is a preview of screen, what I want ! For example, lowest point will bounded by red rectangle.

image description.

And here is my code, what I have now.

   contours = new ArrayList<MatOfPoint>();
   Rect rect;
   Point c1 = new Point(); Point c2 = new Point();

   Imgproc.cvtColor(mRgba, mGray, Imgproc.COLOR_RGB2GRAY);
   subtractorMOG2.apply(mGray, mFGMask, 0.005);

   Imgproc.threshold(mFGMask, mFGMask, 45, 255, Imgproc.THRESH_BINARY);
   Imgproc.erode(mFGMask, mFGMask, new Mat());
   Imgproc.dilate(mFGMask, mFGMask, new Mat());

   Imgproc.findContours(mFGMask, contours, new Mat(), Imgproc.RETR_EXTERNAL , Imgproc.CHAIN_APPROX_NONE);
   Imgproc.drawContours(mRgba, contours, -1, new Scalar(255, 255, 0), 3);

        /*for (int idx = 0; idx < contours.size(); idx++) {

            double contourarea = Imgproc.contourArea(contours.get(idx));
            rect = boundingRect(contours.get(idx));

            c1.x = rect.x; c1.y = rect.y;
            c2.x = rect.x + rect.width; c2.y = rect.y + rect.height;
            if (contourarea > 1024 && contourarea < 30000) {
                Imgproc.rectangle(mRgba, c1, c2, new Scalar(255, 0, 0), 3);
            }
        }*/

  return mRgba;

Thanks everybody for help and comments !

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-03-20 03:23:35 -0600

updated 2017-03-20 03:26:59 -0600

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

edit flag offensive delete link more

Comments

That is it, but I have array with MathOfPoints data type, there is a problem. I do not know, how can I get a coordinates.

KicoSVK gravatar imageKicoSVK ( 2017-03-20 04:38:08 -0600 )edit

did not you get a "contours"?

jsxyhelu gravatar imagejsxyhelu ( 2017-03-20 05:28:12 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-03-19 15:08:46 -0600

Seen: 2,822 times

Last updated: Mar 20 '17