为什么在重写图像后我没有获得相同的值?

时间:2015-04-27 13:50:41

标签: java image image-processing matrix

我已经阅读了一张图片..我们说图像' img',在三个独立的整数矩阵中读取它的RGB值。我用一个新名字写了相同的图像..让我们说图像' simg'。 读取这个' simg'的RGB值。三个新的int矩阵中的图像。当我比较红色矩阵的值时,我得到了不同的值。例如,请参阅以下代码:

        //    Reading two images and checking the differences
    package Steg_garage;

    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.util.Random;
    import javax.imageio.ImageIO;

    /**
     *
     * @author Rohit
     */
    public class difference {

            public static void main(String[] args)
        {

                BufferedImage img = null;

                File f = new File("flower.jpg"); 
                try {
                            img = ImageIO.read(f);
                    }
                catch(Exception e)
                    {
                            e.printStackTrace();
                    }
                int[] RGBarray = null;
                int c = 0;
                int r = 0;
                int [][] alphaPixels = null;
                int [][] redPixels = null;
                int [][] greenPixels = null;
                int [][] bluePixels =null;

                c = img.getWidth();
                r = img.getHeight();

                RGBarray = img.getRGB(0,0,c,r,null,0,c);   
                alphaPixels = new int [r][c];
                redPixels = new int [r][c];
                greenPixels = new int [r][c];
                bluePixels = new int [r][c];
                int ii = 0;// to run inside seperating a loop

                for(int row=0; row<r; row++)
                {
                    for(int col=0; col<c; col++)
                    {
                        alphaPixels[row][col] = ((RGBarray[ii]>>24)&0xff);
                        redPixels[row][col] = ((RGBarray[ii]>>16)&0xff);
                        greenPixels[row][col] = ((RGBarray[ii]>>8)&0xff);
                        bluePixels[row][col] = (RGBarray[ii]&0xff);
                        ii++;
                    }
                }
                int code_length = 5;
                int value = 32;

                for(int row = r-1,  col = 0;col < code_length  ; col++ )
                {
                    redPixels[row][col] = value++;
                }


                int rgba;
                for(int row=0; row<r; row++)
                {
                    for(int col=0; col<c; col++)
                    {
                        rgba = (alphaPixels[row][col] & 0xff) << 24 | (redPixels[row][col] & 0xff) << 16 | (greenPixels[row][col] & 0xff) << 8 | (bluePixels[row][col] & 0xff);
                        img.setRGB(col, row, rgba);
                    }
                }
                try{
                        ImageIO.write(img, "jpg", new File("change"+".jpg"));
                    }
                catch(Exception e)
                {
                    e.printStackTrace();
                }

      // Reading the written image/////////////          

            BufferedImage simg = null;

                File sf = new File("change.jpg"); 
                try {
                            simg = ImageIO.read(sf);
                    }
                catch(Exception e)
                    {
                            e.printStackTrace();
                    }


            int[] sRGBarray = null;
            int sc = 0;
            int sr = 0;
            int [][] salphaPixels = null;
            int [][] sredPixels = null;
            int [][] sgreenPixels = null;
            int [][] sbluePixels =null;

            sc = simg.getWidth();
            sr = simg.getHeight();

            sRGBarray = simg.getRGB(0,0,sc,sr,null,0,sc);   
            salphaPixels = new int [sr][sc];
            sredPixels = new int [sr][sc];
            sgreenPixels = new int [sr][sc];
            sbluePixels = new int [sr][sc];
            int sii = 0;// to run inside seperating a loop

            for(int row=0; row<sr; row++)
            {
                for(int col=0; col<sc; col++)
                {
                    salphaPixels[row][col] = ((sRGBarray[sii]>>24)&0xff);
                    sredPixels[row][col] = ((sRGBarray[sii]>>16)&0xff); 
                    sgreenPixels[row][col] = ((sRGBarray[sii]>>8)&0xff);
                    sbluePixels[row][col] = (sRGBarray[sii]&0xff);
                    sii++;
                }
            }

            System.out.println(" The selected image height is " + img.getHeight()+ " and its width is " + img.getWidth());
            System.out.println(" The steganographed image height is " + img.getHeight()+ " and its width is " + simg.getWidth());


            int count = 0;
            int tcount = 0;
            for(int row=0; row<r; row++)
                {
                    for(int col=0; col<c; col++)
                    {
                        tcount++;
                        if( redPixels[row][col] != sredPixels[row][col])
                          count++;  
                    }
                }

            System.out.println(" The changed pixels are " + count);
            System.out.println(" The total number of pixels are " + tcount);

        }
    }

我期待着&#39;计数&#39;为零,因为两者都是同一图像的副本!但我得到的输出是 &#34;改变的像素是678783&#34;。这怎么可能......?

1 个答案:

答案 0 :(得分:4)

因为JPEG是一种有损格式。如果您使用非损耗格式(例如PNG)进行尝试,则不应存在任何差异。