Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Detect all black pixels inside a surrounded closed white area

How can i detect black pixels surrounded by closed white pixels? Background outside shapes should not be considered.

See next two images: First image i would like to extract all black pixels inside the hallow shape because it's traped/surrounded by white, but image 2 have a opeing and in that case i don't need the pixels.

close open

Detect all black pixels inside a surrounded closed white area

How can i detect black pixels surrounded by closed white pixels? Background outside shapes should not be considered.

See next two images: First image i would like to extract all black pixels inside the hallow shape because it's traped/surrounded by white, but image 2 have a opeing and in that case i don't need the pixels.pixels. Image 3 shows the desired capture area in red

close open

Image 3: Desired capture area from image1 in red capture area

Detect all black pixels inside a surrounded closed white area

How can i detect black pixels surrounded by closed white pixels? Background outside shapes should not be considered.

See next two images: First image i would like to extract all black pixels inside the hallow shape because it's traped/surrounded by white, but image 2 have a opeing and in that case i don't need the pixels. Image 3 shows the desired capture area in red

close open

Image 3: Desired capture area from image1 in red Same pass on image 2 should return 0 pixels (No area detected) capture area

Detect all black pixels inside a surrounded closed white area

How can i detect black pixels surrounded by closed white pixels? Background outside shapes should not be considered.

See next two images: First image i would like to extract all black pixels inside the hallow shape because it's traped/surrounded by white, but image 2 have a opeing and in that case i don't need the pixels. Image 3 shows the desired capture area in red

close open

Image 3: Desired capture area from image1 in red red. Same pass on image 2 should return 0 pixels (No area detected) capture area

Detect all black pixels inside a surrounded closed white area

How can i detect black pixels surrounded by closed white pixels? Background outside shapes should not be considered.

See next two images: First image i would like to extract all black pixels inside the hallow shape because it's traped/surrounded by white, but image 2 have a opeing and in that case i don't need the pixels. Image 3 shows the desired capture area in red

close open

Image 3: Desired capture area from image1 in red. Same pass on image 2 should return 0 pixels (No area detected) capture area

Another example:

Input: ex2_input

Needed area in red: ex2_output

Detect all black pixels inside a surrounded closed white area

How can i detect black pixels surrounded by closed white pixels? Background outside shapes should not be considered.

See next two images: First image i would like to extract all black pixels inside the hallow shape because it's traped/surrounded by white, but image 2 have a opeing and in that case i don't need the pixels. Image 3 shows the desired capture area in red

close open

Image 3: Desired capture area from image1 in red. Same pass on image 2 should return 0 pixels (No area detected) capture area

Another example:

Input: ex2_input

Needed area in red: ex2_outputex2_output

Detect all black pixels inside a surrounded closed white area

How can i detect black pixels surrounded by closed white pixels? Background outside shapes should not be considered.

See next two images: First image i would like to extract all black pixels inside the hallow shape because it's traped/surrounded by white, but image 2 have a opeing and in that case i don't need the pixels. Image 3 shows the desired capture area in red

close open

Image 3: Desired capture area from image1 in red. Same pass on image 2 should return 0 pixels (No area detected) capture area

Another example:

Input: ex2_inputex2_input

Needed area in red: ex2_outputex2_output

Detect all black pixels inside a surrounded closed white area

How can i detect black pixels surrounded by closed white pixels? Background outside shapes should not be considered.

See next two images: First image i would like to extract all black pixels inside the hallow shape because it's traped/surrounded by white, but image 2 have a opeing and in that case i don't need the pixels. Image 3 shows the desired capture area in red

close open

Image 3: Desired capture area from image1 in red. Same pass on image 2 should return 0 pixels (No area detected) capture area

Another example:

Input: ex2_input

Needed area in red: ex2_outputex2_output

Detect all black pixels inside a surrounded closed white area

How can i detect black pixels surrounded by closed white pixels? Background outside shapes should not be considered.

See next two images: First image i would like to extract all black pixels inside the hallow shape because it's traped/surrounded by white, but image 2 have a opeing and in that case i don't need the pixels. Image 3 shows the desired capture area in red

close open

Image 3: Desired capture area from image1 in red. Same pass on image 2 should return 0 pixels (No area detected) capture area

Another example:

Input: ex2_input

Needed area in red: ex2_output

EDIT 1:

VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
                Mat external = new Mat();

                CvInvoke.FindContours(grayscale, contours, external, RetrType.Ccomp, ChainApproxMethod.ChainApproxSimple);

                var arr = external.GetData();
                for (int i = 0; i < contours.Size; i++)
                {
                    if((int)arr.GetValue(0, i, 2) != -1) continue;
                    var r = CvInvoke.BoundingRectangle(contours[i]);
                    CvInvoke.Rectangle(grayscale, r, new MCvScalar(125), 10);
                }

Code on different inputs:

Image 4: OK ex3

Image 5: OK ex4

Image 6: Not OK! ex4

How to discard image6 cases?

Detect all black pixels inside a surrounded closed white area

How can i detect black pixels surrounded by closed white pixels? Background outside shapes should not be considered.

See next two images: First image i would like to extract all black pixels inside the hallow shape because it's traped/surrounded by white, but image 2 have a opeing and in that case i don't need the pixels. Image 3 shows the desired capture area in red

close open

Image 3: Desired capture area from image1 in red. Same pass on image 2 should return 0 pixels (No area detected) capture area

Another example:

Input: ex2_input

Needed area in red: ex2_output

EDIT 1:

                Image<Gray, byte> grayscale = ActualLayerImage.ToEmguImage();
                VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
                Mat external = new Mat();

                CvInvoke.FindContours(grayscale, contours, external, RetrType.Ccomp, ChainApproxMethod.ChainApproxSimple);

                var arr = external.GetData();
                for (int i = 0; i < contours.Size; i++)
                {
                    if((int)arr.GetValue(0, i, 2) != -1) continue;
                    var r = CvInvoke.BoundingRectangle(contours[i]);
                    CvInvoke.Rectangle(grayscale, r, new MCvScalar(125), 10);
                }

Code on different inputs:

Image 4: OK ex3

Image 5: OK ex4

Image 6: Not OK! ex4

How to discard image6 cases?

Detect all black pixels inside a surrounded closed white area

How can i detect black pixels surrounded by closed white pixels? Background outside shapes should not be considered.

See next two images: First image i would like to extract all black pixels inside the hallow shape because it's traped/surrounded by white, but image 2 have a opeing and in that case i don't need the pixels. Image 3 shows the desired capture area in red

close open

Image 3: Desired capture area from image1 in red. Same pass on image 2 should return 0 pixels (No area detected) capture area

Another example:

Input: ex2_input

Needed area in red: ex2_output

EDIT 1:

                Image<Gray, byte> grayscale = ActualLayerImage.ToEmguImage();
                VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
                Mat external = new Mat();

                CvInvoke.FindContours(grayscale, contours, external, RetrType.Ccomp, ChainApproxMethod.ChainApproxSimple);

                var arr = external.GetData();
                for (int i = 0; i < contours.Size; i++)
                {
                    if((int)arr.GetValue(0, i, 2) != -1) continue;
                    var r = CvInvoke.BoundingRectangle(contours[i]);
                    CvInvoke.Rectangle(grayscale, r, new MCvScalar(125), 10);
                }

Code on different inputs:

Image 4: OK ex3

Image 5: OK ex4

Image 6: Not OK! ex4

How to discard image6 cases?

EDIT 2: The solution:

                 Image<Gray, byte> grayscale = ActualLayerImage.ToEmguImage().ThresholdBinary(new Gray(127), new Gray(255) );

                VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
                Mat external = new Mat();

                CvInvoke.FindContours(grayscale, contours, external, RetrType.Ccomp, ChainApproxMethod.ChainApproxSimple);

                var arr = external.GetData();

                /*
                 * hierarchy[i][0]: the index of the next contour of the same level
                 * hierarchy[i][1]: the index of the previous contour of the same level
                 * hierarchy[i][2]: the index of the first child
                 * hierarchy[i][3]: the index of the parent
                 */
                for (int i = 0; i < contours.Size; i++)
                {
                    if ((int)arr.GetValue(0, i, 2) != -1 || (int)arr.GetValue(0, i, 3) == -1) continue;
                    var r = CvInvoke.BoundingRectangle(contours[i]);
                    CvInvoke.Rectangle(grayscale, r, new MCvScalar(125), 5);
                }

Detect all black pixels inside a surrounded closed white area

How can i detect black pixels surrounded by closed white pixels? Background outside shapes should not be considered.

