Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Image overlay and comparison code error.

  1. #1
    Junior Member
    Join Date
    Jul 2021
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Image overlay and comparison code error.

    I am still very much a beginner in programming, and i am facing an issue with a code that i am hoping someone could help me with. I found the java code below for overlaying and comparing the pixels of two images in an online forum. When i try to compile the code i get a syntax error. Hope you can help me find the error. Some of the errors am getting:

    When trying to run the code i get a window saying "The selection cannot be launched, and there are no recent launches"
    next to the first code line "module Image_Overlay {" am getting the error "Syntax error on token(s), misplaced construct(s)"
    next to code line Nr. 7 am getting the error "The type java.awt.image.BufferedImage is not accessible"

    Thanks in advance.
    The code is as following:

    package stackexchange;
     
    import java.awt.Color;
    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;
     
            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 {
     
                            // 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);
            }
        }
     
    }
     
    package stackexchange;
     
    import java.io.IOException;
     
    public class Main {
     
        public static void main(String[] args) {
            // TODO Auto-generated method stub
     
            try {
                PictureOverlayTest test = new PictureOverlayTest(
                        "H:\\stackexchange\\file1.bmp",
                        "H:\\stackexchange\\file2.bmp",
                        "H:\\stackexchange\\file3.bmp");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
     
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Image overlay and comparison code error.

    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2021
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Picture Comparison Code Not Working Properly

    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 )
    Note: This question was also uploaded to: ( https://coderanch.com/t/744374/java/...rking-Properly )

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Image overlay and comparison code error.

    The posted code is missing an import statement and does not compile without an error.
    Please be sure to compile (and test) code before posting it.

    slightest shift in camera angle
    Are the objects in the images at the same locations? Is the difference between the images just lighting or shading?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jul 2021
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Image overlay and comparison code error.

    Thank you for your reply. I created a sepearte .java file for each of the two classes (PictureOverlayTest and Main) and use the cmd compiler to compile them and it works for me.
    The images are practically the same but with a very slight camera angle shift which leads the system to think that the two pics are different in many regions. I added a link to photos showing an example of how the code works.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Image overlay and comparison code error.

    I would think the images need to be aligned so that objects in one image line up with the objects in the other image.
    Otherwise a pixel-by-pixel compare does not make sense.

    cmd compiler to compile them and it works for me.
    The posted code in post#3 will not compile without errors because of the missing import statement.
    The code in post#1 has the import statement.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Reduce the image comparison time
    By RX9382N in forum Algorithms & Recursion
    Replies: 0
    Last Post: April 11th, 2019, 02:20 AM
  2. Reduce the image comparison time
    By RX9382N in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: April 11th, 2019, 12:56 AM
  3. Image comparison
    By klenam in forum The Cafe
    Replies: 1
    Last Post: March 18th, 2014, 07:40 AM
  4. Problem with image crop and pixel comparison code
    By tuathan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 29th, 2012, 09:08 AM
  5. Savings account balance comparison code.
    By Rhyssa6 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 10th, 2011, 06:57 AM

Tags for this Thread