比较两个不同图像的像素,花费的时间太长

发布于 2024-12-11 00:20:36 字数 528 浏览 0 评论 0 原文

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

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++ ;
          }
       }
    }
}

这里计数器的值是两个图像中相同的像素数。问题是,这可行,但执行它需要很多时间,所以时间限制就是这里的问题,那么我怎样才能减少时间并获得准确的结果。欢迎任何其他可能性。

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++ ;
          }
       }
    }
}

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

新人笑 2024-12-18 00:20:36

哦不,当你在 Android 中进行图像处理时请记住这一点。

永远不要连续使用 getPixel() 或 setPixel() ,就像循环一样,它会
导致性能非常糟糕,非常慢。使用 getPixels()
和 setPixels() 代替

,请记住,您需要先阅读 Android 文档。

Oh no, remember this in mind when you do image processing in Android.

Never use getPixel() or setPixel() continuously, like a loop, it will
result in a really really bad performance, damn slow. Use getPixels()
and setPixels() instead

Well, keep in mind that you need to read Android Documentation first.

旧伤还要旧人安 2024-12-18 00:20:36

如果您使用的是 API 级别 12 或更高版本,则有一个名为 Bitmap 上使用 rel="nofollow">sameAs 来完成您想要的操作。否则,请使用 getPixels 并执行以下操作:

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;

或者如果您确实想执行计数器操作来查看有多少像素同样,继续这样做。

实际上,您也可以对缓冲区做一些事情...也许像

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);

我不确定这两种方法在性能上的比较,但如果您愿意的话,这是可以尝试的。

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.

梦归所梦 2024-12-18 00:20:36

你的算法的阶数是n^4。

我想如果你

  1. 循环所有可能的颜色,你可以将其降低到 n^3。
  2. 在该循环内,在每个图像上循环
  3. 查找每个图像中出现的颜色 i
  4. 最后使用方程来增加 Counter

如果颜色 i位图y中出现xbitmap2

Counter = Counter + x*y

The order of your algorithm is n^4.

I guess that you could drive that down to n^3 if you

  1. loop over all possible colors.
  2. inside that loop, loop on each image
  3. find the occurances of color i in each image
  4. finally use an equation to increment Counter

If color i occurs x times in bitmap and y times in bitmap2,

Counter = Counter + x*y

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文