delphi FMX图像的直方图匹配

xiaoxiao2021-02-28  7

//直方图匹配 pipei procedure pipei(b : TBitmap; var g : array of Integer); var x, y , I ,J: Integer; A_BMPData : TBitmapData ; p: PByteArray; n : array [0..255] of Integer; begin //初始化数组 c for I := 0 to 255 do begin n[i] := 0; end; //进行均衡 jh if b.Map( TMapAccess.ReadWrite, A_BMPData) then begin //统计出现的次数 t for y := 0 to A_BMPData.Height - 1 do begin p := A_BMPData.GetScanline(y); for x := 0 to A_BMPData.Width - 1 do begin n[p[x*4]] := n[p[x*4]]+1; end; end; //根据公式算出结果 直方图均衡 s for I := 1 to 255 do begin n[i] := n[i]+n[i-1]; n[i-1] := round(n[i-1]*255/b.Width/b.Height); end; //最后一定是最大的 无需计算 255 n[255] := 255; J := 1; //--------------------------------------------------------------------- //直方图匹配 pipei 由于其是递增的 s for I := 0 to 255 do begin //取最接近的值 由于其递增 中间值小于等于两边值 for J := J to 254 do begin if abs(n[i]-g[J])<abs(n[i]-g[J+1]) then begin n[i] := j; break; end; if (abs(n[i]-g[j]) <= abs(n[i]-g[j-1]))and (abs(n[i]-g[j]) < abs(n[i]-g[j+1])) then begin n[i] := j; break; end; //不符合上述条件 只有这一个值符合了 if j = 254 then n[i] := 255; end; end; //---------------------------------------------------------------------- //给图像赋值 f for y := 0 to A_BMPData.Height - 1 do begin p := A_BMPData.GetScanline(y); for x := 0 to A_BMPData.Width - 1 do begin p[x*4] := n[p[x*4]]; p[x*4+1] := p[x*4]; p[x*4+2] := p[x*4]; end; end; b.Unmap(A_BMPData); end; end; //一个按钮的 点击函数 procedure TForm3.Button2Click(Sender: TObject); var temp : array [0..255] of Integer; I: Integer; begin //根据预期简单的赋值 for I := 0 to 255 do begin if i<=16 then temp[i]:=i*8 else if (i<110) or (i>180) then temp[i] := temp[i-1]+1 else temp[i] := temp[i-1]; if temp[i]>255 then temp[i] := 255; end; pipei(imc.Bitmap,temp); end;

                                                                                                          

原图:             匹配:            均衡:  

转载请注明原文地址: https://www.6miu.com/read-1150195.html

最新回复(0)