问题
I have an image like bellow
i want to make the image become like
I already make the code, like this :
var
Tx,Ty, R,G,B,A, posX, posY : integer;
begin
posX :=-1; posY := -1;
for Ty:= 0 to Image1.Height-1 do
begin
for Tx:= 0 to Image1.Width-1 do
begin
R:= GetRValue(image1.Canvas.Pixels[Tx, Ty]);
G:= GetGValue(image1.Canvas.Pixels[Tx, Ty]);
B:= GetBValue(image1.Canvas.Pixels[Tx, Ty]);
A:= (R + G + B) Div 3;
if (A > 50) then
begin
Image1.Canvas.Pixels[Tx,Ty] := rgb(255,255,255);
end else
begin
Image1.Canvas.Pixels[Tx,Ty] := rgb(0,0,0);
if (posX < 0) then
begin
posX := Tx;
posY := Ty;
end;
end;
end;
end;
Image1.Canvas.Brush.Style := bsSolid;
Image1.Canvas.Brush.Color := clred;
Image1.Canvas.FloodFill(posX,posY,clBlack,fsSurface);
end;
My coding above only can make the floodfill only for the dot.
how can i make floodfill like the result image? thanks
回答1:
It appears that you want to replace all dark pixels with red, and all light pixels with white. Your code is almost there. Instead of replacing the dark pixels with black, and then floodfill-ing the black pixels, why not just set them directly to red, and forget about the floodfill altogether.
Just replace the RGB(0,0,0)
in your code with clRed
, and remove all the code related to the floodfill.
I should try to explain what floodfill is and why it didn't do what you expected. A floodfill changes the color of all similarly-colored, contiguously-adjacent pixels to the selected seed pixel. That is to say, it starts with the pixel you select, in your case, (posX,posY). It processes that pixel, and then all the same-colored pixels adjacent to that pixel, and then the same-colored pixels adjacent to those pixels, and so on.
In this case, you begin the floodfill at a point in the dot over the letter i. The "flood" spreads outwards from that point, from black-pixel to black-pixel, until it hits the border formed by the white pixels. The flood never reaches the main body of the letter i because those points are not contiguous to the seed point.
This link from Wikipedia has more explanation and some animations that may help you understand. http://en.wikipedia.org/wiki/Flood_fill
来源:https://stackoverflow.com/questions/23767303/floodfill-part-of-an-image