Automatic image focal point detection.
Autofocus is an image analysis library which attempts to determine where the focal point of an image is.
composer require freshleafmedia/autofocus
use FreshleafMedia\Autofocus\FocalPointDetector;
$focalPointDetector = new FocalPointDetector();
$point = $focalPointDetector->getPoint(new Imagick('/path/to/image'));
$point->x; // 283
$point->y; // 157
The algorithm works by picking the busiest area of the image:
- Detect edges
- Apply a vignette. This pushes the focus towards the centre
- Split the image into segments
- Calculate the average brightness within each segment
- Pick the segment with the highest brightness
The FocalPointDetector
constructor takes a number of options:
new FocalPointDetector(
segmentSize: 20, // The size of the Segments in pixels
);
You can get access to the processed image to see how the focus was determined:
use FreshleafMedia\Autofocus\FocalPointDetector;
$focalPointDetector = new FocalPointDetector();
$focalPointDetector
->debug(new Imagick('/path/to/image'))
->drawHeatMap()
->drawGrid()
->getRawImage()
->writeImage(__DIR__ . '/debug.jpg');
Unit tests can be run via composer test
This library is essentially a PHP port of https://github.com/sylvainjule/kirby-autofocus.
See LICENSE