• Stars
    star
    1,504
  • Rank 31,186 (Top 0.7 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 3 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐ŸŒ… Content-aware image resizer and object remover based on Seam Carving algorithm

JS IMAGE CARVER

Content-aware image resizer

JS IMAGE CARVER

Background image by Ian Dooley

Content-aware image resizing

Content-aware image resizing might be applied when it comes to changing the image proportions (i.e. reducing the width while keeping the height) and when losing some parts of the image is not desirable. Doing the straightforward image scaling in this case would distort the objects in it. To preserve the proportions of the object while changing the image proportions we may use the Seam Carving algorithm.

In the example below, you may see how the original image width was reduced by 50% using content-aware resizing (left image) and straightforward scaling (right image). In this particular case, the left image looks more natural since the proportions of the balloons were preserved.

Content-aware image resizing

The Seam Carving algorithmโ€™s idea is to find the seam (continuous sequence of pixels) with the lowest contribution to the image content and then carve (remove) it. This process repeats over and over again until we get the required image width or height. In the example below, you may see that the hot air balloon pixels contribute more to the content of the image than the sky pixels. Thus, the sky pixels are being removed first.

JS IMAGE CARVER DEMO

Btw, finding the seam with the lowest energy is a pretty computationally expensive task (especially for large images). To make the seam search faster the dynamic programming approach might be applied.

Objects removal

The importance of each pixel (so-called pixel's energy) is being calculated based on its color (R, G, B) difference between two neighbor pixels. More detailed explanation with examples you may find in Content-aware image resizing in JavaScript blog post. Now, if we set the pixel energy to some really low level artificially (i.e. by drawing a mask on top of them), the Seam Carving algorithm would perform an object removal for us for free.

JS IMAGE CARVER OBJECT REMOVAL DEMO

You may launch the JS IMAGE CARVER and play around with resizing of your custom images. You may also check its source-code.

More examples

Here are some more examples of how the algorithm copes with more complex backgrounds.

Mountains on the background are being shrunk smoothly without visible seams.

Resizing demo with more complex backgrounds

The same goes for the ocean waves. The algorithm preserved the wave structure without distorting the surfers.

Resizing demo with more complex backgrounds

But also we need to keep in mind that the Seam Carving algorithm is not a silver bullet, and it may fail to resize the images where most of the pixels are edges (looks important to the algorithm). In this case, it starts distorting even the important parts of the image. In the example below the content-aware image resizing looks pretty similar to a straightforward scaling since for the algorithm all the pixels look important, and it is hard for it to distinguish Van Gogh's face from the background.

Example when the algorithm does not work as expected

Features

  • Downscaling of the images without distorting their content-intensive parts
  • Objects removal during resizing

Plans

  • Image upscaling
  • Upscale the image to its original size after objects removal
  • Real-time resizing

Author

More Repositories

1

javascript-algorithms

๐Ÿ“ Algorithms and data structures implemented in JavaScript with explanations and links to further readings
JavaScript
173,558
star
2

homemade-machine-learning

๐Ÿค– Python examples of popular machine learning algorithms with interactive Jupyter demos and math being explained
Jupyter Notebook
21,617
star
3

learn-python

๐Ÿ“š Playground and cheatsheet for learning Python. Collection of Python scripts that are split by topics and contain code examples with explanations.
Python
14,972
star
4

state-of-the-art-shitcode

๐Ÿ’ฉState-of-the-art shitcode principles your project should follow to call it a proper shitcode
4,789
star
5

nano-neuron

๐Ÿค– NanoNeuron is 7 simple JavaScript functions that will give you a feeling of how machines can actually "learn"
JavaScript
2,192
star
6

promote-your-next-startup

๐Ÿš€ Free resources you may use to promote your next startup
2,000
star
7

machine-learning-experiments

๐Ÿค– Interactive Machine Learning experiments: ๐Ÿ‹๏ธmodels training + ๐ŸŽจmodels demo
Jupyter Notebook
1,461
star
8

machine-learning-octave

๐Ÿค– MatLab/Octave examples of popular machine learning algorithms with code examples and mathematics being explained
MATLAB
796
star
9

self-parking-car-evolution

๐Ÿงฌ Training the car to do self-parking using a genetic algorithm
TypeScript
688
star
10

use-position

๐ŸŒ React hook usePosition() for fetching and following a browser geolocation
JavaScript
298
star
11

covid-19

๐Ÿ“ˆ Coronavirus (COVID-19) dashboard to show the dynamics of ะกoronavirus distribution per country
JavaScript
265
star
12

nodejs-master-class

๐Ÿ›  This repository contains the homework assignment for Node.js Master Class that is focused on building a RESTful API, web app GUI, and a CLI in plain Node JS with no NPM or 3rd-party libraries
JavaScript
235
star
13

angular-library-seed

๐ŸŒพ Seed project for Angular libraries that are AOT/JIT compatible and that use external SCSS-styles and HTML-templates
TypeScript
203
star
14

okso-app

โœ๐Ÿป The drawing app to express, grasp, and organize your thoughts and ideas. Draw to explain. Draw to grasp.
198
star
15

links-detector

๐Ÿ“– ๐Ÿ‘†๐Ÿป Links Detector makes printed links clickable via your smartphone camera. No need to type a link in, just scan and click on it.
TypeScript
184
star
16

trekhleb.github.io

๐Ÿงฌ My personal website with a list of my projects that help people learn and blog posts about life, web-development, and machine-learning.
TypeScript
176
star
17

hello-docker

๐Ÿณ Example Docker project that is used as illustration for automated continuous delivery flow with DockerCloud and DigitalOcean
Python
48
star
18

micrograd-ts

๐Ÿค– A TypeScript version of karpathy/micrograd โ€” a tiny scalar-valued autograd engine and a neural net on top of it
TypeScript
42
star
19

giphygram

๐Ÿ”Ž Experimental React application for searching GIF images on GIPHY
JavaScript
24
star
20

vscode-search-tree

๐Ÿ”Ž (Draft!) VSCode extension to show the search results in a tree view
TypeScript
21
star
21

trekhleb

๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ป My GitHub profile intro
15
star