See next two images: First image i would like to extract all black pixels inside the hallow shape because it's traped/surrounded by white, but image 2 have a opeing and in that case i don't need the pixels. Image 3 shows the desired capture area in red

close open

Image 3: Desired capture area from image1 in red. Same pass on image 2 should return 0 pixels (No area detected) capture area

Another example:

Input: ex2_input

Needed area in red: ex2_output

EDIT 1:

                Image<Gray, byte> grayscale = ActualLayerImage.ToEmguImage();
                VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
                Mat external = new Mat();

                CvInvoke.FindContours(grayscale, contours, external, RetrType.Ccomp, ChainApproxMethod.ChainApproxSimple);

                var arr = external.GetData();
                for (int i = 0; i < contours.Size; i++)
                {
                    if((int)arr.GetValue(0, i, 2) != -1) continue;
                    var r = CvInvoke.BoundingRectangle(contours[i]);
                    CvInvoke.Rectangle(grayscale, r, new MCvScalar(125), 10);
                }

Code on different inputs:

Image 4: OK ex3

Image 5: OK ex4

Image 6: Not OK! ex4

How to discard image6 cases?

EDIT 2: The solution:

                 Image<Gray, byte> grayscale = ActualLayerImage.ToEmguImage().ThresholdBinary(new Gray(127), new Gray(255) );
ActualLayerImage.ToEmguImage();

                VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
                Mat external = new Mat();

                CvInvoke.FindContours(grayscale, contours, external, RetrType.Ccomp, ChainApproxMethod.ChainApproxSimple);

                var arr = external.GetData();

                /*
                 * hierarchy[i][0]: the index of the next contour of the same level
                 * hierarchy[i][1]: the index of the previous contour of the same level
                 * hierarchy[i][2]: the index of the first child
                 * hierarchy[i][3]: the index of the parent
                 */
                for (int i = 0; i < contours.Size; i++)
                {
                    if ((int)arr.GetValue(0, i, 2) != -1 || (int)arr.GetValue(0, i, 3) == -1) continue;
                    var r = CvInvoke.BoundingRectangle(contours[i]);
                    CvInvoke.Rectangle(grayscale, r, new MCvScalar(125), 5);
                }

Detect all black pixels inside a surrounded closed white area

How can i detect black pixels surrounded by closed white pixels? Background outside shapes should not be considered.

See next two images: First image i would like to extract all black pixels inside the hallow shape because it's traped/surrounded by white, but image 2 have a opeing and in that case i don't need the pixels. Image 3 shows the desired capture area in red

close open

Image 3: Desired capture area from image1 in red. Same pass on image 2 should return 0 pixels (No area detected) capture area

Another example:

Input: ex2_input

Needed area in red: ex2_output

EDIT 1:

                Image<Gray, byte> grayscale = ActualLayerImage.ToEmguImage();
                VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
                Mat external = new Mat();

                CvInvoke.FindContours(grayscale, contours, external, RetrType.Ccomp, ChainApproxMethod.ChainApproxSimple);

                var arr = external.GetData();
                for (int i = 0; i < contours.Size; i++)
                {
                    if((int)arr.GetValue(0, i, 2) != -1) continue;
                    var r = CvInvoke.BoundingRectangle(contours[i]);
                    CvInvoke.Rectangle(grayscale, r, new MCvScalar(125), 10);
                }

Code on different inputs:

Image 4: OK ex3

Image 5: OK ex4

Image 6: Not OK! ex4

How to discard image6 cases?

EDIT 2: The solution:

                 Image<Gray, byte> grayscale = ActualLayerImage.ToEmguImage();

                VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
                Mat external = new Mat();

                CvInvoke.FindContours(grayscale, contours, external, RetrType.Ccomp, ChainApproxMethod.ChainApproxSimple);

                var arr = external.GetData();

                /*
                 * hierarchy[i][0]: the index of the next contour of the same level
                 * hierarchy[i][1]: the index of the previous contour of the same level
                 * hierarchy[i][2]: the index of the first child
                 * hierarchy[i][3]: the index of the parent
                 */
                for (int i = 0; i < contours.Size; i++)
                {
                    if ((int)arr.GetValue(0, i, 2) != -1 || (int)arr.GetValue(0, i, 3) == -1) continue;
                    var r = CvInvoke.BoundingRectangle(contours[i]);
                    CvInvoke.Rectangle(grayscale, r, new MCvScalar(125), 5);
                }