Answer the question
In order to leave comments, you need to log in
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
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"));
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question