0

I'm trying to get an entire dark "D" from this image :

enter image description here

With this code, I get this result :

```

        Cv2.Threshold(im_gray, threshImage, 80, 255, ThresholdTypes.BinaryInv); // Threshold to find contour

        Cv2.FindContours(
            threshImage,
            out contours,
            out hierarchyIndexes,
            mode: RetrievalModes.Tree,
            method: ContourApproximationModes.ApproxSimple
        );

        double largest_area = 0;
        int largest_contour_index = 0;
        Rect rect = new Rect();
        //Search biggest contour
        for (int i = 0; i <contours.Length; i++)
        {
            double area = Cv2.ContourArea(contours[i]);  //  Find the area of contour

            if (area > largest_area)
            {
                largest_area = area;
                largest_contour_index = i;               //Store the index of largest contour
                rect = Cv2.BoundingRect(contours[i]); // Find the bounding rectangle for biggest contour
            }
        }

        Cv2.DrawContours(finalImage, contours, largest_contour_index, new Scalar(0, 0, 0), -1, LineTypes.Link8, hierarchyIndexes, int.MaxValue);

```

But when I save finalImage, I get the result below : enter image description here How can I get entire black letter?

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
Portekoi
  • 1,087
  • 2
  • 22
  • 44

1 Answers1

3

Apart from my comment, I tried out another way. I used the statistical property of the gray scale image.

For this image I used the mean value of the image to select an optimal threshold value.

The optimal value is selected by choosing all pixel values under 33% of the mean value

med = np.mean(gray)
optimal = int(med * 1.33) #selection of the optimal threshold

ret,th = cv2.threshold(gray, optimal, 255, 0)

This is what I got as a result:

enter image description here

Now invert this image and obtain the largest contour and there you go you will have the Big D

EDIT:

For some images with drastic histogram, you can use the median of the gray scale image instead of the mean.

Community
  • 1
  • 1
Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
  • Do you know the equivalent of "numpy" in C# (OpenCvSharp) ? – Portekoi Feb 21 '17 at 16:44
  • 1
    I'm really sorry. I have never worked with C# in OpenCV. My game is OpenCV with python. If I do come across any function I will let you know. :D – Jeru Luke Feb 21 '17 at 16:51
  • 1
    [Visit this](http://stackoverflow.com/questions/15976925/is-there-a-c-sharp-library-that-provides-array-manipulation-like-numpy) LINQ seems to be the option – Jeru Luke Feb 21 '17 at 16:56