Сравнение двух картинок по пикселям - Java
Формулировка задачи:
Привет!
Не могу понять как сравнить две картинки по пикелям при том что бы сгенерировалась новая картинка-результат с подчеркнутой разницой .Я вижу это так,разбил картинку на 10 квадратов начал сравнивать.Но это тупо немного,хотелось бы по пикселям сравнение.
Вот мой код :
И вывожу так
import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class CompareTwoImages { private static BufferedImage image1, image2, imageResult; private static boolean isIdentic; // a number of pieces along vertical and horizontal private static int compareX, compareY; // “tolerance” comparison parameter that allows to treat similar colors as the same private static double sensitivity = 0.10; public CompareTwoImages(String file1, String file2) throws IOException { image1 = loadPNG(file1); image2 = loadPNG(file2); } // set a number of pieces along vertical and horizontal public void setParameters(int compareX, int compareY) { CompareTwoImages.compareX = compareX; CompareTwoImages.compareY = compareY; } // compare the two images in this object. public void compare() { // setup change display image imageResult = new BufferedImage(image2.getWidth(null), image2.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D g2 = imageResult.createGraphics(); g2.drawImage(image2, null, null); g2.setColor(Color.RED); // assign size of each section int blocksX = (int)(image1.getWidth()/compareX); int blocksY = (int)(image1.getHeight()/compareY); CompareTwoImages.isIdentic = true; for (int y = 0; y < compareY; y++) { for (int x = 0; x < compareX; x++) { int result1 [][] = convertTo2D(image1.getSubimage(x*blocksX, y*blocksY, blocksX - 1, blocksY - 1)); int result2 [][] = convertTo2D(image2.getSubimage(x*blocksX, y*blocksY, blocksX - 1, blocksY - 1)); for (int i = 0; i < result1.length; i++) { for (int j = 0; j < result1[0].length; j++) { int diff = Math.abs(result1[i][j] - result2[i][j]); if (diff/Math.abs(result1[i][j]) > sensitivity) { // draw an indicator on the change image to show where change was detected. g2.drawRect(x*blocksX, y*blocksY, blocksX - 1, blocksY - 1); isIdentic = false; } } } } } } public BufferedImage getImageResult() { return imageResult; } // representation fragment's of the picture in a two-dimensional integer array public int[][] convertTo2D(BufferedImage subimage) { int width = subimage.getWidth(); int height = subimage.getHeight(); int[][] result = new int[height][width]; for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { result[row][col] = subimage.getRGB(col, row); } } return result; } // reading picture from disk public static BufferedImage loadPNG(String filename) throws IOException { BufferedImage img = null; try { img = ImageIO.read(new File(filename)); } catch (IOException e) { e.printStackTrace(); } return img; } // writing picture into disk public static void savePNG(BufferedImage bimg, String filename) { try { File outputfile = new File(filename); ImageIO.write(bimg, "png", outputfile); } catch(IOException e) { e.printStackTrace(); } } public boolean isIdentic() { return isIdentic; } }
import java.io.IOException; public class CompareTwoImagesTest { public static void main(String[] args) throws IOException { CompareTwoImages cti = new CompareTwoImages("/Users/.../img/img1.png", "/Users/.../img/img2.png"); cti.setParameters(10, 10); cti.compare(); if (!cti.isIdentic()) { System.out.println("no match"); CompareTwoImages.savePNG(cti.getImageResult(), "/Users/.../img/result.png"); } else { System.out.println("match"); } } }
Решение задачи: «Сравнение двух картинок по пикселям»
textual
Листинг программы
public void compare() { if (image1.getWidth() != image2.getWidth() || image2.getHeight() != image1.getHeight()) { System.out.println("dimensions must be the same"); return; } imageResult = new BufferedImage(image2.getWidth(null), image2.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D g2 = imageResult.createGraphics(); isIdentic = true; for (int i = 0; i < image1.getWidth(); i++) { for (int j = 0; j < image1.getHeight(); j++) { int rgb = Math.abs(image1.getRGB(i, j) - image2.getRGB(i, j)); if (rgb != 0) { g2.setColor(new Color(rgb)); g2.drawRect(i, j, 1, 1); isIdentic = false; } } } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д