find all regions in bitmap image (Whole White color) where lines cross each other and create regions.

asked 2013-09-17 05:34:01 -0500

rat gravatar image

updated 2013-09-17 07:24:10 -0500

Create a white color bitmap. Draw a lot black lines on the bitmap. Use find contour to find all regions in the bitmap. Color it using different color. How can we do this ?

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

// create a tmp matrix
Mat tmpMat = new Mat();

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

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

// draw somany lines on the tmpMat
core.Line(tmpMat .... );
core.Line(tmpMat .....
core.Line(tmpMat .....
core.Line(tmpMat .....

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

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

//find canny
Mat mCanny = tmpMat.clone();
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);

// draw contour

Here i want to detect all regions seperated by lines. When i use drawcontour, it detects not all regions...only some.

edit retag flag offensive close merge delete


Start by removing the blurring and canny edge step. I have no idea why you would use that if you already have a binary image containing all regions perfectly. Use findContours to select all of these regions. The result will be lists of points surrounding your elements. Now you need to create a function that fills your contour, like this one. You guys are almost there...

StevenPuttemans gravatar imageStevenPuttemans ( 2013-09-17 07:27:51 -0500 )edit