Skip to content

Instantly share code, notes, and snippets.

@begnini begnini/feijoes.php
Created Jul 13, 2018

Embed
What would you like to do?
Busca de feijoes em imagem
<?php
use function CV\imread;
use function CV\imwrite;
use function CV\threshold;
use function CV\findContoursWithoutHierarchy;
use function CV\boundingRect;
use function CV\rectangleByRect;
use function CV\cvtColor;
use const CV\THRESH_BINARY_INV;
use const CV\RETR_EXTERNAL;
use const CV\RETR_FLOODFILL;
use const CV\CHAIN_APPROX_SIMPLE;
use const CV\IMREAD_GRAYSCALE;
use const CV\COLOR_GRAY2RGB;
use CV\Scalar;
$binary = null;
$image = imread('feijao.jpg', IMREAD_GRAYSCALE);
threshold($image, $binary, 80, 255, THRESH_BINARY_INV);
imwrite('feijao_bw.jpg', $binary);
$contours = null;
findContoursWithoutHierarchy($binary, $contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
$boxes = array_map(function ($contour) {
return boundingRect($contour);
}, $contours);
$color = cvtColor($binary, COLOR_GRAY2RGB);
foreach ($boxes as $box) {
rectangleByRect($color, $box, new Scalar(0, 255, 255));
}
imwrite('feijao_bbox.jpg', $color);
$boxes = array_filter($boxes, function ($box) {
return $box->width > 20 && $box->height > 20;
});
echo 'Encontrados ' . count($boxes) . ' feijões na foto.';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.