Automatic Image Cropping Using Local Features

Ardiant Utomo
Analytics Vidhya
Published in
2 min readJun 2, 2020

Image cropping is a challenging work to do before doing training in computer vision project. We need to know how to best prepare image data before using the image data into our CNN models or other computer vision stuffs.

We can crop the image based on the region of interest on our own. But it will takes a lot of time, and I hate that kind of work.

I found another way how to crop image automatically using local features (i.e. SIFT, SURF, ORB, etc.). IMO, this method is quite effective for cropping image.

OpenCV

In this example, i will use OpenCV version 3.4.2 because i will use the SIFT algorithm.

First, I have a dog image for the cropping standard. as you can see on the image below, it has a white background so the key points that will be found on that image is focused on the main object.

source: https://s3.amazonaws.com/cdn-origin-etr.akc.org/wp-content/uploads/2019/12/03202400/Yellow-Labrador-Retriever.jpg

After that, I have the second image which will be cropped.

source: https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/dog-puppy-on-garden-royalty-free-image-1586966191.jpg

In the code above, we load the image and then apply SIFT algorithm to find the key points and the descriptors of each images

After all the features of each images being extracted, we find the matching features of the target image with the cropping standard and crop it based on pixel on matching key points.

The results will looks like this:

Cropped image

Finally, we have successfully cropped image using local features(SIFT). we can improve this approach by changing the local features extraction algorithm or the descriptor matcher algorithm.

Maybe this approach is not the best approach, but I think we can take look at this method.

Hope you enjoyed it !! Thank you

--

--