簡體   English   中英

重寫圖像后為什么我沒有得到相同的值?

[英]Why don't I get the same values after rewriting an image?

我讀了一個圖像。讓我們說圖像“ img”,在三個獨立的int矩陣中讀取其RGB值。 我用新的名字寫了同一張圖片。 在三個新的int矩陣中讀取此“ simg”圖像的RGB值。 當比較它們的紅色矩陣的值時,我得到了不同的值。例如,請參見以下代碼:

        //    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);

        }
    }

我期望“計數”的值為零,因為兩者都是同一張圖片的副本! 但是我得到的輸出是“更改后的像素為678783”。 這怎么可能...?

因為JPEG是有損格式。 如果您使用非損耗格式(例如PNG)嘗試,則應該沒有任何區別。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM