1

I am working on a project related to colored image manipulation using JAVA.

I got to know the conversion of the colored image into a matrix using a getSample method of Raster class,

pixels[x][y]=raster.getSample(x,y,0);

I got the matrix in pixels[][] (only the values in red band). Then i converted the matrix back to image using WritableRaster as,

raster.setSample(i,j,0,pixels[i][j]);

I converted it to image using,

*BufferedImage image=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB); image.setData(raster);*

But the problem is,

1) I want the colored image to be displayed as it is whereas i am getting only a particular band(like only red, only blue . . ) because I have to specify a band as per the prototype of the method setSample and getenter code hereSample.

2) How can i get a 2d matrix representing a colored image(of all 3 bands represented in 3 different matrices)

Here is the code that i wrote with the help of snippets of code online...



import java.awt.Image;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.awt.image.SampleModel;
import java.awt.image.WritableRaster;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;


class TestImage {
    ImageIcon icon;
    SampleModel sampleModel;
    public static void main(String args[]){
        TestImage mamu = new TestImage();
        File file = new File("photo.jpg");
        mamu.compute(file);
    }

    public void compute(File file){
        try{
            BufferedImage img= ImageIO.read(file);
            Raster raster=img.getData();
            sampleModel = raster.getSampleModel();
            int w=raster.getWidth(),h=raster.getHeight();
            int pixels[][]=new int[w][h];
            for (int x=0;x<w;x++){
                for(int y=0;y<h;y++){
                    pixels[x][y]=raster.getSample(x,y,0);
                }
            }
            Image image = getImage(pixels);
            JFrame frame = new JFrame("uff");
            ImageIcon icon = new ImageIcon(image);
            JLabel label = new JLabel(icon);
            frame.setContentPane(label);
            frame.setVisible(true);
            frame.setSize(200,200);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public java.awt.Image getImage(int pixels[][]){
         int w=pixels.length;
         int h=pixels[0].length;
         WritableRaster raster= Raster.createWritableRaster(sampleModel, new Point(0,0));
         for(int i=0;i<w;i++){
             for(int j=0;j<h;j++){
                 raster.setSample(i,j,0,pixels[i][j]);
             }
         }
         BufferedImage image=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
         image.setData(raster);
         File output=new File("check.jpg");
         try {
             ImageIO.write(image,"jpg",output);
         }catch (Exception e){
             e.printStackTrace();
         }
         return image;
    }
}
Ashot Karakhanyan
  • 2,804
  • 3
  • 23
  • 28
Akash
  • 85
  • 1
  • 12
  • In which form do you need the pixels? This could be rather easy if you could work with the pixels as a (1D) int[] array... – Marco13 Feb 13 '14 at 19:52
  • @Marco13 I am working on an image compression project for which i need three 2d matrices of three different bands. On each of it ,i should apply compression algorithm ,later combine the three matrices and form a image to display. – Akash Feb 14 '14 at 20:35
  • yeah right @Marco13 , even that can serve the purpose – Akash Jan 22 '15 at 12:26

1 Answers1

2

You may be looking or java.awt.image.LookupOp, which uses a java.awt.image.LookupTable to modify bands en bloc. Several examples are cited here. The image below illustrates inversion:

short[] invert = new short[256];
for (int i = 0; i < 256; i++) {
    invert[i] = (short) (255 - i);
}
BufferedImageOp invertOp = new LookupOp(new ShortLookupTable(0, invert), null));
invertOp.filter(src, dst);

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045