From: Jacques Oberto Subject: Re: detecting borders Date: 07 Mar 2000 00:00:00 GMT Message-ID: <38C5309D.66C0@NOSPAM.ibpc.fr> Content-Transfer-Encoding: 8bit References: <8a2mjj$4ct1@bornews.borland.com> Content-Type: text/plain; charset=iso-8859-1 Organization: Another Netscape Collabra Server User Mime-Version: 1.0 Reply-To: oberto@NOSPAM.ibpc.fr Newsgroups: borland.public.delphi.graphics Hi Kolbeinn: You will find below an example unit to "autocrop" a bitmap in response to your first question. The Scanline stuff is inspired from E.F.Glynn's code. If you solve the multiple crop problem, please let me know. I hope it helps, Jacques Kolbeinn Sigurjonsson wrote: > I have a bitmap that contains a graphic, but the images is larger so that > the graphic is surrounded with white (or any color) borders. (typically when > scanned). > > Is there a good algorythm that will help me to detect the "upperleft" end > "lowerright" corners of the graphic, therefore enabling me to exclude the > surrounding area with a rubberband? > > Far better: > I have one bitmap which contains two or more small images in it (I scan > multiple photos in a row from my scanner) and I want to be able to detect > the border of every single photo.... _________________________________________ unit Unit1;   interface   uses   Windows, Messages, SysUtils, Classes, Graphics,   Controls, Forms, Dialogs, StdCtrls, ExtCtrls;   const   PixelCountMax = 32768;   type   pRGBArray = ^TRGBArray;   TRGBArray = array[0..PixelCountMax-1] of TRGBTriple;   type   TForm1 = class(TForm)     Button1: TButton;     procedure FormCreate(Sender: TObject);     procedure FormDestroy(Sender: TObject);     procedure Button1Click(Sender: TObject);   private     { Private declarations }   public     { Public declarations }   end;   var   Form1: TForm1;   OBitmap, DBitmap : TBitmap;   implementation   {$R *.DFM}   procedure TForm1.FormCreate(Sender: TObject); begin   Form1.color := clLtGray;   OBitmap := TBitmap.Create;   OBitmap.PixelFormat := pf24bit;   DBitmap := TBitmap.Create;   DBitmap.PixelFormat := pf24bit; end;   procedure TForm1.FormDestroy(Sender: TObject); begin   Obitmap.Free;   DBitmap.Free; end;   procedure TForm1.Button1Click(Sender: TObject); var   BackColor : TColor ;   i : INTEGER;   j : INTEGER;   Row: pRGBArray;   IsBlack : Integer;   MyTop, MyBottom, MyLeft, MyRight : Integer;   TempTop, TempBottom : Integer;   begin   OBitmap.Width := 100;   Obitmap.Height := 100;   OBitmap.Canvas.TextOut(20,20,'Hello World !!!');   Form1.Canvas.Draw(0,0,OBitmap);   //Retrieve Background Color   BackColor := OBitmap.Canvas.Pixels[0,0];   MyTop := 0;   TempTop := 0;   MyBottom := 0;   TempBottom := OBitmap.Height;   MyLeft := OBitmap.Width;   MyRight := 0;     //Find Top   for j := 0 to OBitmap.Height-1 do   begin     Row := pRGBArray(OBitmap.Scanline[j]);     isBlack := 0;     for i := 0 to OBitmap.Width-1 do     begin     if (Row[i].rgbtRed  <> GetRvalue(BackColor)) and       (Row[i].rgbtGreen <> GetGvalue(BackColor)) and       (Row[i].rgbtBlue  <> GetBvalue(BackColor)) then     inc(isBlack);     end;     if isBlack = 0 then Inc(TempTop);     if isBlack <> 0 Then MyTop := TempTop;   end;     //Find Bottom   for j := OBitmap.Height-1 Downto 0 do   begin     Row := pRGBArray(OBitmap.Scanline[j]);     isBlack := 0;     for i := 0 to OBitmap.Width-1 do     begin     if (Row[i].rgbtRed  <> GetRvalue(BackColor)) and       (Row[i].rgbtGreen <> GetGvalue(BackColor)) and       (Row[i].rgbtBlue  <> GetBvalue(BackColor)) then     inc(isBlack);     end;     if isBlack = 0 then Dec(TempBottom);     if isBlack <> 0 Then MyBottom := TempBottom;   end;     //Find Left   for j := 0 to OBitmap.Height-1 do   begin     Row := pRGBArray(OBitmap.Scanline[j]);     for i := 0 to OBitmap.Width-1 do     begin       if (Row[i].rgbtRed  <> GetRvalue(BackColor)) and         (Row[i].rgbtGreen <> GetGvalue(BackColor)) and         (Row[i].rgbtBlue  <> GetBvalue(BackColor)) then       if i < MyLeft then MyLeft := i;     end;   end;     //Find Right   for j := 0 to OBitmap.Height-1 do   begin     Row := pRGBArray(OBitmap.Scanline[j]);     for i := OBitmap.Width-1  downto 0 do     begin       if (Row[i].rgbtRed  <> GetRvalue(BackColor)) and         (Row[i].rgbtGreen <> GetGvalue(BackColor)) and         (Row[i].rgbtBlue  <> GetBvalue(BackColor)) then       if i > MyRight then MyRight := i;     end;   end;     DBitmap.Width := MyRight - MyLeft + 4;   Dbitmap.Height := MyBottom - MyTop + 4;     BitBlt(DBitmap.canvas.Handle,-MyLeft+2,-MyTop+3,MyLeft+         MyRight,MyTop+MyBottom, OBitmap.Canvas.Handle,0,0,         SRCCOPY);     Form1.Canvas.Draw(0,120,DBitmap); end;   end.