请教高手,帮我把这个算法转成JAVA,谢谢!本人急用。
//************************************************
//计算图像的重心矩特征
//************************************************
m00:=0;
m01:=0;
m10:=0;
for i:=0 to Bmp.Height-1 do
begin
p := Bmp.scanline[i];
for j:=0 to Bmp.Width-1 do
begin
Color:=Round(p[j * 3 + 2] * 0.3 + p[j * 3 + 1] * 0.59 + p[j
* 3] * 0.11); //(i,j)上的像素值
Value:=Color;
m00:=m00+Value; //0次矩
temp:=j*Value;
m01:=m01+temp; //x方向的一次矩
temp:=i*Value;
m10:=m10+temp; //y方向的一次矩
end;
end;
BX:=m01/m00+0.5; //计算重心,(BX,BY)即为重心所在位置
BY:=m10/m00+0.5;
nBM:= 0;
for i:=0 to Bmp.Height-1 do //计算重心矩 nBW即为重心矩
begin
p := Bmp.scanline[i];
for j:=0 to Bmp.Width-1 do
begin
Color:=Round(p[j * 3 + 2] * 0.3 + p[j * 3 + 1] * 0.59 + p[j
* 3] * 0.11);
Value:=Color;
Flag:=(j-BX)*(i-BY);
Flag:=Flag*Value;
nBM:=nBM+Flag;
end;
end;
Bmp.Free;
Result:=nBM;
//********************************************************
//纹理特征提取源程序
//********************************************************
function TFrmAttr.Texture_Attribute(filename: String): Double;
var
Bmp1,Bmp2: TBitmap;
i,j,m,c1,c2,p,QW:Integer;
pb1,pb2: PByteArray;
begin
Bmp1 := TBitmap.Create; //源二值图像
Image2.Picture.Bitmap.LoadFromFile(filename);
Bmp1.Assign(Image2.Picture.Bitmap);
Bmp2 := TBitmap.Create; //闭运算后的图像
Image1.Picture.Bitmap.LoadFromFile(filename);
Bmp2.Assign(Image1.Picture.Bitmap);
//先对图像进行形态学闭运算 ,方法是先膨胀后腐蚀
PictureTwoValue(Bmp1); // 对图像进行二值化
PictureTwoValue(Bmp2);
if (BitmapDilate(Bmp2, False)) then //对图像进行膨胀
begin
image1.Picture.Assign(Bmp2);
end
else
showmessage('膨胀失败');
if (BitmapErose(Bmp2, True)) then //对图像进行腐蚀
begin
image1.Picture.Assign(Bmp2);
end
else
showmessage('腐蚀失败');
Image1.Picture.Bitmap.Assign(Bmp2);
Image2.Picture.Bitmap.Assign(Bmp1);
//image1为闭运算后的结果
//image2为源二值图像
//提取图像的纹理特征的方法是用原二值图像减去闭运算后的图像,
//下面开始做提取纹理特征的操作
m:=0;
for i:=0 to Bmp1.Height-1 do
begin
pb1 := Bmp1.scanline[i];
pb2 := Bmp2.scanline[i];
for j:=0 to Bmp1.Width-1 do
begin
c1:=Round(pb1[j * 3 + 2] * 0.3 + pb1[j * 3 + 1] * 0.59 + pb1[j
* 3] * 0.11); //(i,j)上的像素值
c2:=Round(pb2[j * 3 + 2] * 0.3 + pb2[j * 3 + 1] * 0.59 + pb2[j
* 3] * 0.11); //(i,j)上的像素值
p:=abs(c1-c2); //像素差
if p=255 then
p:=1
else
p:=0;
m:=m+i*p;
end;
end;
QW:=m; //期望值
{for i:=1 to Bmp1.Height do
begin
for j:=1 to Bmp1.Width do
begin
c1:=Bmp1.Canvas.Pixels[i,j];
c2:=Bmp2.Canvas.Pixels[i,j];
p:=abs(c1-c2);
n:=n+(i-QW)*(i-QW)*p;//方差值
end;
end; }
result:=QW;
end;
end;//**************************************************
计算属性的信息增益
TrainCount:测试集总数
countArr:保存各个类别的个数的数组(ci个数)
AttriArr:保存属性的不同取值时测试集个数
TAArr:类别属性测试集个数
***************************************************
function TForm1.GainA(TrainCount: Integer;
CountArr: array of integer;AttriArr:array of Integer;TAArr:TArr;i:Integer): Double;
var
entropy,Aentr,gain,temp:Double;
idx:Integer;
begin
Aentr:=0;
entropy:=Form1.Entropy(CountArr,TrainCount); for idx:=low(AttriArr) to high(AttriArr) do
begin
temp:=Form1.Entropy(TAArr[i][idx],AttriArr[idx]);
Aentr:=Aentr+(AttriArr[idx] / TrainCount)*temp;
end;
gain:=entropy-Aentr;
Result:=gain;
end;
下面代码是计算信息熵:
function TForm1.Entropy(TypeCountArr: array of Integer;
TrainCount: Integer): Double;
var
entropy:Double;
idx:Integer;
pi:Double;
begin
entropy:=0;
for idx:=0 to 6 do
begin
pi:=(TypeCountArr[idx]) / TrainCount;
entropy:=entropy+pi*log2(pi);
end;
entropy:=-entropy; //计算熵
result:=entropy;
end;
//************************************************
//计算图像的重心矩特征
//************************************************
m00:=0;
m01:=0;
m10:=0;
for i:=0 to Bmp.Height-1 do
begin
p := Bmp.scanline[i];
for j:=0 to Bmp.Width-1 do
begin
Color:=Round(p[j * 3 + 2] * 0.3 + p[j * 3 + 1] * 0.59 + p[j
* 3] * 0.11); //(i,j)上的像素值
Value:=Color;
m00:=m00+Value; //0次矩
temp:=j*Value;
m01:=m01+temp; //x方向的一次矩
temp:=i*Value;
m10:=m10+temp; //y方向的一次矩
end;
end;
BX:=m01/m00+0.5; //计算重心,(BX,BY)即为重心所在位置
BY:=m10/m00+0.5;
nBM:= 0;
for i:=0 to Bmp.Height-1 do //计算重心矩 nBW即为重心矩
begin
p := Bmp.scanline[i];
for j:=0 to Bmp.Width-1 do
begin
Color:=Round(p[j * 3 + 2] * 0.3 + p[j * 3 + 1] * 0.59 + p[j
* 3] * 0.11);
Value:=Color;
Flag:=(j-BX)*(i-BY);
Flag:=Flag*Value;
nBM:=nBM+Flag;
end;
end;
Bmp.Free;
Result:=nBM;
//********************************************************
//纹理特征提取源程序
//********************************************************
function TFrmAttr.Texture_Attribute(filename: String): Double;
var
Bmp1,Bmp2: TBitmap;
i,j,m,c1,c2,p,QW:Integer;
pb1,pb2: PByteArray;
begin
Bmp1 := TBitmap.Create; //源二值图像
Image2.Picture.Bitmap.LoadFromFile(filename);
Bmp1.Assign(Image2.Picture.Bitmap);
Bmp2 := TBitmap.Create; //闭运算后的图像
Image1.Picture.Bitmap.LoadFromFile(filename);
Bmp2.Assign(Image1.Picture.Bitmap);
//先对图像进行形态学闭运算 ,方法是先膨胀后腐蚀
PictureTwoValue(Bmp1); // 对图像进行二值化
PictureTwoValue(Bmp2);
if (BitmapDilate(Bmp2, False)) then //对图像进行膨胀
begin
image1.Picture.Assign(Bmp2);
end
else
showmessage('膨胀失败');
if (BitmapErose(Bmp2, True)) then //对图像进行腐蚀
begin
image1.Picture.Assign(Bmp2);
end
else
showmessage('腐蚀失败');
Image1.Picture.Bitmap.Assign(Bmp2);
Image2.Picture.Bitmap.Assign(Bmp1);
//image1为闭运算后的结果
//image2为源二值图像
//提取图像的纹理特征的方法是用原二值图像减去闭运算后的图像,
//下面开始做提取纹理特征的操作
m:=0;
for i:=0 to Bmp1.Height-1 do
begin
pb1 := Bmp1.scanline[i];
pb2 := Bmp2.scanline[i];
for j:=0 to Bmp1.Width-1 do
begin
c1:=Round(pb1[j * 3 + 2] * 0.3 + pb1[j * 3 + 1] * 0.59 + pb1[j
* 3] * 0.11); //(i,j)上的像素值
c2:=Round(pb2[j * 3 + 2] * 0.3 + pb2[j * 3 + 1] * 0.59 + pb2[j
* 3] * 0.11); //(i,j)上的像素值
p:=abs(c1-c2); //像素差
if p=255 then
p:=1
else
p:=0;
m:=m+i*p;
end;
end;
QW:=m; //期望值
{for i:=1 to Bmp1.Height do
begin
for j:=1 to Bmp1.Width do
begin
c1:=Bmp1.Canvas.Pixels[i,j];
c2:=Bmp2.Canvas.Pixels[i,j];
p:=abs(c1-c2);
n:=n+(i-QW)*(i-QW)*p;//方差值
end;
end; }
result:=QW;
end;
end;//**************************************************
计算属性的信息增益
TrainCount:测试集总数
countArr:保存各个类别的个数的数组(ci个数)
AttriArr:保存属性的不同取值时测试集个数
TAArr:类别属性测试集个数
***************************************************
function TForm1.GainA(TrainCount: Integer;
CountArr: array of integer;AttriArr:array of Integer;TAArr:TArr;i:Integer): Double;
var
entropy,Aentr,gain,temp:Double;
idx:Integer;
begin
Aentr:=0;
entropy:=Form1.Entropy(CountArr,TrainCount); for idx:=low(AttriArr) to high(AttriArr) do
begin
temp:=Form1.Entropy(TAArr[i][idx],AttriArr[idx]);
Aentr:=Aentr+(AttriArr[idx] / TrainCount)*temp;
end;
gain:=entropy-Aentr;
Result:=gain;
end;
下面代码是计算信息熵:
function TForm1.Entropy(TypeCountArr: array of Integer;
TrainCount: Integer): Double;
var
entropy:Double;
idx:Integer;
pi:Double;
begin
entropy:=0;
for idx:=0 to 6 do
begin
pi:=(TypeCountArr[idx]) / TrainCount;
entropy:=entropy+pi*log2(pi);
end;
entropy:=-entropy; //计算熵
result:=entropy;
end;
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货