0

I have generated the binary version of an image where the edges of the image are in white and the rest of the image is in black. I used the Canny Edge filter built into Magick.NET and saved the image. Now I need to detect a rectangle on that image which is clearly outlined on the image. I have installed Emgu CV and Emgu CV runtime and used the code below but it's not working , help me make the code below detect the rectangle and returns the rectangle points of a rectangular contour.

using Emgu.CV;

using Emgu.CV.CvEnum;

using Emgu.CV.Structure;

using Emgu.CV.Util;

using ImageMagick;


MagickImage image = new MagickImage("original.jpg");
//apply the canny edge filter
image.CannyEdge();
//Save the binary image to disk
image.Write("canny_edge_image.jpg");

// Load the Canny edge image

Image<Gray, byte> cannyImage = new Image<Gray, byte>("canny_edge_image.jpg");

// Find contours in the image

VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();

CvInvoke.FindContours(cannyImage, contours, null, RetrType.List, ChainApproxMethod.ChainApproxSimple);

// Iterate through the contours

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

{

    // Approximate the contour to a polygon

    VectorOfPoint approx = new VectorOfPoint();

    CvInvoke.ApproxPolyDP(contours[i], approx, CvInvoke.ArcLength(contours[i], true) * 0.02, true);

    // Check if the polygon has four points (i.e. it's a rectangle)

    if (approx.Size == 4)

    {

        // Draw the rectangle

        CvInvoke.DrawContours(cannyImage, contours, i, new MCvScalar(255, 0, 0), 2);

        // You can also access the points of the rectangle using:

        Point[] points = approx.ToArray();

        // Use points[0], points[1], points[2], and points[3] to access the four corner points of the rectangle

    }

}

// Display or save the image with the rectangle drawn on it

CvInvoke.Imshow("Rectangle", cannyImage);

CvInvoke.WaitKey(0);

0 Answers0