Ask Your Question
0

Get all points inside a rectangle using opencv findcontour method

asked 2013-09-15 08:28:32 -0600

rat gravatar image

updated 2013-09-16 03:04:59 -0600

Siegfried gravatar image

I want to find all points in the rectangle. But when i use findContour method in opencv, it actually finds points in the lines drawn ( Not points inside the rectangle). I verified using drawcontour method ( even if we use thickness as -1, it draws lines ).

How can i find points inside the smaller rectangle using findContour method ?

//srcMatrix create
Mat srcMat = new Mat();
srcMat.create(100, 100, CvType.8UC3);

// create a canny matrix
Mat mCanny = new Mat();

// Make it to sizeof srcMatrix and 
// make channel to 1
mCanny.create( srcMat.cols(), srcMat.rows, CvType.8UC1) 

// clear it
mCanny.setTo(new Scalar(0));

// draw a border   rectangle on the mcanny
Core.rectangle(mCanny, 
        new Point(0,0),
            new Point(100, 100),
            new Scalar(255));

// draw a rectangle
Core.rectangle(mCanny, 
        new Point(25,25),
            new Point(50, 50),
            new Scalar(255));

// make it binary image
Imgproc.cvtColor(mCanny , mCanny , Imgproc.COLOR_RGB2GRAY);

// make it blur
Imgproc.blur( mCanny, mCanny, new Size(3,3));

//find canny
Imgproc.Canny(mCanny ,mCanny, 2,4); 

/// find Contour
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(mCanny,
            contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
edit retag flag offensive close merge delete

Comments

I have a bitmap of whitecolor. I have drawn many criss cross lines. All lines are straight and will touch boundaries. These line caused whole plane to split into many regions. I want to detect in which mouse clicked ?

I was amazed to see findcountour method is not able to find some contours. Why is it so ? is there any good way ?

rat gravatar imagerat ( 2013-09-16 09:58:59 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-09-16 05:47:52 -0600

Siegfried gravatar image

Hi, with findContours you can detect the contours in your image. From the documentation "Each contour is stored as a vector of points". If you want the bounding box of the detected contour you can use the function cv.boundingRect(). An other option is to draw the contour into the image with drawContours. With the parameter CV_FILLED the enclosed image area of the contour will be filled with a user defined color.

Are you sure that the drawing of the border works? The Point(100,100) lies outside the image range. Image coordinates start by 0, so your maximum row and column index is 99.

edit flag offensive delete link more

Comments

thanks a lot. it works

rat gravatar imagerat ( 2013-09-16 09:59:50 -0600 )edit

Feel free to accept his answer, so this topic shows solved.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-09-17 02:34:49 -0600 )edit

Question Tools

Stats

Asked: 2013-09-15 08:28:32 -0600

Seen: 6,690 times

Last updated: Sep 16 '13