Comparing images pixel by pixel

Hello everyone. Colleagues, I have a small code that makes a screenshot of the screen. He’s a worker.


Now I want to compare this picture with another picture. The comparison is pixel-by-pixel. I also want to write the code that will check for comparison pixel by pixel in the Java Edit node. But I can’t do it, please help me in the implementation of this code. My code is below:

import javax.imageio.ImageIO;

public class CheckPixels {
    public static void main(String args[]) throws IOException {

        File pic1 = new File("D:\\ani\\img1.png");
        File pic2 = new File("D:\\ani\\img2.png");
        if (Arrays.equals(returnPixelVal(pic1), returnPixelVal(pic2))) {
            System.out.println("Match");
        } else {
            System.out.println("No match");
        }

    }

    public static byte[] returnPixelVal(File in) {

        BufferedImage img = null;
        File f = null;
        byte[] pixels = null;
        // read image
        try {
            f = in;
            img = ImageIO.read(f);
            pixels = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
        } catch (IOException e) {
            System.out.println(e);
        }

        return pixels;

    }

}

Hi @Nuke_Attokurov,

that sounds like an interesting project!

Since my experience in Java is rather rusty, I would proceed using the KNIME Image Processing Extension in the following fashion. You could

  1. output the path to the images as two columns
  2. make them to a collection
  3. turn the collection cell into a flow variable
  4. feed the flow varaible into a Image Reader Node to let KNIME Read the images to compare
  5. Use an Image Calculator Node to calculate the pixel-wise difference
  6. The next steps depend on what you want to do - probably extracting the pixel positions of where there are differences? Then I’d probably
    1… use a Global Thresholder first to binarize the image
    2…use a Connected Component Analysis to get adjacent pixel differences as what we call a “labeling”
    3… calculate the midpoints via a the Segment Features Node

Would that be a doable roadmap for you? Please let me know if I can help further!

Kind regards,
Lukas

4 Likes

Thank you very much for everything yes this is exactly what I needed

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.