0

In this code I have taken an image as input and output the same image. As far I know if two images are same then their PSNR value will be inf. So I calculate their PSNR value using MATLAB but it shows 48.05 that means those image are not same. But I read and write the same image, why this is happening. How can I fix it?

public class ImageProcessing {

     BufferedImage image = null;
     int width;
     int height;

     public ImageProcessing() {

         // Input the image
         try {
             File input = new File("image0.jpg");
             image = ImageIO.read(input);
             width = image.getWidth();
             height = image.getHeight();


             /*int count = 0;
             for (int i = 0; i < height; i++) {
                 for (int j = 0; j < width; j++) {
                     count++;
                     Color c = new Color(image.getRGB(j, i));
                     System.out.println("S.No: " + count + " Red: " + c.getRed() + "  Green: " + c.getGreen() + " Blue: " + c.getBlue());
                 }
             }*/
         } catch (Exception e) {
             System.out.println("Error: " + e);
         }

         // Output the image
         try {
             File input = new File("image1.jpg");
             ImageIO.write(image, "jpg", input);
             System.out.println("Writing complete.");
         } catch (Exception e) {
             System.out.println("Error: " + e);
         }
     }

     public static void main(String[] args) {
         // TODO code application logic here
         System.out.println("System Start");

         ImageProcessing obj = new ImageProcessing();
     }
 }

2 Answers 2

1

JPG is a lossy format, You will lose information reading it in and writing it out each time you save it.

Perhaps try using a non-lossy format such as PNG or GIF.

Sign up to request clarification or add additional context in comments.

Comments

1

Right, to go off of Salmans comment, with lossless compression, every bit of data originally in the file remains after its uncompressed. This is generally used for txt documents or spreadsheet files where information can be lost such as financial data. PNG or GIF use lossless compression. What would be the advantage of using loss compression? It makes the file smaller by removing redundant information. Typically the user doesn't notice it and is used in sound and video. JPEG uses lossy compression and usually the creator can decide how much loss to introduce to trade-off between file size and image quality.

Drawn mostly from: http://whatis.techtarget.com/definition/lossless-and-lossy-compression

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.