• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

how can i fill the different color with the region separate by the black line?

hi,
  experts,please help me,
the image in here:
http://www26.brinkster.com/jbaswjy
i want to fill the different color with the region seperate by the black line,how can i do?
 thanks
0
jbas
Asked:
jbas
  • 8
  • 6
  • 3
  • +2
1 Solution
 
333Commented:
use TCanvas.FloodFill
for example:
  Image1.Picture.Bitmap.Canvas.Brush.Style:=bsSolid;
  Image1.Picture.Bitmap.Canvas.Brush.Color:=clBlue;
  Image1.Picture.Bitmap.Canvas.FloodFill(1, 1, clWhite, fsSurface);

this will paint your region (topmost on the left) with blue.
0
 
jbasAuthor Commented:
to 333
 thanks!but it not work ,mybe you not understond my meaning,i want to fill different color in a timage.and the image first assign a picture,like  this :http://www26.brinkster.com/jbaswjy
and second i hope to fill different color in the image,separate by the black line area!
thanks!
0
 
CynnaCommented:
jbas,

1. So, if I got you right, you want to fill each region
   with different color, right?

2. These regions are enclosed with black line, and "filled"
   with white color, right?

3. Do you have some idea what are these colors for each
   region, or do you just want to have regions filled
   with random colors ?

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jbasAuthor Commented:
to  Cynna :
  thanks to you answer!
>>1. So, if I got you right, you want to fill each region
>>  with different color, right?
yes!
>>2. These regions are enclosed with black line, >>and "filled"  with white color, right?
yes!
>>3. Do you have some idea what are these colors for each
>>  region, or do you just want to have regions filled
>>  with random colors ?
random colors!
i waiting you answer! thanks!
0
 
SlavakCommented:
it sounds as common task to color a map with 4 different colors, isn't it?
0
 
333Commented:
jbas,

this works for me (i assume, that bitmap is monochrome):

procedure TForm1.Button1Click(Sender: TObject);
var
  i,j: integer;
begin
  // Load bitmap
  Image1.Picture.LoadFromFile('d:\temp\temp.bmp');
  // If it's monochrome, make it color
  Image1.Picture.Bitmap.PixelFormat:=pf8bit;
  Randomize;
  // Go thru bitmap pixel by pixel
  for i:=0 to Image1.Picture.Bitmap.Width-1 do
    for j:=0 to Image1.Picture.Bitmap.Height-1 do
  // See if pixels color is white
  //  If white then FloodFill area with random color
      if Image1.Picture.Bitmap.Canvas.Pixels[i,j]=clWhite then begin
        Image1.Picture.Bitmap.Canvas.Brush.Color:=StringToColor(Format('$00%0.2x%0.2x%0.2x',[Random(254),Random(254),Random(254)]));
        Image1.Picture.Bitmap.Canvas.FloodFill(i,j,clWhite,fsSurface);
      end;
end;

0
 
CynnaCommented:
jbas,

333 gave you the wright answer.
Copy/Paste that example, it will do what you want.

0
 
jbasAuthor Commented:
thank!
   when i use bmp,it can work well ,but when i use jpg,it can not work!why?
0
 
jbasAuthor Commented:
333 ,Cynna :
  thanks!i should .jpg ->.bmp ,and do your code to finished my work?
0
 
333Commented:
well, to use .jpg you should add unit JPEG to your form.
for example:

uses Windows,Forms,...,JPEG;

after that, you will be able to load .jpg .
however, TJPEGImage doesn't have canvas property, so you wont be able to paint on it (fill regions). you will need to copy image on to bitmap, in order to paint on it.

so, here is some changes you need to make in order to use jpg:

1) add JPEG to your form
uses Windows,Forms,...,JPEG;

2) and my previous procedure should look like this:
procedure TForm1.Button1Click(Sender: TObject);
var
 i,j: integer;
 JPGImage : TJPEGImage;
 ImageFileName: string;
begin
 // Name of your image file
 ImageFileName:='d:\temp\temp.jpg';

 // Check here for image file extension, so you know if it's jpg or bmp
 if LowerCase(ExtractFileExt(ImageFileName))='.bmp' then begin
 // Load bitmap
   Image1.Picture.LoadFromFile(ImageFileName);
 // If it's monochrome, make it color
   Image1.Picture.Bitmap.PixelFormat:=pf8bit;
 end
 else begin
 // Process jpg image
   JPGImage := TJPEGImage.Create;
   JPGImage.LoadFromFile(ImageFileName);
 // Copy jpg to bitmap
   Image1.Picture.Assign(JPGImage);
   JPGImage.Free;
 end;

 Randomize;
 // Go thru bitmap pixel by pixel
 for i:=0 to Image1.Picture.Bitmap.Width-1 do
   for j:=0 to Image1.Picture.Bitmap.Height-1 do
 // See if pixels color is white
 //  If white then FloodFill area with random color
     if Image1.Picture.Bitmap.Canvas.Pixels[i,j]=clWhite then begin
       Image1.Picture.Bitmap.Canvas.Brush.Color:=StringToColor(Format('$00%0.2x%0.2x%0.2x',[Random(254),Random(254),Random(254)]));
       Image1.Picture.Bitmap.Canvas.FloodFill(i,j,clWhite,fsSurface);
     end;
