MATLAB values: [ 33 105 9 ] from the png file
Java values: [ 101 172 53 ] after internal processing
C:\ece595_06\image>java Test2 BufferedImage@4aaa045f: type = 10 ColorModel: #pixelBits = 8 numComponents = 1 color space = java.awt.color.ICC_ColorSpace@60051f44 transparency = 1 has alpha = false isAlphaPre = false ByteInterleavedRaster: width = 360 height = 120 #numDataElements 1 dataOff[0] = 0 Type: BYTE_GRAY ColorSpace Type: 6 This is CS_sRGB: false pixel ff656565 java.awt.Color[r=101,g=101,b=101] fraction 101.000 pixel ffacacac java.awt.Color[r=172,g=172,b=172] fraction 172.000 pixel ff353535 java.awt.Color[r=53,g=53,b=53] fraction 53.0000 Sum 326.000
Test2.javaimport java.awt.*;
import java.awt.image.*;
import java.awt.color.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.JFrame;
// MATLAB: 0.2989 * R + 0.5870 * G + 0.1140 * B
public class Test2 {
public static void infoImage(BufferedImage img) {
System.out.println(img);
System.out.println("Type: " + getTypeString(img.getType()));
ColorModel model = img.getColorModel();
ColorSpace space = model.getColorSpace();
System.out.println("ColorSpace Type: " + space.getType());
System.out.println("This is CS_sRGB: " + space.isCS_sRGB());
}
public static String getTypeString(int type) {
switch (type) {
case 0: return "CUSTOM";
case 1: return "INT_RGB";
case 5: return "3BYTE_BGR";
case 10: return "BYTE_GRAY";
default: return "UNKNOWN";
}
}
public static void main(String[] args) {
String filename = "gray.png";
if (args.length>0) filename = args[0];
showImage f1 = new showImage(filename);
f1.setLocation(100,100);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int w = 120;
int p;
Color c;
double frac, sum=0.0;
infoImage(f1.img);
for (int n=0; n<3; n++) {
p = f1.img.getRGB(60+w*n,60);
c = new Color(p);
System.out.format("pixel %x%n",p);
System.out.println(c);
frac = c.getRed();
System.out.format("fraction %g%n",frac);
sum = sum + frac;
}
System.out.format("Sum %g%n",sum);
}
}
Maintained by John Loomis, updated Thu Oct 10 11:30:25 2013