📜 ⬆️ ⬇️

Entropy-based smart crop

In many web applications, there is a need for automatic cropping - whether it be cutting out avatars from a downloaded photo, previewing large images or creating thumbnails in large galleries.



But the machines are still not human, and the necessary area will not always be cut. Old ways to cut a miniature of 100x100 from the upper left corner or from the center fade into the background, and a smart crop comes into the arena.

Traditional Smart Crop


Most libraries use smart crop to avoid distorting and stretching photos. The bottom line is that first, the code will resize the image so that one of the parameters — length or width — matches the specified one. It then checks if the second parameter matches the given one. If not, crop unnecessary areas from the center.
')
The creation of a miniature, for example, 400x400 will occur as follows:



The hitch is that far from always (or rather almost never) the most important thing is in the very center. Most often - some part of the main thing, but the objects are most often somehow shifted.

Entropy-based smart crop


The general idea is to find a fragment of a picture that carries the greatest amount of information and save it in a cropped version.

How it works


At first the picture is resized.

Then the filter is run to determine the boundaries. The picture becomes black and white and only contours of objects remain on it.

The saturation is reduced as much as possible and the picture is somewhat “washed out”.

The fragment with the highest entropy is determined — that is, with the greatest number of contours. The coordinates of this fragment are returned, that is, that part of the picture that cannot be cut off in any way.


The most difficult stage is the search for a fragment with the highest entropy. The search is performed fragmentary - the picture is “cut” into 25 bars, the entropy for each of them is calculated and compared. Then the group with maximum entropy is determined.

Application


It is convenient to use this library for use.
Requirements: PHP 5.3 with the Imagick extension. It is applied in three stages.

Initialization:

$center = new CropEntropy($imagePath); 

Resize and crop:

 $croppedImage = $center->resizeAndCrop($width, $height); 

Saving a new image:

 $croppedImage->writeimage($newImagePath); 


You can try entropy-based cropping here , setting the required thumbnail size.
Sometimes the algorithm accurately identifies the most important area. But sometimes it makes mistakes , especially with non-standard - non-square cropping parameters (300x100). But here is a very good result with the same parameters, but another photo. The algorithm does not guarantee a perfect result, but nevertheless it is one of the best autocrop methods.

Abstract


  1. One of the best ways to automate Crop is Crop based on entropy.
  2. The algorithm determines the fragment with the greatest amount of information and stores it in the drop version.
  3. The advantages and disadvantages of the method can be verified with this tool.

Source: https://habr.com/ru/post/259997/


All Articles