end;

0
 
hgbdelphiCommented:
sorry,it can not work well.please help me.

procedure Tsimulate.Button1Click(Sender: TObject);
var
  i,j: integer;
  JPGImage : TJPEGImage;
  ImageFileName: string;
  icolor:Tcolor;
  wb:boolean;
begin
   if OpenPictureDialog1.Execute then
   begin
       ImageFileName:=OpenPictureDialog1.FileName;
       if LowerCase(ExtractFileExt(ImageFileName))='.bmp' then
         Imagbf.Picture.LoadFromFile(ImageFileName)
       else
       begin
        JPGImage := TJPEGImage.Create;
        JPGImage.LoadFromFile(ImageFileName);
        Imagbf.Picture.Assign(JPGImage);
        JPGImage.Free;
       end;
   end;
//   repaint;
   Randomize;

 for i:=0 to Imagbf.Picture.Bitmap.Width-1 do
 for j:=0 to Imagbf.Picture.Bitmap.Height-1 do
 if Imagbf.Picture.Bitmap.Canvas.Pixels[i,j]=clWhite then
 begin
    icolor:= rgb(random(1000),random(1000),random(1000));
    if (icolor<>clblack) and (icolor<>clwhite) then
    begin
       Imagbf.Picture.Bitmap.Canvas.Brush.Color:=icolor;
       Imagbf.Picture.Bitmap.Canvas.FloodFill(i,j,clWhite,fsSurface);
    end;
 end;

  repaint;
{
 for i:=0 to Imagbf.Picture.Bitmap.Height-1 do
 for j:=0 to Imagbf.Picture.Bitmap.Width-1 do
 begin
     wb:=false;
     if Imagbf.Picture.Bitmap.Canvas.Pixels[j,i]=clblack then
     begin
            Imagbf.Picture.Bitmap.Canvas.Pixels[j,i]:=Imagbf.Picture.Bitmap.Canvas.Pixels[j-1,i];
            wb:=true;
     end;
     if Imagbf.Picture.Bitmap.Canvas.Pixels[j+1,i]=clblack then
     begin
       if wb=true then
       Imagbf.Picture.Bitmap.Canvas.Pixels[j+1,i]:=Imagbf.Picture.Bitmap.Canvas.Pixels[j+2,i];
       wb:=false;
     end;
 end;
 repaint;
 Btnmn.Enabled:=true;
 }
end;
0
 
333Commented:
what exactly is wrong? empty bitmap? bad colors? regions aren
't filled?
0
 
hgbdelphiCommented:
sorry,it can not work well.please help me.

procedure Tsimulate.Button1Click(Sender: TObject);
var
  i,j: integer;
  JPGImage : TJPEGImage;
  ImageFileName: string;
  icolor:Tcolor;
  wb:boolean;
begin
   if OpenPictureDialog1.Execute then
   begin
       ImageFileName:=OpenPictureDialog1.FileName;
       if LowerCase(ExtractFileExt(ImageFileName))='.bmp' then
         Imagbf.Picture.LoadFromFile(ImageFileName)
       else
       begin
        JPGImage := TJPEGImage.Create;
        JPGImage.LoadFromFile(ImageFileName);
        Imagbf.Picture.Assign(JPGImage);
        JPGImage.Free;
       end;
   end;
//   repaint;
   Randomize;

 for i:=0 to Imagbf.Picture.Bitmap.Width-1 do
 for j:=0 to Imagbf.Picture.Bitmap.Height-1 do
 if Imagbf.Picture.Bitmap.Canvas.Pixels[i,j]=clWhite then
 begin
    icolor:= rgb(random(1000),random(1000),random(1000));
    if (icolor<>clblack) and (icolor<>clwhite) then
    begin
       Imagbf.Picture.Bitmap.Canvas.Brush.Color:=icolor;
       Imagbf.Picture.Bitmap.Canvas.FloodFill(i,j,clWhite,fsSurface);
    end;
 end;

  repaint;
{
 for i:=0 to Imagbf.Picture.Bitmap.Height-1 do
 for j:=0 to Imagbf.Picture.Bitmap.Width-1 do
 begin
     wb:=false;
     if Imagbf.Picture.Bitmap.Canvas.Pixels[j,i]=clblack then
     begin
            Imagbf.Picture.Bitmap.Canvas.Pixels[j,i]:=Imagbf.Picture.Bitmap.Canvas.Pixels[j-1,i];
            wb:=true;
     end;
     if Imagbf.Picture.Bitmap.Canvas.Pixels[j+1,i]=clblack then
     begin
       if wb=true then
       Imagbf.Picture.Bitmap.Canvas.Pixels[j+1,i]:=Imagbf.Picture.Bitmap.Canvas.Pixels[j+2,i];
       wb:=false;
     end;
 end;
 repaint;
 Btnmn.Enabled:=true;
 }
