圖片放大問題-使用內差法 |
尚未結案
|
cubi
初階會員 發表:56 回覆:94 積分:35 註冊:2002-04-15 發送簡訊給我 |
請教各位前輩 小弟參考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 發送簡訊給我 |
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 發送簡訊給我 |
cubi 您好: 依您上傳的程式重建後測試發現:
在256色Bitmap圖檔下,的確會出現問題,建議是:在TrackBar1Change(.)中,再一次的設定PixelFormat,如下紅色部分。
至於沒有跟著放大?我測試時似乎沒有這個現象。
上述結果供您參考。
引言:RichTop 敬上 =====***** 把數學當工具,可以解決問題;將數學變能力,能夠發現並解決問題! =====#####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; 請問前輩,小弟是那兒出錯了,放大後的圖,變成彩色的,而且似乎 並沒有按比例放大,放大後,右邊有被切掉 謝謝,謝謝 |
japhenchen
高階會員 發表:51 回覆:444 積分:184 註冊:2003-07-23 發送簡訊給我 |
|
cubi
初階會員 發表:56 回覆:94 積分:35 註冊:2002-04-15 發送簡訊給我 |
|
richtop
資深會員 發表:122 回覆:646 積分:468 註冊:2003-06-10 發送簡訊給我 |
|
cubi
初階會員 發表:56 回覆:94 積分:35 註冊:2002-04-15 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |