1

My project need to crop the image automatically to remove the white space around the drawing (Lattice).

imgLattice

Here is my code

grayImage = grayImage.ThresholdBinary(new Gray(threshold), new Gray(255));

VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
CvInvoke.FindContours(grayImage, contours, null, RetrType.External, ChainApproxMethod.ChainApproxSimple);

for (int i = 0; i < contours.Size; i++)
{

Rectangle rect = CvInvoke.BoundingRectangle(contours[i]);

    if (rect.Width > minWidth && rect.Height > minHeight)
    {
        CvInvoke.DrawContours(image, contours, i, new MCvScalar(255, 0, 0), 2);
    }

}

imageBox.Image = image;
burnsi
  • 6,194
  • 13
  • 17
  • 27
  • Can you please post the original image, and not a screen capture of the image? Can you please post a code sample we can execute (including using statements, main function, reading the image...)? What is the white space around the Lattice? We can see a gray space with a black frame, and a small white space inside... Please explain: Why do you think your solution is not working? – Rotem Jan 20 '23 at 21:45
  • Hello, sorry for the late answer I was busy with other problems with my app. So for the image, I only have sadly this screenshot of my shape. Talking about the white space around, I want to detect the contour of the shape automaticaly to then crop around to just keep the shape in the image. I think my solution is not working because it can't detect correct the contour of my image it just detect random bordure. I'm new to image processing and new to openCV so its certainely an error of me. Drive with all files https://drive.google.com/drive/folders/1QWv2hl-uLhFbB738Gp67zzd0v__LBW50?usp=sharing – Thomas Herr Feb 10 '23 at 07:34

1 Answers1

1

The main issue is that FindContours finds white contours, and the image background is white.

We use ThresholdBinaryInv instead of ThresholdBinary.
ThresholdBinaryInv applies threshold and invert black and white after applying the threshold (the pattern is going to be white on black instead of black on white).


Code sample:

using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using System.Drawing;

namespace Testings
{
    public class Program
    {
        static void Main(string[] args)
        {
            int threshold = 254;
            var image_file_name = @"auxetic_lattice_screen.png";

            Mat image = new Mat(image_file_name, Emgu.CV.CvEnum.ImreadModes.Color);  //Read input image as BGR

            var grayImage = new Image<Gray, System.Byte>(image_file_name); //Read input image as Grayscale

            //grayImage = grayImage.ThresholdBinary(new Gray(threshold), new Gray(255));
            grayImage = grayImage.ThresholdBinaryInv(new Gray(threshold), new Gray(255)); // Use ThresholdBinaryInv - invert black and white so that the pattern be white

            VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
            CvInvoke.FindContours(grayImage, contours, null, RetrType.External, ChainApproxMethod.ChainApproxSimple);

            int minWidth = grayImage.Width / 2;
            int minHeight = grayImage.Height / 2;
            Mat croppedImage = null;

            for (int i = 0; i < contours.Size; i++)
            {
                Rectangle rect = CvInvoke.BoundingRectangle(contours[i]);

                if (rect.Width > minWidth && rect.Height > minHeight)
                {
                    croppedImage = new Mat(image.Clone(), rect); //Crop the rectangle
                    CvInvoke.DrawContours(image, contours, i, new MCvScalar(255, 0, 0), 2);
                }
            }

            

            //Show images for testing
            CvInvoke.Imshow("grayImage", grayImage);
            CvInvoke.Imshow("image", image);
            CvInvoke.WaitKey();

            CvInvoke.Imwrite("output_image.png", image); //Save output for testing
            CvInvoke.Imwrite("croppedImage.png", croppedImage); //Save output for testing            
        }
    }
}

Result:
enter image description here

Rotem
  • 30,366
  • 4
  • 32
  • 65
  • wow thanks for your help it works perfectly in my app! didnt knew that `FindContours` finds white contours I was struggling to understand the documentation. Thank you for your time have a good day! – Thomas Herr Feb 10 '23 at 10:47