Determine how two images should be combined to produce the original image

Suppose I have two similar images as follows

Image 1 (base)

img1

Image 2 (target)

img2

The second image is basically a variation of the first image, except with a green oval slapped on top (indeed, I used image1 as the base to produce image2). To save space, I realize that instead of storing two separate images, I simply need to store the difference and then record the offset in, say, a text file.

So I would go and arbitrarily crop out that oval that I want, and I’ve effectively shaved off 2 KB in total because my images are solid colors so it’s nicely compressed.

img3

This is (or was?) a common technique used in various applications such as games to push down filesize, especially if you have a single image with dozens of variations, but those variations cover maybe 10% of the entire image, so there’s a lot of redundant data. And if you’re working with full 32bpp with lots and lots of color everywhere, that can add up quite quickly.

Problem

The problem I’m interested in is this: given the base image (image 1) and the cropped variation, merge them together in such a way that you end up with the original image (image 2)

Ideas

If the cropped variation preserved its position by simply removing all the redundant pixels, then the problem is already solved. However, that isn’t always the case.

My approach is largely based on the fact that the target image is derived from the base image. For the above examples, the difference is a single green oval. Because I sloppily cropped out the oval without making any effort to isolate it from the background, I happen to have given myself a way to determine whether the cropped image “fits” in place.

So for example, if I were to randomly place the cropped image somewhere in the middle, I might get something like this

pic4

Now when you compare it to the base image, you can see that the red and blue rectangles are slightly off by a lot, and it is obviously visually incorrect.

If I didn’t know anything about the surrounding pixels (for example, if the cropped variation was simply the green oval), then it would be impossible to reproduce the target image without going in there manually.

I can indirectly quantify how “close” my candidate image is to the target image by taking the sum of the differences between all pixels between the candidate image and the base image: the only pixels that should be different are the ones that make up the variation; the rest of the surrounding pixels that just happened to be in the rectangle should be exactly the same as the base image and should contribute a difference of 0.

Basic Solution

A brute-force approach would basically compute the sum of all pixel differences of every possible position that the cropped image can be placed, and then return all the positions that minimize this sum. For sufficiently complex images, the chances that there will be multiple results is pretty small compared to simple images.

At some point I realized that I only have to consider the pixels inside the cropped image’s rectangle, because those are the only pixels in the candidate image that changes compared to the base image, so this avoids some unnecessary computation.

Improvements?

My idea seems kind of slow. Well, it’ll likely take less than a minute for images of size 1280×960 or 1920x1080p, and it’s still a lot faster than doing it by hand.

Can my approach be improved? For example, do I really need to try every possible combination?

It would be interesting to see if there were other ways to approach the problem as well.

4

You’re basically reinventing video compression. See e.g this introductory document describing how MPEG-2 does it, or this for MPEG-4/H264. To address a couple of points in your post:

  • The search can be done much more efficiently than a brute-force scan over all possibilities by starting on a coarse grid (or at lower resolution) to get an estimate of the motion, and then refining it.
  • I believe one of the things MPEG-4 adds to MPEG-2’s approach is some notion of segmented regions of a scene (or sprite coding), which allows better compression of objects moving against a static background, layered cel animations and the like. The term “object based video” is often used.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Determine how two images should be combined to produce the original image

Suppose I have two similar images as follows

Image 1 (base)

img1

Image 2 (target)

img2

The second image is basically a variation of the first image, except with a green oval slapped on top (indeed, I used image1 as the base to produce image2). To save space, I realize that instead of storing two separate images, I simply need to store the difference and then record the offset in, say, a text file.

So I would go and arbitrarily crop out that oval that I want, and I’ve effectively shaved off 2 KB in total because my images are solid colors so it’s nicely compressed.

img3

This is (or was?) a common technique used in various applications such as games to push down filesize, especially if you have a single image with dozens of variations, but those variations cover maybe 10% of the entire image, so there’s a lot of redundant data. And if you’re working with full 32bpp with lots and lots of color everywhere, that can add up quite quickly.

Problem

The problem I’m interested in is this: given the base image (image 1) and the cropped variation, merge them together in such a way that you end up with the original image (image 2)

Ideas

If the cropped variation preserved its position by simply removing all the redundant pixels, then the problem is already solved. However, that isn’t always the case.

My approach is largely based on the fact that the target image is derived from the base image. For the above examples, the difference is a single green oval. Because I sloppily cropped out the oval without making any effort to isolate it from the background, I happen to have given myself a way to determine whether the cropped image “fits” in place.

So for example, if I were to randomly place the cropped image somewhere in the middle, I might get something like this

pic4

Now when you compare it to the base image, you can see that the red and blue rectangles are slightly off by a lot, and it is obviously visually incorrect.

If I didn’t know anything about the surrounding pixels (for example, if the cropped variation was simply the green oval), then it would be impossible to reproduce the target image without going in there manually.

I can indirectly quantify how “close” my candidate image is to the target image by taking the sum of the differences between all pixels between the candidate image and the base image: the only pixels that should be different are the ones that make up the variation; the rest of the surrounding pixels that just happened to be in the rectangle should be exactly the same as the base image and should contribute a difference of 0.

Basic Solution

A brute-force approach would basically compute the sum of all pixel differences of every possible position that the cropped image can be placed, and then return all the positions that minimize this sum. For sufficiently complex images, the chances that there will be multiple results is pretty small compared to simple images.

At some point I realized that I only have to consider the pixels inside the cropped image’s rectangle, because those are the only pixels in the candidate image that changes compared to the base image, so this avoids some unnecessary computation.

Improvements?

My idea seems kind of slow. Well, it’ll likely take less than a minute for images of size 1280×960 or 1920x1080p, and it’s still a lot faster than doing it by hand.

Can my approach be improved? For example, do I really need to try every possible combination?

It would be interesting to see if there were other ways to approach the problem as well.

4

You’re basically reinventing video compression. See e.g this introductory document describing how MPEG-2 does it, or this for MPEG-4/H264. To address a couple of points in your post:

  • The search can be done much more efficiently than a brute-force scan over all possibilities by starting on a coarse grid (or at lower resolution) to get an estimate of the motion, and then refining it.
  • I believe one of the things MPEG-4 adds to MPEG-2’s approach is some notion of segmented regions of a scene (or sprite coding), which allows better compression of objects moving against a static background, layered cel animations and the like. The term “object based video” is often used.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật