Reputation: 2244
I have this degraded image :
Using paintbucket
tool in Paint.NET
Windows application, I fill the character image black.
My questions are :
Thanks.
Upvotes: 2
Views: 656
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
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.
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.
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:
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
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:
Upvotes: 2
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