我当时正在做一个简单的图像处理任务,我在Matlab上原型化了这个任务,然后制作了一个到Java的端口。在Java上执行任务后,我注意到了不同的结果。这是一个特定于平台的问题吗?
到目前为止,我所做的任务包括获取图像的绿色通道,计算具有一定范围值的像素数的百分比,以及计算该通道的算术平均值。代码和结果如下:
Matlab软件
a = imread('large.jpg');
g = a(:,:,2);
[x,y] = size(b);
count = 0;
for i=1:x
for j=1:y
if g(i,j) < 90 & g(i,j) > 30
count = count + 1;
%Mistake part for arithmetic mean. Thanks to james_alvarez
g(i,j) = 255;
end
end
end
count
(count/(3000*2400))*100
mean2(g)
----------
1698469
23.5898
165.9823
Java语言
BufferedImage img = ImageIO.read(Function.class.getResource("large.jpg"));
int gCount = 0;
int meanCount = 0;
int w = img.getWidth();
int h = img.getHeight();
for(int i = 0 ; i < w ; i++){
for(int j = 0 ; j< h ; j++){
Color c = new Color(img.getRGB(i,j));
int g = c.getGreen();
meanCount += g;
if(g < 90 && g > 30)
gCount++;
}
}
System.out.println(gCount);
System.out.println((gCount/(3000.0*2400.0))*100.0);
System.out.println(meanCount/(3000*2400));
----------
1706642
23.70336111111111
121
为什么他们给出了截然不同的结果?