alyssaeliyah
alyssaeliyah

Reputation: 2244

Algorithm that Fills the White Holes Within the Character Image

I have this degraded image :

enter image description here

Using paintbucket tool in Paint.NET Windows application, I fill the character image black.

enter image description here

My questions are :

  1. What algorithm can we use to fill up the holes within the character image?
  2. Is there any corresponding built-in function in Python that fills up the white holes in a character image?
  3. What probable algorithm does the paint bucket tool in Paint.Net uses ?

Thanks.

Upvotes: 2

Views: 656

Answers (3)

user12228709
user12228709

Reputation:

Not a python answer, but you still might find this interesting. If you don't it is all free, so please forgive me.

I have a solution for C# that is pretty useful for things Paint.Net is bad at, like using the paint bucket tool when it can't figure out where the borders are.

My site https://pixeldatabase.net uses a language I invented called Bitmap Query Language, or BQL for short. If you know SQL for SQL Server, you are 80% of the way to BQL.

The Nuget package that powers this is located here:
Nuget: DataJuggler.PixelDatabase
https://github.com/DataJuggler/PixelDatabase

The BQL that does the same as clicking the Paint Bucket tool in Paint.Net, but better because it fills in the little gaps:

Update
Set Color Black
Where
Total < 450

Result:
enter image description here

The way BQL works is first all the criteria is applied, and in this case all the pixels that have a total < or equal to 450 *, where total equals the sum of Red + Green + Blue.

  • For simplicity less than (<) is the same as less than or equal to.

enter image description here

The background areas all have a total of in the 500's and up, so the query only affects the dark areas.

If you want to not include the extra little black area on the right, just add this:
X < 168

This does the same thing as the first query, but it only affects the pixels that are 168 or less.

enter image description here

And the last thing I will show you, is if you want to remove the background after the previous query:

Hide
Total > 20

And this will get rid of any pixels that have a total value of 20 or higher:

enter image description here

It looks white here because this page is white.

I have used Paint.Net for 15 years, and to me BQL is what images need.

Sorry to post here if it offends anyone, I just think I created something super neat, but maybe like all my other ideas, I am the only one that likes it.

Upvotes: 1

amras
amras

Reputation: 1619

The solution below uses OpenCV and Numpy libraries, but produces even better result than Paint.Net. The explanations of the code sections are inline:

import numpy as np
import cv2

# The standard stuff: image reading, grayscale conversion, inverting, morphology & edge detection
image = cv2.imread('charm.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.bitwise_not(gray)
sqKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
gray = cv2.morphologyEx(gray, cv2.MORPH_CLOSE, sqKernel)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
edges = cv2.Canny(thresh, 50, 200)

# Finding and sorting contours based on contour area
cnts = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5]

# Filling the contours with black color
for c in cnts:
    cv2.drawContours(image, [c], -1, (0, 0, 0), -1)

# Displaying the result
cv2.imshow("Contour", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

The output result:

enter image description here

Upvotes: 2

sam
sam

Reputation: 1896

From my college day, I remember working/building Grayscale morphological filters.

You can check for Grayscale erosion and dilation at the below link. I think Paint.Net has also used techniques similar to these itself.

https://imagej.net/index.php?title=MorphoLibJ&mobileaction=toggle_view_mobile

Hope this helps!

Upvotes: 1

Related Questions