線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2675
推到 Plurk!
推到 Facebook!

圖片放大問題-使用內差法

尚未結案
cubi
初階會員


發表:56
回覆:94
積分:35
註冊:2002-04-15

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-11-15 10:30:18 IP:61.58.xxx.xxx 未訂閱
請教各位前輩
  小弟參考arisaka_matsuri前輩的作品,內差法影像縮放,
http://delphi.ktop.com.tw/topic.php?topic_id=49370
改成delphi,出來的結果如下:
ps: 原圖為畫面截取下來,非從檔案Load進來        程式碼:    implementation
var
  bmp1, bmp2: TBitmap;    procedure TForm2.ImgResize(RefBitmap, DescBitmap: TBitmap);
var
  XRatio, YRatio: Double;
  dx, dy: Double;           // (x,y) 分別與 (l,k) 距離
  x, y: Double;             // (x,y) 代表 pDestBitmap(c,r) 對應到 pRefBitmap 中的位置
  c1, c2, c3, c4: Integer;  // Bilinear Eq.: f = c1 + c2*x + c3*y + c4*x*y
  k, l: Integer;            // (l,k) 代表 pRefBitmap 像素的位置(x,y)
  r, c: Integer;            // (c,r) 代表 pDestBitmap 像素的位置(x,y)
  b: Integer;
  pRef: array of PByteArray;
  pDest: PByteArray;
begin
  XRatio := DescBitmap.Width / RefBitmap.Width;
  YRatio := DescBitmap.Height / RefBitmap.Height;      // 準備指向pRefBitmap像素記憶體的指標
  SetLength(pRef, RefBitmap.Height);
  for k := Low(pRef) to High(pRef) do
    pRef[k] := RefBitmap.ScanLine[k];      // bilinear 內差
  for r := 0 to DescBitmap.Height - 1 do
  begin
    pDest := DescBitmap.ScanLine[r];
    for c := 0 to DescBitmap.Width - 1 do
    begin
      if XRatio < 1 then x := c / XRatio + XRatio
      else               x := c / XRatio;
      if YRatio < 1 then y := r / YRatio + YRatio
      else               y := r / YRatio;
      l := Floor(x);
      k := Floor(y);
      dx := x - l;
      dy := y - k;
      // 分別對BGR三色計算
      for b := 0 to 2 do
      begin
        c1 := pRef[k][l * 3 + b];            if l = RefBitmap.Width - 1 then
          c2 := 0
        else
          c2 := pRef[k][(l + 1) * 3 + b] - pRef[k][l * 3 + b];            if k = RefBitmap.Height - 1 then
          c3 := 0
        else
          c3 := pRef[k + 1][l * 3 + b] - pRef[k][l * 3 + b];            if (l = RefBitmap.Width - 1) or (k = RefBitmap.Height - 1) then
          c4 := 0
        else
          c4 := pRef[k][l * 3 + b] + pRef[k + 1][(l + 1) * 3 + b] -
                pRef[k][(l + 1) * 3 + b] - pRef[k + 1][l * 3 + b];            pDest[c * 3 + b] := Byte(Trunc(c1 + c2 * dx + c3 * dy + c4 * dx * dy));
      end;
    end;
  end;
end;    
發表人 - cubi 於 2004/11/15 10:52:38
cubi
初階會員


發表:56
回覆:94
積分:35
註冊:2002-04-15

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-11-15 10:34:27 IP:61.58.xxx.xxx 未訂閱

procedure TForm2.FormCreate(Sender: TObject);
begin
  bmp1 := TBitmap.Create;
  bmp2 := TBitmap.Create;
  bmp1.PixelFormat := pf24bit;
  bmp2.PixelFormat := pf24bit;
  //SourceImage.Left := 0;
  //SourceImage.Top := 0;
end;    procedure TForm2.TrackBar1Change(Sender: TObject);
var
  nRatio: Byte;
begin
  nRatio := TrackBar1.Position   1;
  if nRAtio = 1 then
  begin
    //Image=>放大後的圖
    Image1.Width := SourceImage.Width;
    Image1.Height := SourceImage.Height;
    Image1.Picture.Bitmap.Assign(SourceImage.Picture.Bitmap);
  end
  else
  begin
    bmp1 := SourceImage.Picture.Bitmap;
    bmp2.Width := bmp1.Width * nRatio;
    bmp2.Height := bmp1.Height * nRatio;
    //SourceImage.Width := bmp2.Width;
    //SourceImage.Height := bmp2.Height;
    ImgResize(bmp1, bmp2);
    Image1.Width := bmp2.Width;
    Image1.Height := bmp2.Height;
    Image1.Picture.Bitmap.Assign(bmp2);
  end;
end;    請問前輩,小弟是那兒出錯了,放大後的圖,變成彩色的,而且似乎
並沒有按比例放大,放大後,右邊有被切掉
謝謝,謝謝
發表人 - cubi 於 2004/11/15 10:56:04
richtop
資深會員


發表:122
回覆:646
積分:468
註冊:2003-06-10

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-11-15 17:29:31 IP:211.76.xxx.xxx 未訂閱
cubi 您好:    依您上傳的程式重建後測試發現: 在256色Bitmap圖檔下,的確會出現問題,建議是:在TrackBar1Change(.)中,再一次的設定PixelFormat,如下紅色部分。 至於沒有跟著放大?我測試時似乎沒有這個現象。 上述結果供您參考。    
引言:
    procedure TForm2.TrackBar1Change(Sender: TObject);
var
  nRatio: Byte;
begin
  nRatio := TrackBar1.Position   1;
  SourceImage.Picture.Bitmap.PixelFormat := pf24bit;
  if nRAtio = 1 then
  begin
    //Image=>放大後的圖
    Image1.Width := SourceImage.Width;
    Image1.Height := SourceImage.Height;
    Image1.Picture.Bitmap.Assign(SourceImage.Picture.Bitmap);
  end
  else
  begin
    bmp1 := SourceImage.Picture.Bitmap;
    bmp2.Width := bmp1.Width * nRatio;
    bmp2.Height := bmp1.Height * nRatio;
    //SourceImage.Width := bmp2.Width;
    //SourceImage.Height := bmp2.Height;
    ImgResize(bmp1, bmp2);
    Image1.Width := bmp2.Width;
    Image1.Height := bmp2.Height;
    Image1.Picture.Bitmap.Assign(bmp2);
  end;
end;

請問前輩,小弟是那兒出錯了,放大後的圖,變成彩色的,而且似乎
並沒有按比例放大,放大後,右邊有被切掉
謝謝,謝謝
RichTop 敬上 =====***** 把數學當工具,可以解決問題;將數學變能力,能夠發現並解決問題! =====#####
japhenchen
高階會員


發表:51
回覆:444
積分:184
註冊:2003-07-23

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-11-15 18:16:53 IP:219.134.xxx.xxx 未訂閱
http://delphi.ktop.com.tw/topic.php?TOPIC_ID=58409 利用系統提供的api 來達成你要的目地 藏私の禁止
cubi
初階會員


發表:56
回覆:94
積分:35
註冊:2002-04-15

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-11-16 11:15:07 IP:203.187.xxx.xxx 未訂閱
謝謝前輩,可以了,但為什麼會這樣呢,可否請前輩指點迷津, 另感謝japhenchen前輩,CopyRect小弟之前已有試過,但放大 後會有距齒邊緣,故採平內差法,再次感謝前輩,謝謝
richtop
資深會員


發表:122
回覆:646
積分:468
註冊:2003-06-10

發送簡訊給我
#6 引用回覆 回覆 發表時間:2004-11-18 01:32:08 IP:211.76.xxx.xxx 未訂閱
cubi 您好: 我猜測可能是因為在Assign後,相當於又將Bitmap的格式設成256色(精確的說是該來源Bitmap的原有格式),所以早先的PixleFormat也跟著被更改了。 所以每每在要處理圖像時,再次明確指定其PixleFormat似乎比較保險些。 RichTop 敬上 =====***** 把數學當工具,可以解決問題;將數學變能力,能夠發現並解決問題! =====#####
cubi
初階會員


發表:56
回覆:94
積分:35
註冊:2002-04-15

發送簡訊給我
#7 引用回覆 回覆 發表時間:2004-11-18 09:13:53 IP:203.187.xxx.xxx 未訂閱
嗯嗯~~原來是這樣地,小弟知道了,感謝前輩,謝謝
系統時間:2024-05-06 3:24:49
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!