Arsip: Crop Image dari Background.....

 
user image
more 16 years ago

RoenZ

Hai smuanya..... ada yang tau nga gimana caranya crop image dari background??? Maskudnya begini.... Misalnya ada objek orang dan backgroundnya warna hijau... trus gimana caranya memisahkan antara orang dengan backgroundnya??? Objek orang akan dipisahkan dan langsung di-crop dari image dan posisi pixel x dan y pun kita ketahui....klo kita crop dengan rectangle berarti ada koordinat dari rectangle tersebut (top,left,right,bottom)..... Mohon bantuannya yah....soalnya dah mentok banget....... Thx.... -RoenZ-
user image
more 16 years ago

ebta

Ini ada kode yang mungkin bisa membantu
{ ... }
  type
    TRGBArray = Array of TRGBTriple;  {pf24bit}
    pRGBArray = ^TRGBArray;
{ ... }

function CropRect(Bitmap: TBitmap; C: TColor): TRect;
var
  ScanlinePtr: PByte;
  ScanlineIncrement: Integer;
  LastScanline: PByte;
  X: Integer;
  Line: pRGBArray;
  AColor : TColor;
  r, g, b: Integer;
  x1, x2, y1, y2, x3, x4, y3, n: Integer;
begin
  Assert(Bitmap.PixelFormat = pf24bit);  {must be pf24bit!}
  AColor := ColorToRGB(C);
  r := GetRValue(AColor);
  g := GetGValue(AColor);
  b := GetBValue(AColor);
  X := Bitmap.Width;
  ScanlinePtr := Bitmap.Scanline[0];
  ScanlineIncrement := integer(Bitmap.Scanline[1]) - integer(ScanlinePtr);
  LastScanline := ScanlinePtr;
  Inc(LastScanLine, ScanlineIncrement  * Bitmap.Height);
  x1 := Bitmap.Width;
  x2 := 0;
  y1 := 0;
  y2 := Bitmap.Height;
  y3 := 0;
  repeat
    Line := pRGBArray(ScanLinePtr);
    x3 := - 1;
    x4 := - 1;
    n := 0;
    while (n < X) and (x3 < 0) do
    begin
      if (Line[n].rgbtRed <> r) or (Line[n].rgbtGreen <> g) or (Line[n].rgbtBlue <> b) then
        x3 := n;
      Inc(n);
    end;
    n := X - 1;
    while (n > - 1) and (x4 < 0) do
    begin
      if (Line[n].rgbtRed <> r) or (Line[n].rgbtGreen <> g) or (Line[n].rgbtBlue <> b) then
        x4 := n;
      Dec(n);
    end;
    if (x3 > - 1) and (x3 < x1) then
      x1 := x3;
    if (x4 > - 1) and (x4 > x2) then
      x2 := x4;
    if (x3 > - 1) or (x4 > - 1) then
    begin
      if y1 = 0 then
        y1 := y3
      else
        y2 := y3;
    end;
    Inc(ScanlinePtr, ScanlineIncrement);  {move to the next scanline}
    Inc(y3);
  until
    (ScanlinePtr = LastScanline);
  result := Rect(x1, y1, x2 + 1, y2);
  if IsRectEmpty(result) then
    result := Rect(0, 0, Bitmap.Width, Bitmap.Height);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  r: TRect;
begin
  if Assigned(Image1.Picture) then  {contains srcBmp}
  begin
    r := CropRect( srcBmp, FCropColor );  {FCropColor is lower left pixel}
    cropBmp.Width := r.Right - r.Left;
    cropBmp.Height := r.Bottom - r.Top;
    cropBmp.Canvas.CopyRect( Rect(0, 0, cropBmp.Width, cropBmp.Height), srcBmp.Canvas, r );
    Image2.Picture.Bitmap.Assign(cropBmp);
  end;
end;
user image
more 16 years ago

pebbie

asumsi pixelformat = pf24bit tipe data dasar

type
  TWarnaRGB = packed record
    b, g, r : byte;
  end;
  TArrWarnaRGB = array of TWarnaRGB;
  PArrRGB = ^TArrWarnaRGB;
fungsi dasar

function max2(a, b:integer):integer;begin
   if a>b then result := a else result := b;
end;

function min2(a, b:integer):integer;begin
   if a>b then result := b else result := a;
end;
function is_same_color(w1, w2 : TWarnaRGB):boolean;
const epsilon = 1e-5;
begin
   //use simple
   result := (w1.r = w2.r) and (w1.g=w2.g) and (w1.b=w2.b);
   //or use distance measure
   //result := (sqrt(sqr(w2.r-w1.r)+sqr(w1.g-w2.g)+sqr(w1.b-w2.b))) < epsilon;
end;
fungsi cropping

function GetCropRect(b:TBitmap; BGColor : TWarnaRGB):TRect;
    j, i : integer;
    p : PArrRGB;
    p1, p2 : TPoint;
begin
   { get crop rect }
   result := Rect(0, 0, 0, 0);
   p1 := Point(9999, 9999);
   p2 := Point(-9999, -9999);
   for j : = 0 to b.Height-1 do begin
      p := b.scanline[j];
      for i := 0 to b.Width-1 do begin
         if not is_same_color(BGColor, p[i]) then begin
            p1 := Point(min2(i, p1.X), min2(j, p1.Y));//kiri atas
            p2 := Point(max2(i, p2.X), max2(j, p2.Y));//kanan bawah
         end;
      end;
   end;
   result := Rect(p1.X, p1.Y, 1+p2.X-p1.X, 1+p2.Y-p1.Y);//x, y, w, h
end;
//get cropped bitmap
function CropBitmap(b:TBitmap; cRect:TRect; BGColor : TWarnaRGB):TBitmap;
var
   p1, p2 : PArrRGB;
   i, j : integer;
begin
   result := TBitmap.Create;
   result.Width := cRect.w;
   result.Height := cRect.h;
   result.PixelFormat := pf24bit;
   for j : = 0 to result.Height-1 do begin
      p1 := b.scanline;
      p2 := result.scanline[j];
      for i := 0 to result.Width-1 do begin
         if not is_same_color(BGColor, p[i]) then begin
            p2[i] := p1;
         end;
      end;
   end;
end;
variabel global

var
    cropRect : TRect;
    cropBmp  : TBitmap;
    srcBmp : TBitmap;//asumsi sudah ada
    BGColor : TWarnaRGB;
main code

begin
   cropRect := GetCropRect(srcBmp, BGColor);//background warna hijau
   cropBmp := CropBitmap(srcBmp, cropRect, BGColor); 
end.
user image
more 16 years ago

pebbie

@RoenZ: Hai smuanya..... ada yang tau nga gimana caranya crop image dari background??? Maskudnya begini.... Misalnya ada objek orang dan backgroundnya warna hijau... trus gimana caranya memisahkan antara orang dengan backgroundnya??? Objek orang akan dipisahkan dan langsung di-crop dari image dan posisi pixel x dan y pun kita ketahui....klo kita crop dengan rectangle berarti ada koordinat dari rectangle tersebut (top,left,right,bottom)..... Mohon bantuannya yah....soalnya dah mentok banget....... Thx.... -RoenZ-
eh, kodeku salah ya? :D asumsi kodeku ngambil bounding rect dari seluruh objek yang ada di gambar asal (kalau cuma 1 ya bagus.. :D)
user image
more 16 years ago

RoenZ

Wah2 makasih banyak yah.... atas kode2 nya....
more ...
  • Pages:
  • 1
Share to
Local Business Directory, Search Engine Submission & SEO Tools FreeWebSubmission.com SonicRun.com