简体   繁体   English

比较两个图像是否相同

[英]compare two images is same or not

I know how to compare two string is same or not.this is coding for compare two strings TextView t,t1; 我知道如何比较两个字符串是否相同。这是比较两个字符串的编码TextView t,t1;

String s,s1;
s=t.getText().toString();
s1=t1.setText().toString();
if(s.equals(s1)){
   t.setText("equal");
}
else{
   t.setText("not equal");
}

i need the coding for compare two images are same or not.please give me early 我需要比较两个图像的编码是否相同。请尽早给我

Check that the height matches, if not return false. 检查高度是否匹配,如果不返回false。 Then, check if the width matches, and if not, return false. 然后,检查宽度是否匹配,如果不匹配,则返回false。 Then check each pixel until you find one that doesn't match. 然后检查每个像素,直到找到不匹配的像素。 When you do, return false. 当您这样做时,返回false。 If every pixel matches, return true. 如果每个像素都匹配,则返回true。

Pseudocode 伪码

bool imagesAreEqual(Image i1, Image i2)
{
    if (i1.getHeight() != i2.getHeight) return false;
    if (i1.getWidth() != i2.getWidth) return false;

    for (int y = 0; y < i1.getHeight(); ++y)
       for (int x = 0; x < i1.getWidth(); ++x)
            if (i1.getPixel(x, y) != i2.getPixel(x, y)) return false;

    return true;
}

In reality, you probably want to treat the image as a two dimensional array if you can, and just compare bytes. 实际上,如果可能的话,您可能希望将图像视为二维数组,并仅比较字节。 I don't know the Android image API, but getPixel might be slow. 我不知道Android图片API,但是getPixel可能很慢。

Image can be defined as an abstraction of BufferedImage , it just holds header-like information. 可以将Image定义为BufferedImage的抽象,它仅包含类似标头的信息。 Before reading the image pixels, compare the images with their sizes. 在读取图像像素之前,请将图像与其尺寸进行比较。

File f1;
File f2;
Image i1 = ImageIO.read(f1);
Image i2 = ImageIO.read(f2);
if(i1.getHeight() == i2.getHeight
    && i1.getWidth() == i2.getWİdth) {
    BufferedImage b1 = ImageIO.read(f1);
    BufferedImage b2 = ImageIO.read(f2);
    //compare pixels
}

Image is read way more faster than BufferedImage, as it doesn't get pixels from the file. 图像的读取速度比BufferedImage更快,因为它不会从文件中获取像素。 For comparison, I suggest a two or more layered pixels comparison. 为了进行比较,我建议进行两个或多个分层像素比较。 Using random will boost your speed, as finding a non-matching pixel is enough for you. 使用随机会提高您的速度,因为找到不匹配的像素就足够了。

for(i=0; i<N; i++) {
    x = r.nextInt(184);
    y = r.nextInt(184);
    if(b1.getRGB(x,y) != b2.getRGB(x,y)) {
        break;
    }
}

If the images pass the randomized comparison, do a pixel by pixel comparison, with breaking the loop in case of finding non-matching pixels. 如果图像通过随机比较,则逐个像素进行比较,并在发现不匹配像素的情况下中断循环。 I answered assuming that you need a fast running comparison. 我回答假设您需要快速运行比较。 For that, I don't suggest MD5 hashing, unless you want to find all duplicate images in a big image directory. 为此,我不建议使用MD5哈希,除非您想在大图像目录中找到所有重复的图像。 If you just want to compare two images, and fast, MD5 don't make much sense, as it's needed to read image's all pixels to checkout a hash value and this decreases speed for one comparison . 如果您只是想快速比较两个图像,那么MD5并没有太大意义,因为需要读取图像的所有像素以检出哈希值,这会降低一次比较的速度。

如果要检查两个图像是否绝对相等,则从两个图像中获取字节,然后将两个数组进行逐元素检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM