Anonymous user
Percentage difference between images: Difference between revisions
Percentage difference between images (view source)
Revision as of 19:08, 18 October 2017
, 6 years ago→{{header|Java}}: restructure code to be reusable
(→{{header|Java}}: restructure code to be reusable) |
|||
Line 735:
<lang java>import java.awt.image.BufferedImage;
import
import java.io.IOException;
import
public
public static void main(String
// https://rosettacode.org/mw/images/b/b6/Lenna100.jpg
BufferedImage img1 = ImageIO.read(new File("Lenna50.jpg"));
▲ URL url2 = new URL("http://rosettacode.org/mw/images/b/b6/Lenna100.jpg");
}
private static double getDifferencePercent(BufferedImage img1, BufferedImage img2) {
int width2 = img2.getWidth(null);▼
int
int
if (width != width2 || height != height2) {
throw new IllegalArgumentException(String.format("Images must have the same dimensions: (%d,%d) vs. (%d,%d)", width, height, width2, height2));
long diff = 0;▼
for (int x = 0; x < width; x++) {
diff += pixelDiff(img1.getRGB(x, y), img2.getRGB(x, y));
}
}
long maxDiff = 3L * 255 * width * height;
return 100.0 * diff / maxDiff;
}
▲ long diff = 0;
▲ for (int x = 0; x < width1; x++) {
int r1 = (rgb1 >> 16) & 0xff;
int g1 = (rgb1 >> 8) & 0xff;
int b1 =
int r2 = (rgb2 >> 16) & 0xff;
int g2 = (rgb2 >> 8) & 0xff;
int b2 =
▲ }
}
}</lang>
|