Compare the pixel of the two different images, takes too long time

前端 未结 3 1816
春和景丽
春和景丽 2021-01-16 12:11

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:



        
相关标签:
3条回答
  • 2021-01-16 12:29

    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

    0 讨论(0)
  • 2021-01-16 12:30

    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.

    0 讨论(0)
  • 2021-01-16 12:32

    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.

    0 讨论(0)
提交回复
热议问题