比较两个不同图像的像素,花费的时间太长 [英] Compare the pixel of the two different images, takes too long time

查看:96
本文介绍了比较两个不同图像的像素,花费的时间太长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较两个不同图像的像素。我将第一个图像的像素与第二个图像的所有像素进行比较。这是我的代码:

I want to compare the pixel of the two different images. I am comparing the pixel of first image with all the pixels of the second Image. Here is my code stuff:

for (int i = 0; i < bitmap.getWidth(); i++) {
    for (int j = 0; j < bitmap.getHeight(); j++) {

        for (int k = 0 ; k<bitmpa2.getWidth(); k++) {
            for (int l = 0 ; l<bitmpa2.getHeight(); l++) {

                if (bitmap.getPixel(i, j) == bitmap2.getPixel(k, l))
                  Counter++ ;
          }
       }
    }
}

这里Counter的值是两个图像中相同的像素数。问题是这可以工作,但是执行它需要很长时间,所以时间约束是这里的问题,那么我有什么可以减少时间并获得精确结果。任何其他可能性都是受欢迎的。

Here the value of Counter is the number of pixels that are same in both the Images. The problem is that this works but it takes much time to perform it, so time constraint is the problem here, So what can I have to reduce the time and get exact result. Any other possibility is welcome.

推荐答案

如果您使用的是API级别12或更高级别,则会有一个名为 sameAs 位图可以完全按照您的要求进行操作。否则,使用 getPixels 并执行以下操作:

If you're using API level 12 or above, there's a method called sameAs on Bitmap to do exactly what you're looking for. Otherwise, use getPixels and do something like:

int width = bitmap1.getWidth();
int height = bitmap1.getHeight();
int pixelCount = width * height;
int[] pixels1 = new int[pixelCount];
int[] pixels2 = new int[pixelCount];

bitmap1.getPixels(pixels1, 0, 0, 0, 0, width, height);
bitmap2.getPixels(pixels2, 0, 0, 0, 0, width, height);

for (int i = 0; i < pixelCount; i++) {
    if (pixels1[i] != pixels2[i]) {
        return false;
    }
}
return true;

或者,如果你真的想做反击的事情,看看有多少像素是相同的,请继续并且这样做。

Or if you really want to do the counter thing to see how many pixels are the same, go ahead and do that.

实际上,你也可以对缓冲区做些什么......也许像

Actually, you can probably do something with the buffers too... Maybe something like

int width = bitmap1.getWidth();
int height = bitmap1.getHeight();
int pixelCount = width * height;
IntBuffer buffer1 = IntBuffer.allocate(pixelCount);
IntBuffer buffer2 = IntBuffer.allocate(pixelCount);
bitmap1.copyPixelsToBuffer(buffer1);
bitmap2.copyPixelsToBuffer(buffer2);
int result = buffer1.compareTo(buffer2);

我不确定这两种方法在性能上的比较,但它可以解决这个问题你想要的。

I'm not sure how those two methods compare in performance, but it's something to play around with if you want.

这篇关于比较两个不同图像的像素,花费的时间太长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