Pack images into a larger image - algorithm

There are images (literally, PNG files), they come in different sizes and need to be put together into a larger image, so that (1) they don’t overlap, and (2) the amount of unused pixels is minimal.
Can someone point me in the right direction? I can imagine that packing rectangles is nothing new, but honestly, I cannot find neither an implementation nor an algorithm for this.

This is one algorithm: Fast optimizing rectangle packing algorithm for building CSS sprites. The article includes an explanation and C# code. It also links to a paper that might be interesting.

Related

Algorithm to pack N rectangles into a larger one

I am writing a (JavaScript) thumbnail generator and this bit of the project involves placing the N thumbnails in regular rows and columns within a larger target space, including a border around the outside and "visually pleasing" borders between each thumbnail.
Each thumbnail is large and already scaled to turn it into a thumbnail so one approach is to change the size of each thumbnail to help fulfil the packing conditions.
Each thumbnail is of an identical size. The number of thumbnails could be anywhere from a small number (4??) to something around 100.
I have been looking for some ideas and there seem to be plenty of example code for algorithms that pack as many rectangles of different sizes into the densest possible configuration - but I haven't come across anything similar to what I am trying to do.
If anyone can point me at some code or even an algorithm, I'd be very grateful.

Resources & approaches for detecting content-aware fill

How would one approach the problem of telling apart images (and highlighting areas) that have been content-aware filled?
Positive class image (original image without edits):
Negative class image (Tampered image with insignia removed):
I understand that this can be quite meaningfully tackled by training a segmentation CNN network. Is there a working solution that I haven't been able to find or an approach that in relatively simple to implement.
These images are samples from a codebase that throws Runtime errors.
I am no expert on this, but as far as I know structural image manipulation algorithms like Photoshop's content aware fill (which is based on the PatchMatch Algorithm) are dividing an image up into so-called patches defined by a common intensity statistic. The manipulation is then carried out by replacing the target patch by a source patch with statistics matching the adjacent patches as well as possible (minimizing some distance metric).
Therefore, in a forged image you would find cloned regions, e.g. patches with identical or almost identical intensity distributions. To detect this, my naive approach would be to split the image up in many small subimages and use each one as a filter mask to be cross-correlated with the full image. I would expect authentic images to show only one more or less clear global maximum. A forged image would potentially produce two or more "global" maxima having a similar height.
But there seems to be a whole bunch of people having released some open source code for forgery detection, most probably using smarter approaches then mine -> Github topic: Fogery detection.

layout algorithm for tightly-packed image thumbnails

I'm working on an image gallery and I'd like to tightly pack the image thumbnails. The thumbnails are:
different aspect ratios
available at the same source resolution (longest edge 256 pixels)
I'd like to come up with an optimal solution (will probably have to be a heuristic) that allowed me to balance:
the padding between each thumbnail (preferably constant)
the consistency of thumbnail size (preferably all the same size)
the amount of each image that gets cropped for the display (preferably none)
the proximity of images consistent with their sort order (preferably sort-neighbours will be near one another in the grid)
I think this is a variant of the rectangle packing problem.
I've found some good references: Fast Optimizing Rectangle Packing Algorithm for Building CSS Sprites
But I wanted to check with the experts to see if anyone knows of:
any established algorithms that are available publicly,
any open source libraries that implement them or
any other mathematical references or guidance that might help me produce something as good as: http://labs.tineye.com/multicolr#colors=4b669e;weights=100;
I have come up with something like this (now also with code on github)
https://mendrik.github.io/diorama/
I should add that the order will be random, and the sizes try to be uniform, but for me it was more important to fill out the entire space rather than keep the sizes consistent. You can resize the browser window to see how it works.
If your height is not fixed, there are several other options, mostly knapsack or partitioning algorithms. 2d bin backing will leave you with gaps or wont find solutions that always fit all images.
my algorithm has almost no cropping and fits always all images into the given space, provided there are enough combinations to do so. the less images the more cropping obviously.

Fastest method to search for a specified item on an image?

