充填图像的一部分 [英] Floodfill part of an Image
问题描述
我有一个类似波纹管的图像
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
推荐答案
似乎您要用红色替换所有暗像素,并用白色替换所有亮像素。您的代码几乎在那里。不要将黑色像素替换为黑色,然后对黑色像素进行泛洪,为什么不直接将它们设置为红色,而完全忘记泛洪呢?
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.
只需用 clRed替换代码中的
,然后删除所有与Floodfill相关的代码。 RGB(0,0,0)
Just replace the RGB(0,0,0)
in your code with clRed
, and remove all the code related to the floodfill.
我应该尝试解释什么是洪水填充,以及为什么它没有达到您的预期。泛洪将所有颜色相似,相邻像素的颜色更改为选定的种子像素。也就是说,它从您选择的像素开始,在这种情况下为(posX,posY)。它处理该像素,然后处理与该像素相邻的所有相同颜色的像素,然后处理与那些像素相邻的相同颜色的像素,依此类推。
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.
在这种情况下,您将在字母i上的圆点处开始填充。 泛洪从该点向外扩散,从黑色像素到黑色像素,直到到达由白色像素形成的边界。洪水永远不会到达字母i的主体,因为这些点与种子点不相邻。
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.
来自Wikipedia的此链接提供了更多解释和一些动画,可能会帮助您理解。 http://en.wikipedia.org/wiki/Flood_fill
This link from Wikipedia has more explanation and some animations that may help you understand. http://en.wikipedia.org/wiki/Flood_fill
这篇关于充填图像的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!