0

I have built a code for Image Binarization and it works kind of well but the texts in my binary images either get too big or there's some white noise in them. What I want to do it to try Erosion, Dilation, Opening, Closing individually and then see which one is improving the results for me. Where should I use these morphological operations in my code. For example In Between Sharpened and binary image or between division and sharpened?

            import numpy as np
            import cv2
            import skimage.filters as filters

            gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
            smooth = cv2.GaussianBlur(gray, (93,93), 0,)
            division = cv2.divide(gray, smooth, scale=255)

            # kernel = np.ones((5,5),np.uint8) # use operations here
            # opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)

            sharp = filters.unsharp_mask(division, radius=1.5, amount=1.5, multichannel=False, preserve_range=False)
            sharp = (255*sharp).clip(0,255).astype(np.uint8)

            # kernel = np.ones((5,5),np.uint8) # or here
            # opening = cv2.morphologyEx(sharp, cv2.MORPH_OPEN, kernel)

            thresh = cv2.threshold(sharp, 0, 255, cv2.THRESH_OTSU )[1]
Deshwal
  • 3,436
  • 4
  • 35
  • 94
  • You can even try them before `smooth`. – Cris Luengo Mar 08 '21 at 14:23
  • @beaker Sorry. My bad. – Deshwal Mar 09 '21 at 06:55
  • @CrisLuengo So there is not a specific place where I'd try? I thought using them with threshold or Even after binary images would work? It's more of a theoretical problem actually. – Deshwal Mar 09 '21 at 06:57
  • 1
    Morphological operators will have different results depending on where they are used. It depends on the actual issues you're trying to fix, where the fixing should happen. This is something that experience teaches. So the best thing to do is to try it out in different places and see what happens to the image. Always look at each of your intermediate images. // You can certainly apply morphology after binarization, but binarization removes a lot of information, it is usually best to make that your last step in processing. – Cris Luengo Mar 09 '21 at 07:10
  • Actually [this is the actual idea](https://stackoverflow.com/questions/63612617/how-do-you-remove-the-unnecessary-blackness-from-binary-images-mainly-caused-by) and [this is my newest implementation and use case](https://stackoverflow.com/questions/66525234/manually-performing-perspective-transformation-on-images-using-opencv-for-differ). Can you help me looking at the use case? Just your experience and what MIGHT work. – Deshwal Mar 09 '21 at 07:14

0 Answers0