Created
July 13, 2018 02:23
-
-
Save begnini/7442c744472eb9137d2f20c551434c3f to your computer and use it in GitHub Desktop.
Busca de feijoes em imagem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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