3

i am doing work on images using java. i read a gray scale image and convert the pixel values to 0 and 1 i got the output image correctly for only some images. in others some image portions are lost here is the code i am using to make the image array back into image

`BufferedImage I = ImageIO.read(new File("path"));
 SampleModel sampleModel;
 Raster pixelData;
 pixelData = I.getData();
 int[][] pixels=new int[wid][hgt];
 sampleModel=pixelData.getSampleModel();



 BufferedImage image=new BufferedImage(wid,hgt,BufferedImage.TYPE_BYTE_BINARY);
 WritableRaster raster=Raster.createWritableRaster(sampleModel,new Point(0,0));
     for(int i=0;i<wid;i++)
     {
         for(int j=0;j<hgt;j++)
         {
            raster.setSample(i,j,0,pixels[i][j]);
         }
     }
     image.setData(raster);

File output=new File("path");
    ImageIO.write(image,"png",output);
 System.out.println("..End..");`

size of the image is same as original but the entire size contains only a portion of original image.can u help me

JithPS
  • 1,167
  • 1
  • 7
  • 19

2 Answers2

1

Your problem is probably related to the sample model you are using. The sample model is responsible to describe how the Raster is going to store the data, maybe you are using a model that puts more info per pixel and then the image gets only a part of the original buffer.

Cheers

[Update] @Joop Egen is correct you need to use the sample model from the image in which you defined that you are using a grayscale byte per pixel "configuration"

prmottajr
  • 1,816
  • 1
  • 13
  • 22
  • SampleModel sampleModel; sampleModel=pixelData.getSampleModel(); i have used the following code to get the samplemodel is ther any other options? – JithPS Dec 27 '13 at 08:15
  • 1
    @user3133364: you need two `SampleModel`s; take `image.getSampleModel()`. – Joop Eggen Dec 27 '13 at 10:44
  • can u give me an example illustrating this – JithPS Dec 27 '13 at 11:34
  • @ Joop Eggen why i need 2 SampleModel s. – JithPS Dec 27 '13 at 11:41
  • i have used Image.getSampleModel() but it didnt make any difference – JithPS Dec 27 '13 at 11:42
  • i have used many sample model techniques likePixelInterleavedSampleModel ,SinglePixelPackedSampleModel .But yet the code works well for only some images .For rest, image portions get lost.why this happening? – JithPS Dec 27 '13 at 19:47
  • i found that the probelm occurs only for 24 bit depth images .8 bit depth gryascale images work perfectly. how can i solve this problem????????? – JithPS Dec 28 '13 at 07:40
  • @user3133364 when you created you image buffer you specified byte binary and tths will wor only for 8 bits grayscale. Check the javadoc for imagebuffer and see the other modes. You will have to create your imagebuffer accordingly with tge type of image you will be using. – prmottajr Dec 28 '13 at 10:09
  • @prmottajr There is no image buffer type for setting 3 bytes per pixel fo 24 bit depth grayscale. what to do then??? – JithPS Dec 30 '13 at 17:05
  • @user3133364 You can try TYPE_3BYTE_BGR which uses 1 byte per color channel, just beware that it is "reversed" in the sense that the channels are Blue, Green and Red. Take a look at the BufferedImage javadoc here http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/image/BufferedImage.html – prmottajr Dec 30 '13 at 17:16
0

got a gud answer for my problem it worked well for all images(including 24 bit and 8 bit images)

 BufferedImage I = ImageIO.read(new File("path"));
 Raster pixelData;
 pixelData = I.getData();
 int pixels[][]=new int[wid][hgt];


     for ( x=0;x<wid;x++)
     {
         for( y=0;y<hgt;y++)
         {
             pixels[x][y]=pixelData.getSample(x,y,0);
          }  
     }


 BufferedImage image=new BufferedImage(wid,hgt,BufferedImage.TYPE_BYTE_BINARY);
 ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
 int[] nBits = { 8 };
 ColorModel cm = new ComponentColorModel(cs, nBits, false, true,Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
 SampleModel sm = cm.createCompatibleSampleModel(wid, hgt);
 WritableRaster raster=Raster.createWritableRaster(sm,new Point(0,0));
     for(int i=0;i<wid;i++)
     {
         for(int j1=0;j1<hgt;j1++)
         {
            raster.setSample(i,j1,0,pixels[i][j1]);
         }
     }
  image.setData(raster);
  File output=new File("path");
  ImageIO.write(image,"png",output);` 
JithPS
  • 1,167
  • 1
  • 7
  • 19