Image overlay and comparison code error.

Discussion in 'Programming' started by Rabee Taha, Jul 1, 2021.

  1. #1
    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. Here is the link to the three images showing the errors that i am getting ( https://postimg.cc/gallery/fJZW2k5 ). Thanks in advance.

    The code:

    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();
    }
    }

    }
     
    Rabee Taha, Jul 1, 2021 IP
  2. derhau

    derhau Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #2
    I want to look at the pixels in two comparable pictures, createing a saved third picture where the pixels that are a similar between the two are become blue. The code likewise needs to make sure that the pictures are of a similar size prior to starting the examination, and ways out with a mistake to the control center on the off chance that they are not a similar size. Here is the code I have. I actually need to overlay the two pictures and turn the comparable pixels blue. I really want to utilize Filechooser.pickAFile yet other than that I am totally lost regarding how to go about it.
    import java.awt.Color;

    public class PP2kthoma34
    {

    public static void principal (String[] args)
    {
    // Unique picture
    Picture p1;//make the variable
    String fileName = FileChooser.pickAFile();
    FileChooser.setMediaPath ( fileName );
    System.out.println (fileName);
    p1 = new Picture( fileName );

    // Width and length of unique picture
    int width1 = p1.getWidth();
    int height1 = p1.getHeight();
    Pixel[] pixelArray1 = p1.getPixels();
    System.out.println(pixelArray1.length + "pixels");
    System.out.println("");

    // Changed picture
    Picture p2;//make the variable
    String filename2;
    filename2 = FileChooser.pickAFile();
    FileChooser.setMediaPath ( filename2 );
    System.out.println (filename2);
    p2 = new Picture( filename2 );

    //Width and Height of controlled picture
    int width2 = p2.getWidth();
    int height2 = p2.getHeight();
    Pixel[] pixelArray2 = p2.getPixels();
    System.out.println(pixelArray2.length + "pixels");
    System.out.println("");

    //making sure that the pictures are a similar size
    in the event that ((width1 != width2) || (height1 != height2))
    {
    System.err.println("Error: Image aspects don't coordinate");
    System.exit(1);
    }

    // Save Third picture with similitudes featured in blue.
    String filename3;
    filename3 = FileChooser.pickAFile();
    p2.write( filename3 );
    }

    }// end
     
    derhau, Jul 16, 2022 IP