Finding image subsets between two images
I'm working on a way to handle hardware-based bitmap animation. As an input, I've got an image sequence of a simple bitmap (it's not a video, it's more like simple shapes, even though they might contain bitmap fills). I'm making a texture atlas of this animation (so it can be rendered quickly with GPU), and since this sequence sometimes has most part of it standing still while a small part of it is animating, I need an algorithm that can find the "common parts" between two images, so I can save memory.
The images might not have the same size (if an object is growing or shrinking, for example), so I need a way to detect the 开发者_运维百科biggest common area between the two. I've seen this answer and it partly solves my problem. I'd like to know, though, if there is already a better algorithm for my case, specially because since the sizes can vary, one image is not necessarily contained within the other, but I'd need to find the common parts between the two.
One problem I see is that one image can be contained in many ways in another, how do you determine the right answer?
Does it have to be real-time? If not then you can do the simple O(n^4) search for it using a fitness function.
The fitness function could be the error between the images (which gives a n^8 algorithm).
UPDATE: Wrong analysis of me sorry. The search is n^2 and the fitness function is n^2 which gives n^4.
The whole algorithm should look something like this:
w1 = width of image 1
w2 = width of image 2
h1 = height of image 1
h2 = height of image 2
for x = -w1 to w1+w2
for y = -h1 to h1+h2
find max fitness(x,y)
fitness(xc,yc){
m=0
for each x where image 1 overlaps image 2 displaced by xc
for each y where image 1 overlaps image 2 displaced by yc
if (image1[x][y] == image2[x+xc][y+yc])
m += 1
return m
}
UPDATE: Modified fitness function to find the number of overlaps, and then try to find the most overlaps.
精彩评论