472,345 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,345 software developers and data experts.

Picture Comparison Code Not Working Properly

5 2Bits
I am still very much a beginner in programming and am facing a problem with a Java code. I would be very thankful for suggestions and comments. The problem is as following:

I found a Java code online that is designed to compare two pictures and highlight the differences between them. However, the way the code works is by using a loop that compares the RGB value of each pixel of the two pictures and highlights it in a certain color if the value is equal. This however results in a problem that even the slightest shift in camera angle or lighting conditions between the two pictures leads to a different RGB value of the pixels making the code highlight them as a difference even though the pictures are mostly identical. The code is written below and i have added the link to photos showing examples of the outcome of the code. What would you suggest i change in the code? Thank you for your help in advance.

The code:

Expand|Select|Wrap|Line Numbers
  1. import java.awt.image.BufferedImage;
  2.     import java.io.File;
  3.     import java.io.IOException;
  4.  
  5.     import javax.imageio.ImageIO; 
  6. public class PictureOverlayTest {
  7.     /*
  8.      * Four variables, three for the wanted BufferedImages, one String for the
  9.      * Path of the third Image, which does not already exist.
  10.      */
  11.  
  12.     private BufferedImage image1;
  13.     private BufferedImage image2;
  14.     private BufferedImage image3;
  15.  
  16.     private String pathImage3;
  17.  
  18.     public PictureOverlayTest(String filePathAndName1, String filePathAndName2,
  19.             String filePathAndName3) throws IOException {
  20.         /*
  21.          * Constructor in order to keep this method reusable and clean. Needs
  22.          * three Strings. The paths and Filenames of all three images. Image 1
  23.          * and 2 should exist already, Image 3 will be created if all
  24.          * requirements are met. Constructor creates the first two buffered
  25.          * images, sets all needed variables and starts the checkAndCompare()
  26.          * method
  27.          */
  28.  
  29.         File file = new File(filePathAndName1);
  30.         this.image1 = ImageIO.read(file);
  31.  
  32.         file = new File(filePathAndName2);
  33.         this.image2 = ImageIO.read(file);
  34.  
  35.         this.pathImage3 = filePathAndName3;
  36.         checkAndCompare();
  37.     }
  38.  
  39.     private void checkAndCompare() throws IOException {
  40.         /*
  41.          * This function creates the Color blue, compares the sizes of both
  42.          * pictures and if they are the same, creates a third image. Then it
  43.          * loops through the two images and compares each pixel. If the pixels
  44.          * are the same, the third image gets a blue pixel at that point
  45.          */
  46.  
  47.         Color blue = Color.blue;
  48.         Color yellow = Color.yellow;
  49.  
  50.         if (image1.getHeight() == image2.getHeight()
  51.                 && image1.getWidth() == image2.getWidth()) {
  52.  
  53.             image3 = new BufferedImage(image1.getWidth(), image1.getHeight(),
  54.                     image1.getType());
  55.             for (int y = 0; y < image1.getHeight(); y++) {
  56.                 for (int x = 0; x < image1.getWidth(); x++) {
  57.  
  58.                     int colorImage1 = image1.getRGB(x, y);
  59.                     int colorImage2 = image2.getRGB(x, y);
  60.  
  61.                     if (colorImage1 == colorImage2) {
  62.  
  63.                         image3.setRGB(x, y, blue.getRGB());
  64.  
  65.                     } else {
  66.                               image3.setRGB(x, y, yellow.getRGB()); 
  67.                         // Whatever Color you want. By default it is black.
  68.  
  69.                     }
  70.  
  71.                 }
  72.             }
  73.             savePicture3();
  74.             System.out.println("Message: Image comparison is done");
  75.  
  76.         } else {
  77.  
  78.             System.out.println("Error: Image dimensions do not match");
  79.  
  80.         }
  81.  
  82.     }
  83.  
  84.     private void savePicture3() throws IOException {
  85.         /*
  86.          * This method saves the created Image into a file onto your computer.
  87.          * The if() statement is used to check if the file was successfully
  88.          * created, in order to avoid unwanted errors. Keep in mind, that you
  89.          * have to change the "bmp" in ImageIO.write() to whatever format you
  90.          * actually want
  91.          */
  92.  
  93.         File file = new File(pathImage3);
  94.         if (file.createNewFile()) {
  95.             ImageIO.write(image3, "bmp", file);
  96.         }
  97.     }
  98.  
  99. }
  100.  
  101. import java.io.IOException;
  102.  
  103. public class Main {
  104.  
  105.     public static void main(String[] args) {
  106.         // TODO Auto-generated method stub
  107.  
  108.         try {
  109.             PictureOverlayTest test = new PictureOverlayTest(
  110.                     "C:\\Users\\Rabee Taha\\Desktop\\Java Test Pics\\test1.png",
  111.                     "C:\\Users\\Rabee Taha\\Desktop\\Java Test Pics\\test2.png",
  112.                     "C:\\Users\\Rabee Taha\\Desktop\\Java Test Pics\\test3.png");
  113.         } catch (IOException e) {
  114.             // TODO Auto-generated catch block
  115.             e.printStackTrace();
  116.         }
  117.     }
  118.  
  119. }
  120.  
