Android Question FloodFill prevent for a color

samannnn

Member
i use floodfill for fill white space between black area.
1- my problem is that floodfill algorithm cannot fill all space ,as you can see, some space in the edge. how Increasing the accuracy of the algorithm?

2- when i click on black lines, floodfill algorithm change the lines color, how can i prevent changing black lines color?
 

Attachments

  • Untitled11.jpg
    Untitled11.jpg
    5 KB · Views: 49
  • Untitled.png
    Untitled.png
    12.2 KB · Views: 60

Brian Dean

Well-Known Member
Licensed User
Longtime User
Probably happens because of antialiasing . . . . . . . . How? Of what object?
The problem is that the image being used was created with anti-aliasing. If you look at the forum example you will see exactly the same "missing pixels" as this poster is seeing. Flood fill will only fill pixels of the specified colour; shaded pixels around edges will not be the specified colour so will not be changed.
 
Upvote 0

emexes

Expert
Licensed User

You could modify the pixel color comparison of this code so that instead of filling pixels that match a specified color, it instead fills pixels that do NOT match specified boundary color(s).

Or add a bit of stretchiness to the comparison, eg RGB components have to match within 32, rather than have to match precisely. Will only half-fix the issue, but better than nothing.
 
Upvote 0

emexes

Expert
Licensed User
You'll have also likely have a colors-don't-precisely-match issue if your image is via a lossy compression like JPEG.
 
Upvote 0

samannnn

Member
Probably happens because of antialiasing. This causes the areas around the lines to be semitransparent. Disable it.
as you can see at the forum example,flood fill cannot fill pixels of around edges and you will see exactly the same problem.

the second question is: how prevent to change black lines color?
 
Upvote 0

samannnn

Member
You could modify the pixel color comparison of this code so that instead of filling pixels that match a specified color, it instead fills pixels that do NOT match specified boundary color(s).

Or add a bit of stretchiness to the comparison, eg RGB components have to match within 32, rather than have to match precisely. Will only half-fix the issue, but better than nothing.
it is difficult to find lines color code. it can be 0000000 or not be.
 
Upvote 0

emexes

Expert
Licensed User

is this part of the code you're using? :

B4X:
Private Sub ColorsEqual(pm1 As PremultipliedColor, pm2 As PremultipliedColor) As Boolean
   Return pm1.r = pm2.r And pm1.g = pm2.g And pm1.b = pm2.b
End Sub

if so then try replacing that color comparison with something like this:

B4X:
Private Sub ColorsEqual(pm1 As PremultipliedColor, pm2 As PremultipliedColor) As Boolean
    Dim MaxDifference As Int = 64    '0 = exact match
 
    If Abs(pm1.r - pm2.r) > MaxDifference Then Return False
    If Abs(pm1.g - pm2.g) > MaxDifference Then Return False
    If Abs(pm1.b - pm2.b) > MaxDifference Then Return False

    Return True
End Sub

which should make things better but not perfect.
 
Upvote 0

emexes

Expert
Licensed User
which should make things better but not perfect.

Do you have control over the original images ie before recoloring?

If so, then what would solve the problem is to ensure that the image only contains the few block-color-area pixel colors, and save the image in a lossless format eg (preferably) PNG or (possibly might also work) GIF with no dithering.
 
Upvote 0

samannnn

Member
is this part of the code you're using? :



if so then try replacing that color comparison with something like this:

B4X:
Private Sub ColorsEqual(pm1 As PremultipliedColor, pm2 As PremultipliedColor) As Boolean
    Dim MaxDifference As Int = 64    '0 = exact match
 
    If Abs(pm1.r - pm2.r) > MaxDifference Then Return False
    If Abs(pm1.g - pm2.g) > MaxDifference Then Return False
    If Abs(pm1.b - pm2.b) > MaxDifference Then Return False

    Return True
End Sub

which should make things better but not perfect.
When replace your code, it doesn't work.
 
Upvote 0

emexes

Expert
Licensed User
If you can post an original of an image before you:

use floodfill for fill white space between black area.

then I will try various fixes here.

I am pretty sure that the only 100% fix is to modify the base image to remove dithering and interpolations and antialiasing, so that it only uses the few actual intended palette colors.
 
Upvote 0

samannnn

Member
If you can post an original of an image before you:



then I will try various fixes here.

I am pretty sure that the only 100% fix is to modify the base image to remove dithering and interpolations and antialiasing, so that it only uses the few actual intended palette colors.
1.png
 
Upvote 0
Top