Imagine we have a simple 2D drawing, filled it with lots of non-overlapping circles and only a few stars.
If we are to find all the stars among all these circles, I can think of very few methods. Brute force is one of them. Another one is possibly reduce the image size (to the optimal point where you can still distinguish the objects apart) and then apply brute force and map to the original image. The drawback of brute force is of course, it is very time consuming. I am looking for faster methods, possibly the fastest one.
What is the fastest image processing method to search for the specified item on a simple 2D image?
One typical way of looking for an object in an image is through cross correlation. Basically, you look for the position where the cross-correlation between a mask (the object you're attempting to find) and the image is the highest. That position is the likely location of the object you're trying to find.
For the sake of simplicity, I will refer to the object you're attempting to find as a star, but in general it can be any shape.
Some problems with the above approach:
The size of the mask has to match the size of the star. If you don't know the size of the star, then you will have to try different size masks. Image pyramids are more effective than just iteratively trying different size masks, but still require extra effort.
Similarly, the orientations of the mask and the star have to match. If they don't, the cross-correlation won't work.
For these reasons, the more you know about your problem, the simpler it becomes. This is the reason why people have asked you for more information in the comments. A general purpose solution doesn't really exist, to the best of my knowledge. Maybe someone more knowledgeable can correct me on this.
As you've mentioned, reducing the size of the image will help you reduce the computational time of your approach. In my opinion, it's hardly the core element of a solution -- it's just an optional optimization step.
If the shapes are easy to segment from the background, you might be able to compute distinguishing shape/color descriptors. Depending on your problem you could choose descriptors that are invariant to scale, translation or rotation (e.g. compactness, if it is unique to each shape). I do not know if this will be faster, though.
If you already know the exact shape and have an idea about the size, you might want to have a look at the Generalized Hough Transform, which is basically a formalized description of your "brute force algorithm"
As you list a property that the shapes are not overlapping then I assume an efficient algorithm would be able to
cut out all the shapes by scanning the image in some way (I can imagine relatively efficient and simple algorithm for convex shapes)
when you are left with cut out shapes you could use cross relation misha mentioned
You should describe the problem a bit better
can the shapes be rotated or scaled (or some other transform?)
is the background uniform colour
are the shapes uniform colour
are the shapes filled
Depending on the answer on the above questions you might have more less or more simple solutions.
Also, maybe this article might be interesting.
If the shapes are very regular maybe turning them into vectors could fit your needs nicely, but it might be an overkill, really depends what you want to do later.
Step 1: Thresholding - reduce the image to 1 bit (black or white) if the general image set permits it. [For the type of example you cite, my guess is thresholding would work nicely - leaving enough details to find objects].
Step 2: Optionally do some smoothing/noise removal.
Step 3: Use some clustering approach to gather the foreground objects.
Step 4: Use an appropriate heuristic to identify the objects.
The parameters in steps 1/2 will depend a lot on the type of images as well as experimentation/observation. 3 is usually straightforward if you have worked out 1/2 correctly. 4 will depend very much on the problem (for example, in your case identifying stars - which would depend on what is the actual shape of the stars expected in the images).

Position boxes like in Exposé

Does anyone know a way to locate rectangles to best fit a certain area? The rectangles can be scaled up to a certain limit, but they should keep their proportions.
I basically want to rebuild Mac OS' Exposé:
Picture
Thanks,
eWolf
The problem is NP Hard, but that is only for the optimal solution. What I guess you really want is a solution that just looks good.
So I suggest looking for algorithms that make data look good. Once I wanted to layout pictures for the DeepZoom Composer and I tried to recycle a graph drawing force-interaction based algorithm. http://en.wikipedia.org/wiki/Force-based_algorithms
It worked really well even for 600 hundred rectangles, you just have to play with the details of the simulation.
How the distance is calculated?
What functions do you use for the attraction and repulsion forces?
How much overlap are you going to accept?
The only problem I could not solve entirely, was that the rectangles formed a circle shape, rather then a big rectangle shape, which is natural when you are simulating physics. But you can work around that with carefully placed auxiliary force sources.

Resources