SIGN IN SIGN UP
esimov / caire UNCLAIMED

Content aware image resize library

0 0 0 Go
2019-01-22 21:07:12 +02:00
<h1 align="center"><img alt="Caire Logo" src="https://user-images.githubusercontent.com/883386/51555990-a1762600-1e81-11e9-9a6a-0cd815870358.png" height="180"></h1>
2018-02-27 16:42:21 +02:00
[![build](https://github.com/esimov/caire/actions/workflows/build.yml/badge.svg)](https://github.com/esimov/caire/actions/workflows/build.yml)
2022-03-02 15:29:01 +02:00
[![Go Reference](https://pkg.go.dev/badge/github.com/esimov/caire.svg)](https://pkg.go.dev/github.com/esimov/caire)
2020-02-10 12:32:42 +02:00
[![license](https://img.shields.io/github/license/esimov/caire)](./LICENSE)
2025-04-27 17:49:35 +03:00
[![release](https://img.shields.io/badge/release-v1.5.0-blue.svg)](https://github.com/esimov/caire/releases/tag/v1.5.0)
[![homebrew](https://img.shields.io/badge/homebrew-v1.5.0-orange.svg)](https://formulae.brew.sh/formula/caire)
2021-06-18 10:10:10 +03:00
[![caire](https://snapcraft.io/caire/badge.svg)](https://snapcraft.io/caire)
2018-02-07 06:49:05 +02:00
2020-01-30 10:06:37 -07:00
**Caire** is a content aware image resize library based on *[Seam Carving for Content-Aware Image Resizing](https://inst.eecs.berkeley.edu/~cs194-26/fa16/hw/proj4-seamcarving/imret.pdf)* paper.
2018-01-28 14:37:10 +02:00
## How does it work
2018-01-28 14:37:10 +02:00
* An energy map (edge detection) is generated from the provided image.
* The algorithm tries to find the least important parts of the image taking into account the lowest energy values.
2020-11-24 21:10:38 +02:00
* Using a dynamic programming approach the algorithm will generate individual seams across the image from top to down, or from left to right (depending on the horizontal or vertical resizing) and will allocate for each seam a custom value, the least important pixels having the lowest energy cost and the most important ones having the highest cost.
2021-05-31 16:18:34 +03:00
* We traverse the image from the second row to the last row and compute the cumulative minimum energy for all possible connected seams for each entry.
* The minimum energy level is calculated by summing up the current pixel value with the lowest value of the neighboring pixels obtained from the previous row.
* We traverse the image from top to bottom and compute the minimum energy level. For each pixel in a row we compute the energy of the current pixel plus the energy of one of the three possible pixels above it.
2018-01-28 14:37:10 +02:00
* Find the lowest cost seam from the energy matrix starting from the last row and remove it.
2018-01-29 09:54:49 +02:00
* Repeat the process.
2018-01-28 14:37:10 +02:00
#### The process illustrated:
2018-01-30 17:03:08 +02:00
| Original image | Energy map | Seams applied
|:--:|:--:|:--:|
2020-01-30 10:06:37 -07:00
| ![original](https://user-images.githubusercontent.com/883386/35481925-de130752-0435-11e8-9246-3950679b4fd6.jpg) | ![sobel](https://user-images.githubusercontent.com/883386/35481899-5d5096ca-0435-11e8-9f9b-a84fefc06470.jpg) | ![debug](https://user-images.githubusercontent.com/883386/35481949-5c74dcb0-0436-11e8-97db-a6169cb150ca.jpg) | ![out](https://user-images.githubusercontent.com/883386/35564985-88c579d4-05c4-11e8-9068-5141714e6f43.jpg) |
2018-01-28 14:37:10 +02:00
## Features
2019-04-24 08:54:48 +03:00
Key features which differentiates this library from the other existing open source solutions:
2018-01-28 14:37:10 +02:00
2021-12-04 14:55:43 +02:00
- [x] **GUI progress indicator**
2018-01-29 09:43:10 +02:00
- [x] Customizable command line support
2018-01-29 09:54:49 +02:00
- [x] Support for both shrinking or enlarging the image
- [x] Resize image both vertically and horizontally
2021-12-04 14:55:43 +02:00
- [x] Face detection to avoid face deformation
2025-04-27 10:40:09 +03:00
- [x] Support for multiple output image type (jpg, jpeg, png, bmp)
2021-12-04 14:55:43 +02:00
- [x] Support for `stdin` and `stdout` pipe commands
2021-05-24 16:41:27 +03:00
- [x] Can process whole directories recursively and concurrently
2018-01-28 14:37:10 +02:00
- [x] Use of sobel threshold for fine tuning
2018-01-29 09:43:10 +02:00
- [x] Use of blur filter for increased edge detection
- [x] Support for squaring the image with a single command
2018-02-27 12:02:36 +02:00
- [x] Support for proportional scaling
2021-12-27 11:11:43 +02:00
- [x] Support for protective mask
- [x] Support for removal mask
- [x] [GUI debug mode support](#masks-support)
2021-11-26 07:21:48 +02:00
2018-01-29 09:43:10 +02:00
## Install
First, install Go, set your `GOPATH`, and make sure `$GOPATH/bin` is on your `PATH`.
```bash
2025-04-27 10:40:09 +03:00
$ go install github.com/esimov/caire/cmd/caire@latest
2018-01-29 09:43:10 +02:00
```
2018-02-07 06:49:05 +02:00
## MacOS (Brew) install
2020-11-01 07:58:50 +02:00
The library can also be installed via Homebrew.
2018-02-05 10:13:03 +02:00
```bash
$ brew install caire
```
2018-01-28 14:37:10 +02:00
## Usage
```bash
$ caire -in input.jpg -out output.jpg
```
### Supported commands:
2020-01-30 10:06:37 -07:00
```bash
2018-01-28 14:37:10 +02:00
$ caire --help
```
2018-01-29 09:43:10 +02:00
The following flags are supported:
2018-01-28 14:37:10 +02:00
2018-01-29 09:43:10 +02:00
| Flag | Default | Description |
| --- | --- | --- |
2020-01-30 10:06:37 -07:00
| `in` | - | Input file |
| `out` | - | Output file |
2018-01-29 09:43:10 +02:00
| `width` | n/a | New width |
| `height` | n/a | New height |
2021-12-16 14:44:21 +02:00
| `preview` | true | Show GUI window |
2018-01-30 13:51:31 +02:00
| `perc` | false | Reduce image by percentage |
2018-02-14 10:37:13 +02:00
| `square` | false | Reduce image to square dimensions |
2022-02-04 17:07:41 +02:00
| `blur` | 4 | Blur radius |
| `sobel` | 2 | Sobel filter threshold |
2018-01-29 09:43:10 +02:00
| `debug` | false | Use debugger |
| `face` | false | Use face detection |
| `angle` | float | Plane rotated faces angle |
2021-12-27 13:01:58 +02:00
| `mask` | string | Mask file path |
| `rmask` | string | Remove mask file path |
2022-09-22 14:34:26 +03:00
| `color` | string | Seam color (default `#ff0000`) |
2022-01-08 07:53:44 +02:00
| `shape` | string | Shape type used for debugging: `circle`,`line` (default `circle`) |
2020-07-08 11:22:34 +03:00
2022-05-23 09:49:30 +03:00
## Face detection
The library is capable of detecting human faces prior resizing the images by using the lightweight Pigo (https://github.com/esimov/pigo) face detection library.
The image below illustrates the application capabilities for human face detection prior resizing. It's clearly visible that with face detection activated the algorithm will avoid cropping pixels inside the detected faces, retaining the face zone unaltered.
| Original image | With face detection | Without face detection
|:--:|:--:|:--:|
| ![Original](https://user-images.githubusercontent.com/883386/37569642-0c5f49e8-2aee-11e8-8ac1-d096c0387ca0.jpg) | ![With Face Detection](https://user-images.githubusercontent.com/883386/41292871-6ca43280-6e5c-11e8-9d72-5b9a138228b6.jpg) | ![Without Face Detection](https://user-images.githubusercontent.com/883386/41292872-6cc90e8e-6e5c-11e8-8b41-5b4eb5042381.jpg) |
[Sample image source](http://www.lens-rumors.com/wp-content/uploads/2014/12/EF-M-55-200mm-f4.5-6.3-IS-STM-sample.jpg)
2021-12-04 14:55:43 +02:00
### GUI progress indicator
2021-12-16 14:44:21 +02:00
<p align="center"><img alt="GUI preview" title="GUI preview" src="https://github.com/esimov/caire/raw/master/gui_preview.gif"></p>
2021-12-08 14:19:53 +02:00
2023-01-18 16:06:29 +02:00
A GUI preview mode is also incorporated into the library for in time process visualization. The [Gio](http://gioui.org/) GUI library has been used because of its robustness and modern architecture. Prior running it please make sure that you have installed all the required dependencies noted in the installation section (https://gioui.org/#installation) .
2022-05-23 09:49:30 +03:00
The preview window is activated by default but you can deactivate it any time by setting the `-preview` flag to false. When the images are processed concurrently from a directory the preview mode is deactivated.
2021-12-04 14:55:43 +02:00
### Face detection to avoid face deformation
2022-05-23 09:49:30 +03:00
In order to detect faces prior rescaling, use the `-face` flag. There is no need to provide a face classification file, since it's already embedded into the generated binary file. The sample code below will resize the provided image with 20%, but checks for human faces in order tot avoid face deformations.
2020-07-08 11:22:34 +03:00
2021-07-26 06:30:14 +03:00
For face detection related settings please check the Pigo [documentation](https://github.com/esimov/pigo/blob/master/README.md).
2020-07-08 11:22:34 +03:00
```bash
2021-11-10 17:18:15 +02:00
$ caire -in input.jpg -out output.jpg -face=1 -perc=1 -width=20
2020-07-08 11:22:34 +03:00
```
2021-12-04 14:55:43 +02:00
### Support for `stdin` and `stdout` pipe commands
You can also use `stdin` and `stdout` with `-`:
2018-01-29 21:11:56 +02:00
```bash
2021-12-04 14:55:43 +02:00
$ cat input/source.jpg | caire -in - -out - >out.jpg
2018-01-29 21:11:56 +02:00
```
2021-12-04 14:55:43 +02:00
`in` and `out` default to `-` so you can also use:
2020-07-08 11:22:34 +03:00
2021-12-04 14:55:43 +02:00
```bash
$ cat input/source.jpg | caire >out.jpg
$ caire -out out.jpg < input/source.jpg
```
2018-02-27 12:02:36 +02:00
2022-05-23 09:49:30 +03:00
You can provide also an image URL for the `-in` flag or even use **curl** or **wget** as a pipe command in which case there is no need to use the `-in` flag.
2018-01-29 09:54:49 +02:00
```bash
2021-12-04 14:55:43 +02:00
$ caire -in <image_url> -out <output-folder>
$ curl -s <image_url> | caire > out.jpg
2018-01-29 09:54:49 +02:00
```
2021-12-04 14:55:43 +02:00
### Process multiple images from a directory concurrently
2022-05-23 09:49:30 +03:00
The library can also process multiple images from a directory **concurrently**. You have to provide only the source and the destination folder and the new width or height in this case.
2020-01-30 10:06:37 -07:00
```bash
2021-12-04 14:55:43 +02:00
$ caire -in <input_folder> -out <output-folder>
2020-01-30 10:06:37 -07:00
```
2021-12-04 14:55:43 +02:00
### Support for multiple output image type
2025-04-27 10:40:09 +03:00
There is no need to define the output file type, just use the correct extension and the library will encode the image to that specific type.
2021-12-04 14:55:43 +02:00
### Other options
In case you wish to scale down the image by a specific percentage, it can be used the **`-perc`** boolean flag. In this case the values provided for the `width` and `height` are expressed in percentage and not pixel values. For example to reduce the image dimension by 20% both horizontally and vertically you can use the following command:
2020-01-30 10:06:37 -07:00
```bash
2021-12-04 14:55:43 +02:00
$ caire -in input/source.jpg -out ./out.jpg -perc=1 -width=20 -height=20 -debug=false
2020-01-30 10:06:37 -07:00
```
2021-12-04 14:55:43 +02:00
Also the library supports the **`-square`** option. When this option is used the image will be resized to a square, based on the shortest edge.
2022-05-23 09:49:30 +03:00
When an image is resized on both the X and Y axis, the algorithm will first try to rescale it prior resizing, but also will preserve the image aspect ratio. The seam carving algorithm is applied only to the remaining points. Ex. : given an image of dimensions 2048x1536 if we want to resize to the 1024x500, the tool first rescale the image to 1024x768 and then will remove only the remaining 268px.
2021-12-04 14:55:43 +02:00
### Masks support:
2022-01-08 08:12:33 +02:00
- `-mask`: The path to the protective mask. The mask should be in binary format and have the same size as the input image. White areas represent regions where no seams should be carved.
- `-rmask`: The path to the removal mask. The mask should be in binary format and have the same size as the input image. White areas represent regions to be removed.
Mask | Mask removal
:-: | :-:
2022-10-24 13:51:52 +03:00
<video src='https://user-images.githubusercontent.com/883386/197509861-86733da8-0846-419a-95eb-4fb5a97607d5.mp4' width=180/> | <video src='https://user-images.githubusercontent.com/883386/197397857-7b785d7c-2f80-4aed-a5d2-75c429389060.mp4' width=180/>
### Caire integrations
2020-11-24 21:10:38 +02:00
- [x] Caire can be used as a serverless function via OpenFaaS: https://github.com/esimov/caire-openfaas
2019-03-29 12:57:36 +02:00
- [x] Caire can also be used as a `snap` function (https://snapcraft.io/caire): `$ snap run caire --h`
2019-06-03 16:58:09 +03:00
<a href="https://snapcraft.io/caire"><img src="https://raw.githubusercontent.com/snapcore/snap-store-badges/master/EN/%5BEN%5D-snap-store-white-uneditable.png" alt="snapcraft caire"></a>
2019-06-19 06:37:06 +03:00
## Results
2018-01-29 09:43:10 +02:00
#### Shrunk images
| Original | Shrunk |
2018-01-29 09:43:10 +02:00
| --- | --- |
| ![broadway_tower_edit](https://user-images.githubusercontent.com/883386/35498083-83d6015e-04d5-11e8-936a-883e17b76f9d.jpg) | ![broadway_tower_edit](https://user-images.githubusercontent.com/883386/35498110-a4a03328-04d5-11e8-9bf1-f526ef033d6a.jpg) |
| ![waterfall](https://user-images.githubusercontent.com/883386/35498250-2f31e202-04d6-11e8-8840-a78f40fc1a0c.png) | ![waterfall](https://user-images.githubusercontent.com/883386/35498209-0411b16a-04d6-11e8-9ce2-ec4bce34828a.jpg) |
| ![dubai](https://user-images.githubusercontent.com/883386/35498466-1375b88a-04d7-11e8-8f8e-9d202da6a6b3.jpg) | ![dubai](https://user-images.githubusercontent.com/883386/35498499-3c32fc38-04d7-11e8-9f0d-07f63a8bd420.jpg) |
| ![boat](https://user-images.githubusercontent.com/883386/35498465-1317a678-04d7-11e8-9185-ec92ea57f7c6.jpg) | ![boat](https://user-images.githubusercontent.com/883386/35498498-3c0f182c-04d7-11e8-9af8-695bc071e0f1.jpg) |
2018-01-29 09:54:49 +02:00
#### Enlarged images
2018-01-29 09:43:10 +02:00
| Original | Extended |
| --- | --- |
2018-01-31 20:19:18 +02:00
| ![gasadalur](https://user-images.githubusercontent.com/883386/35498662-e11853c4-04d7-11e8-98d7-fcdb27207362.jpg) | ![gasadalur](https://user-images.githubusercontent.com/883386/35498559-87eb6426-04d7-11e8-825c-2dd2abdfc112.jpg) |
2018-01-29 09:43:10 +02:00
| ![dubai](https://user-images.githubusercontent.com/883386/35498466-1375b88a-04d7-11e8-8f8e-9d202da6a6b3.jpg) | ![dubai](https://user-images.githubusercontent.com/883386/35498827-8cee502c-04d8-11e8-8449-05805f196d60.jpg) |
2018-01-28 14:37:10 +02:00
### Useful resources
* https://en.wikipedia.org/wiki/Seam_carving
* https://inst.eecs.berkeley.edu/~cs194-26/fa16/hw/proj4-seamcarving/imret.pdf
* http://pages.cs.wisc.edu/~moayad/cs766/download_files/alnammi_cs_766_final_report.pdf
* https://stacks.stanford.edu/file/druid:my512gb2187/Zargham_Nassirpour_Content_aware_image_resizing.pdf
2018-06-06 09:26:42 +03:00
## Author
2019-03-15 14:29:58 +02:00
* Endre Simo ([@simo_endre](https://twitter.com/simo_endre))
2018-01-29 09:43:10 +02:00
## License
2018-04-06 05:37:56 +03:00
Copyright © 2018 Endre Simo
2018-01-29 09:43:10 +02:00
This project is under the MIT License. See the LICENSE file for the full license text.