A
A
artshelom2017-06-27 19:32:21
Java
artshelom, 2017-06-27 19:32:21

How to compare pictures?

Good evening everyone.
I have an idea for image recognition, on the topics of nature, space, etc.
After reading the literature on this topic, I realized that for recognition, you need a lot of pictures to compare and a powerful computer, otherwise the work will be slow.
And my question is, if there are ready-made libraries in java for image recognition ?? Are there any other pitfalls to be aware of?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2017-06-27
@artshelom

If the question is to find several differences between two completely identical pictures, then libraries are not needed.

import java.io.File;
import java.awt.Color;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

public class ImageSimilarity {
    public static BufferedImage getDifferenceImage(BufferedImage img1, BufferedImage img2) {
        final int w = img1.getWidth(),
                  h = img1.getHeight(), 
                  highlight = Color.MAGENTA.getRGB();
        final int[] p1 = img1.getRGB(0, 0, w, h, null, 0, w);
        final int[] p2 = img2.getRGB(0, 0, w, h, null, 0, w);

        for (int i = 0; i < p1.length; i++) {
            if (p1[i] != p2[i]) {
                p1[i] = highlight;
            }
        }

        final BufferedImage out = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        out.setRGB(0, 0, w, h, p1, 0, w);
        return out;
    }

    public static void main(String[] args) throws Exception {
        ImageIO.write(
            getDifferenceImage(
                ImageIO.read(new File("img1.png")),
                ImageIO.read(new File("img2.png"))),
            "png",
            new File("diff.png"));    
    }
}

Libraries are needed for more complex tasks, such as finding the difference between two photographs of the same place taken at different times. The above code will stupidly fill the final image with purple fog. For such cases, libraries like OpenCV or OpenIMAJ are already needed.

D
Dimonchik, 2017-06-27
@dimonchik2013

yes: not all pictures are recognized
xs what you need, but there is no miracle library, there are frameworks that you need to learn ,
start here , but in general there are a lot of materials

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question