end;
0
 
hgbdelphiCommented:
sorry,it can not work well.please help me.

procedure Tsimulate.Button1Click(Sender: TObject);
var
  i,j: integer;
  JPGImage : TJPEGImage;
  ImageFileName: string;
  icolor:Tcolor;
  wb:boolean;
begin
   if OpenPictureDialog1.Execute then
   begin
       ImageFileName:=OpenPictureDialog1.FileName;
       if LowerCase(ExtractFileExt(ImageFileName))='.bmp' then
         Imagbf.Picture.LoadFromFile(ImageFileName)
       else
       begin
        JPGImage := TJPEGImage.Create;
        JPGImage.LoadFromFile(ImageFileName);
        Imagbf.Picture.Assign(JPGImage);
        JPGImage.Free;
       end;
   end;
//   repaint;
   Randomize;

 for i:=0 to Imagbf.Picture.Bitmap.Width-1 do
 for j:=0 to Imagbf.Picture.Bitmap.Height-1 do
 if Imagbf.Picture.Bitmap.Canvas.Pixels[i,j]=clWhite then
 begin
    icolor:= rgb(random(1000),random(1000),random(1000));
    if (icolor<>clblack) and (icolor<>clwhite) then
    begin
       Imagbf.Picture.Bitmap.Canvas.Brush.Color:=icolor;
       Imagbf.Picture.Bitmap.Canvas.FloodFill(i,j,clWhite,fsSurface);
    end;
 end;

  repaint;
{
 for i:=0 to Imagbf.Picture.Bitmap.Height-1 do
 for j:=0 to Imagbf.Picture.Bitmap.Width-1 do
 begin
     wb:=false;
     if Imagbf.Picture.Bitmap.Canvas.Pixels[j,i]=clblack then
     begin
            Imagbf.Picture.Bitmap.Canvas.Pixels[j,i]:=Imagbf.Picture.Bitmap.Canvas.Pixels[j-1,i];
            wb:=true;
     end;
     if Imagbf.Picture.Bitmap.Canvas.Pixels[j+1,i]=clblack then
     begin
       if wb=true then
       Imagbf.Picture.Bitmap.Canvas.Pixels[j+1,i]:=Imagbf.Picture.Bitmap.Canvas.Pixels[j+2,i];
       wb:=false;
     end;
 end;
 repaint;
 Btnmn.Enabled:=true;
 }
end;
0
 
jbasAuthor Commented:
i use the code but empty bitmap,why?
0
 
jbasAuthor Commented:
to 333 ,Cynna :
  thanks!i do not finished it,i use:
             imagbf.Picture.Assign(JPGImage);
i shoule trans .jpg to .bmp????
0
 
333Commented:
oops, sorry, my mistake :)

change the line where you assign JPEG image to TImage from
  Image1.Picture.Assign(JPGImage);
to
  Image1.Picture.Bitmap.Assign(JPGImage);

or in your code change the line from your last post from  imagbf.Picture.Assign(JPGImage);
to
  imagbf.Picture.Bitmap.Assign(JPGImage);
0
 
jbasAuthor Commented:
333,
  thanks to answer my question!
  but if i use image1.picture.bitmap.assign(jpgimage),it can appear the picture in timage,but the picture is not clear,do not finished my work.
  how can i do it ?
0
 
333Commented:
what do you mean "the picture is not clear"? is there lot of white space that isn't filled?
if yes, then this is because your jpg image is not 100% white. due to jpg compression some pixels in image are not RGB(255,255,255), but their value is something near white, for example RGB(253,250,254), so those pixels will not be filled.
the best solution would be to use monochrome bitmaps (with only two colors - 100% white and 100% black).
another solution would be to convert loaded jpg image to monochrome and then back to color. to do this add this to your code (right after line Randomize;):

Image1.Picture.Bitmap.Monochrome:=true;
Image1.Picture.Bitmap.PixelFormat:=pf16bit;
but as you will see, your picture will be very scary :) this is because when converting to monochrome, delphi converts each pixel that is not 100% white to 100% black. so if you have gray pixel, that is very near to white (i.e. RGB(253,255,255)) it will be converted to clBlack.
as a workaround you may instead of those two lines add this loop:
for i:=0 to Image1.Picture.Bitmap.Width-1 do
  for j:=0 to Image1.Picture.Bitmap.Height-1 do
    if (GetRValue(Image1.Picture.Bitmap.Canvas.Pixels[i,j])>30) and
       (GetGValue(Image1.Picture.Bitmap.Canvas.Pixels[i,j])>30) and
       (GetBValue(Image1.Picture.Bitmap.Canvas.Pixels[i,j])>30) then
      Image1.Picture.Bitmap.Canvas.Pixels[i,j]:=clWhite
    else
      Image1.Picture.Bitmap.Canvas.Pixels[i,j]:=clBlack;

this will set any pixel that is darker than RGB(30,30,30) to black and any others to white.
0
 
jbasAuthor Commented:
thanks ,my english is poor ,sorry!
Cynna ,!because the points can not share,sorry!
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 8
  • 6
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now