Here is the link to example images of the outcome ( https://postimg.cc/gallery/rkXfPr7 )
Jul 24 '21 #1
1 3641
dev7060
625 Expert 512MB
I am still very much a beginner in programming and am facing a problem with a Java code. I would be very thankful for suggestions and comments. The problem is as following:

I found a Java code online that is designed to compare two pictures and highlight the differences between them. However, the way the code works is by using a loop that compares the RGB value of each pixel of the two pictures and highlights it in a certain color if the value is equal. This however results in a problem that even the slightest shift in camera angle or lighting conditions between the two pictures leads to a different RGB value of the pixels making the code highlight them as a difference even though the pictures are mostly identical. The code is written below and i have added the link to photos showing examples of the outcome of the code. What would you suggest i change in the code? Thank you for your help in advance.
You can look into machine learning/artificial intelligence libraries. I believe there would be something related to image processing that could serve the purpose.
Jul 24 '21 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: weasel | last post by:
Why is the Farenheit to Celsius part not working properly? Instead of showing a similar range of what the farenheit is listing, the celsius portion...
3
by: Andrew | last post by:
I'm having a major problem with a databound listbox in C#. In the constructor for the form I am trying to pre-select some of the items based in...
8
by: Randy Yates | last post by:
I'm using mingw/g++ 3.3.3. When I use pos = tellg(), getline(), setg(pos), then the next getline() does NOT get from the original position. I've...
1
by: Roberto Castro | last post by:
I have some problems with the way I am showing the BLOB fields in the Image web controls. It does work on my localhost though sometimes I need to...
5
by: Nita Raju | last post by:
Hi, I have to validate a textbox for date without using the validation controls. So i had to use IsDate(). It's not working properly when i give...
2
by: Annu | last post by:
Hi I need help on <enbed> tag. Following code(No 1) is working properly on windows but on linux code no.2 is not working Code No 1: <EMBED...
6
by: Ptaku25 | last post by:
Hi all // my code var string1= "Pablo has 3 cats and 1 dog"; var string2= "Pablo has 3 cats"; var str1 = ""; var str2 = ""; str1 =...
12
kamill
by: kamill | last post by:
I have done a logout page for logout from admin section and provides a link to logout from admin section.Whenever i clicked on logout link it...
3
by: rajasree | last post by:
Hi all, am doing a project in PHP. my javascript code is working properly in ie. But its not working in firefox. Please help me my code is as...
1
by: robin1983 | last post by:
Dear All, I got stuck in simple problem, I have a two php file one for registration form and one for to check and insert into the table. The...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.