Skip to main content

Problem in traslating and cropping. Is a BUG??

1 reply [Last post]
giuseppe83
Offline
Joined: 2005-08-31

I have noted a strange behavior in the following code.
I take a subimage 100x100 from an image 256x256 rotated of 1 degree and evaluate the pixels' average.
After I take the 256x256 image, crop a 254x254 subimage in the center, translate it of 1 degree and then take a 100x100 subimage. Visively and teorically the image have to be the same but the pixel's everage is different! Can be this due to the 5043459 BUG?? Who can works around tho obtein two subimages with same pixels values?

-------CODE---------------------
public class prova {

/**
* @param args
*/
public static void main(String[] args) {

PlanarImage src=JAI.create("fileload","C:/IRApplication/dataset/boat/boatGray.jpg");
BufferedImage img=src.getAsBufferedImage();

//trasform the image
BufferedImage b=tr(img);
//extract a subimage 100x100 in the image's center and evaluate the pixels' average
int h = b.getHeight();
int w = b.getWidth();
int subX=(w-100)/2;
int subY=(h-100)/2;
int[] refPixels = null;
BufferedImage center1=b.getSubimage(subX,subY,100,100);
refPixels = center1.getData().getPixels(0, 0, center1.getWidth(),center1.getHeight(), refPixels);
System.out.println(evaluateAvg(refPixels));
display(center1);

//NOW TRY ANOTHER APPROACH; TEORICALLY IDENTICAL
//BEFORE TRASNFORMING I CROP THE ORIGINAL IMAGE(256x256)
//A TAKE AN IMAGE 254X254 in the center
int subX_crop=(w-254)/2;
int subY_crop=(h-254)/2;
BufferedImage sub =img.getSubimage(subX_crop,subY_crop,254,254);
BufferedImage sub_b=new BufferedImage(sub.getWidth(),sub.getHeight(),sub.getType());
sub_b.setData(sub.getData());
//TRANSFORM THE 254X254 image
BufferedImage subT=tr(sub_b);
//TAKE A 100X100 subimage in the center and evaluate
//pixels' average
int h2 = subT.getHeight();
int w2 = subT.getWidth();
int subX2=(w2-100)/2;
int subY2=(h2-100)/2;
int[] refPixels2 = null;
BufferedImage center2=subT.getSubimage(subX2,subY2,100,100);
refPixels2 = center2.getData().getPixels(0, 0, center2.getWidth(),center2.getHeight(), refPixels2);
System.out.println(evaluateAvg(refPixels2));
display(center2);
}

private static double evaluateAvg(int[] pixels) {
//media pixels Reference image
double avg = 0;
int area = pixels.length;
for (int i = 0; i < area; i++) {
avg += pixels[i];
}
avg /= area;
return avg;
}

//TRASNFORM THE IMAGE SRC
private static BufferedImage tr(BufferedImage src){
AffineTransform at=AffineTransform.getRotateInstance(Math.toRadians(1));
ParameterBlockJAI pb = new ParameterBlockJAI("affine");
pb.addSource(PlanarImage.wrapRenderedImage(src));
pb.setParameter("transform",at);
pb.setParameter("interpolation",Interpolation.getInstance(Interpolation.INTERP_BILINEAR));
PlanarImage img = (PlanarImage) JAI.create("affine", pb);
BufferedImage tr=new BufferedImage(src.getWidth(),src.getHeight(),src.getType());
BufferedImage b=img.getAsBufferedImage();
Raster r=b.getData();
tr.setData(r);
return tr;
}

private static void display(BufferedImage i){
JFrame f=new JFrame();
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.getContentPane().add(new DisplayJAI(i));
f.pack();
f.setVisible(true);

}

}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
giuseppe83
Offline
Joined: 2005-08-31

These are image's information

PlanarImage[minX=0 minY=0 width=256 height=256 tileGridXOffset=0 tileGridYOffset=0 tileWidth=256 tileHeight=256 sampleModel=java.awt.image.PixelInterleavedSampleModel@1000000 colorModel=ColorModel: #pixelBits = 8 numComponents = 1 color space = java.awt.color.ICC_ColorSpace@1697b67 transparency = 1 has alpha = false isAlphaPre = false]