Automatic Cropping with Hill Climbing Algorithm

Moradeke Onamusi
4 min readSep 5, 2022

--

Overview

SmartCrop is an automatic cropping tool that optimizes product images. It automatically crops product images to different sizes, given a specified aspect ratio. In this project, images have been cropped using specifications for the different types of Instagram posts. The idea is to use a single product image as different posts across instagram, without affecting the product in the image.

The program:

  • Detects the product in the image and gets it location data
  • Locates points within the image which form a box that satisfies specified aspect ratio requirements, while containing (most to all of) the product in the picture
  • Crops the image using the location of the box in step 2.

The Algorithm

Hill Climbing Algorithm is a local search algorithm for mathematical optimization problems. The algorithm starts at an initial state and finds the neighbouring states. It then compares the value assigned to each state; the Objective Function if the goal is to get the maximum value or the cost function if the goal is to get the minimum value. The state with the highest objective function or lowest cost function becomes the optimal state. In this project, I defined a cost function as my goal was to find the box with the minimum distance to the image edges (closest box to the edges) in order to capture as much of the product and background as possible.

Learn more about the algorithm from my favourite course. Lecture by Brian Yu at HarvardCS50

Two variations of the hill-climbing algorithm were used; Random-restart hill climbing and steepest-ascent hill climbing.

The program starts at a random initial state i.e. a random box with dimensions that satisfy the specified aspect ratio.

The steepest-ascent hill climbing algorithm gets all the neighbours. I have defined neighbours as all possible boxes with the same dimension. For example, given a 1:1 aspect ratio, the program starts with a random box of say 270 x 270. The neighbours are all 270 x 270 boxes located at any point within the image.

Using the steepest-ascent hill climbing algorithm may give the best 270 x 270 box(local minimum), but not necessarily the best square cropper for the image(global minimum). Enter random-restart.

Random restart repeats the process multiple times (10 times in the case of this project) and has a different initial state each time. This means we get to compare 1:1 boxes of different sizes, giving us a better shot at getting the global minimum.

Pseudocode

Pseudocode for implementation of the random restart steepest-ascent hill climbing algorithm
Pseudocode for implementation of the random restart steepest-ascent hill climbing algorithm

Note

  • A state is a box with points a,b,c,d, a point has coordinates(x,y)
  • Neigbours represent different states.
  • The set of neighbours have been limited to a size of 30 out of potential hundreds of thousands to millions of possible neighbours.
  • Each neighbour has a cost function which is a value assigned to each state depending on their distance to the image edges.
  • The cost function also considers the center of the box. A box also carries some value depending on how close its center is to the center of the image

Link to GitHub Repository Here

Execution

Original Image

Feed Post. 1:1 Aspect Ratio

Vertical Posts

Vertical feed post 1. 4:5 Aspect Ratio
Vertical feed post 2. 4:5 Aspect Ratio
Vertical feed post 3. 4:5 Aspect Ratio

Landscape Post

Landscape feed post 1. 1.91:1 Aspect Ratio
Landscape feed post 2. 1.91:1 Aspect Ratio

IG Stories

IG Story Post 1. Aspect Ratio 9:16.
Horribly cropped IG Story Post 2. Aspect Ratio 9:16.
IG Story Post 3. Aspect Ratio 9:16.

IGTV Cover

IGTV Cover. 1:1.55 Aspect Ratio
IGTV Cover 2. 1:1.55 Aspect Ratio

Libraries and resources used

Conclusion

I very much liked the result of combining these two variations hill climbing algorithm. The algorithm has a tendency to do better with a larger set of neighbours, more random restarts and, of course, defining a good cost function.

--

--