Detecting document in scanned image OpenCV

I am using OpenCV for image preprocessing. I only need to crop the scanned photo, no white area. I am using the algorithm:

  • image_canny <- apply the canny edge detector to this channel.
  • for the threshold in bunch_of_increasing_thresholds:
  • image_thresholds [threshold] <- apply a threshold to this channel for each contour found in {image_canny} U image_thresholds:
  • Approximate outline with polygons
  • if the approximation has four corners and the angles are close to 90 degrees. To search for a rectangle object in the scanned image. But this example doesn't work if I place my photo in the corner of the scanner!

Can anyone please advise how can I find this photo in the scanned image? any examples, methods?

+3


source to share


1 answer


There are several ways to achieve your goal. I'll give the code for OpenCvSharp, it will look like normal C ++.

  • Try adding a neutral border around your image. For example, you can just add 10-20 pixels of white around the original image. It can create false paths, but your target portion of the image will no longer be in the corner.

    Mat img = Cv2.ImRead("test.jpg");
    Mat imgExtended = new Mat(new OpenCvSharp.Size(img.Size().Width + 40, img.Size().Height + 40), MatType.CV_8UC3, Scalar.White);
    OpenCvSharp.Rect roi = new OpenCvSharp.Rect(new OpenCvSharp.Point(20, 20), img.Size());
    img.CopyTo(imgExtended.SubMat(roi));
    img = imgExtended;
    Mat coloredImage = img.Clone();
    Cv2.CvtColor(img, img, ColorConversionCodes.BGR2GRAY);
    OpenCvSharp.Point[][] contours;
    HierarchyIndex[] hierarchy;
    Cv2.Canny(img, result, 80, 150);
    Cv2.FindContours(result, out contours, out hierarchy, RetrievalModes.External, ContourApproximationModes.ApproxSimple);
    
          

  • You have an object and an almost white background. You can perform any thresholding operation and then take the largest blob.

Refresh. In both cases, the dark line at the top of the image and the dark area in the left corner can still be a problem. In this case, you can select the contour with the largest area using the function



double Cv2.ContourArea(Point[] Contour);

      

And then try to create a bounding box that minimizes the error.

0


source







All Articles