Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Get all points inside a rectangle using opencv findcontour method

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);

Get all points inside a rectangle using opencv findcontour method

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 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);

Get all points inside a rectangle using opencv findcontour method

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>(); ArrayList<matofpoint>();

Imgproc.findContours(mCanny, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

click to hide/show revision 4
changed tags and formated source code

Get all points inside a rectangle using opencv findcontour method

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

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

CvType.8UC3); // create a canny matrix

matrix Mat mCanny = new Mat();

Mat(); // Make it to sizeof srcMatrix and // make channel to 1

1 mCanny.create( srcMat.cols(), srcMat.rows, CvType.8UC1)

// clear it

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

Scalar(0)); // draw a border rectangle on the mcanny

mcanny Core.rectangle(mCanny, new Point(0,0), new Point(100, 100), new Scalar(255));

Scalar(255)); // draw a rectangle

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

Scalar(255)); // make it binary image

image Imgproc.cvtColor(mCanny , mCanny , Imgproc.COLOR_RGB2GRAY);

Imgproc.COLOR_RGB2GRAY); // make it blur

blur Imgproc.blur( mCanny, mCanny, new Size(3,3));

Size(3,3)); //find canny

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

/// find Contour

List<matofpoint> Contour List<MatOfPoint> contours = new ArrayList<matofpoint>();

ArrayList<MatOfPoint>(); Imgproc.findContours(mCanny, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

Imgproc.CHAIN_APPROX_SIMPLE);