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: -
import java.awt.image.BufferedImage;
-
import java.io.File;
-
import java.io.IOException;
-
-
import javax.imageio.ImageIO;
-
public class PictureOverlayTest {
-
/*
-
* Four variables, three for the wanted BufferedImages, one String for the
-
* Path of the third Image, which does not already exist.
-
*/
-
-
private BufferedImage image1;
-
private BufferedImage image2;
-
private BufferedImage image3;
-
-
private String pathImage3;
-
-
public PictureOverlayTest(String filePathAndName1, String filePathAndName2,
-
String filePathAndName3) throws IOException {
-
/*
-
* Constructor in order to keep this method reusable and clean. Needs
-
* three Strings. The paths and Filenames of all three images. Image 1
-
* and 2 should exist already, Image 3 will be created if all
-
* requirements are met. Constructor creates the first two buffered
-
* images, sets all needed variables and starts the checkAndCompare()
-
* method
-
*/
-
-
File file = new File(filePathAndName1);
-
this.image1 = ImageIO.read(file);
-
-
file = new File(filePathAndName2);
-
this.image2 = ImageIO.read(file);
-
-
this.pathImage3 = filePathAndName3;
-
checkAndCompare();
-
}
-
-
private void checkAndCompare() throws IOException {
-
/*
-
* This function creates the Color blue, compares the sizes of both
-
* pictures and if they are the same, creates a third image. Then it
-
* loops through the two images and compares each pixel. If the pixels
-
* are the same, the third image gets a blue pixel at that point
-
*/
-
-
Color blue = Color.blue;
-
Color yellow = Color.yellow;
-
-
if (image1.getHeight() == image2.getHeight()
-
&& image1.getWidth() == image2.getWidth()) {
-
-
image3 = new BufferedImage(image1.getWidth(), image1.getHeight(),
-
image1.getType());
-
for (int y = 0; y < image1.getHeight(); y++) {
-
for (int x = 0; x < image1.getWidth(); x++) {
-
-
int colorImage1 = image1.getRGB(x, y);
-
int colorImage2 = image2.getRGB(x, y);
-
-
if (colorImage1 == colorImage2) {
-
-
image3.setRGB(x, y, blue.getRGB());
-
-
} else {
-
image3.setRGB(x, y, yellow.getRGB());
-
// Whatever Color you want. By default it is black.
-
-
}
-
-
}
-
}
-
savePicture3();
-
System.out.println("Message: Image comparison is done");
-
-
} else {
-
-
System.out.println("Error: Image dimensions do not match");
-
-
}
-
-
}
-
-
private void savePicture3() throws IOException {
-
/*
-
* This method saves the created Image into a file onto your computer.
-
* The if() statement is used to check if the file was successfully
-
* created, in order to avoid unwanted errors. Keep in mind, that you
-
* have to change the "bmp" in ImageIO.write() to whatever format you
-
* actually want
-
*/
-
-
File file = new File(pathImage3);
-
if (file.createNewFile()) {
-
ImageIO.write(image3, "bmp", file);
-
}
-
}
-
-
}
-
-
import java.io.IOException;
-
-
public class Main {
-
-
public static void main(String[] args) {
-
// TODO Auto-generated method stub
-
-
try {
-
PictureOverlayTest test = new PictureOverlayTest(
-
"C:\\Users\\Rabee Taha\\Desktop\\Java Test Pics\\test1.png",
-
"C:\\Users\\Rabee Taha\\Desktop\\Java Test Pics\\test2.png",
-
"C:\\Users\\Rabee Taha\\Desktop\\Java Test Pics\\test3.png");
-
} catch (IOException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
-
}
-
Here is the link to example images of the outcome ( https://postimg.cc/gallery/rkXfPr7 )
1 3785
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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 is showing half the range of farenheit.
print...
|
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 information in the database. When I step through the...
|
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 tried doing
a clear() before the seekg() to no...
|
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 hit Refresh for the images to load properly....
|
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 "11//2004".
When i enter the above date it...
|
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 type='application/x-mplayer2'
...
|
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 = string1.match(string1, "g");
|
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 redirected to index.php of admin section......BUT when i...
|
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 follows;
<script language="javascript"...
|
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 problem is that when I get any kind error in...
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |