tools/roi-visualize: upd

This commit is contained in:
Evgeny Zinoviev 2022-06-07 19:57:12 +03:00
parent 49106dd29d
commit 70a622e04b

View File

@ -16,7 +16,7 @@ function parse_roi_input(string $file): array {
$lines = array_map(fn($line) => array_map('intval', explode(' ', $line)), $lines); $lines = array_map(fn($line) => array_map('intval', explode(' ', $line)), $lines);
foreach ($lines as $points) { foreach ($lines as $points) {
if (count($points) != 4) if (count($points) != 4)
throw new Exception("invalid line: ".implode(' ', $points)); throw new Exception(__METHOD__.": invalid line: ".implode(' ', $points));
} }
return $lines; return $lines;
@ -57,8 +57,9 @@ $colors = [
if ($argc < 2) if ($argc < 2)
fatal("usage: {$argv[0]} --roi FILE --input PATH --output PATH\n"); fatal("usage: {$argv[0]} --roi FILE --input PATH --output PATH\n");
array_shift($argv); try {
while (count($argv) > 0) { array_shift($argv);
while (count($argv) > 0) {
switch ($argv[0]) { switch ($argv[0]) {
case '--roi': case '--roi':
array_shift($argv); array_shift($argv);
@ -76,28 +77,28 @@ while (count($argv) > 0) {
break; break;
default: default:
fatal('unsupported argument: '.$argv[0]); throw new Exception("unsupported argument: {$argv[0]}");
}
} }
}
if (!$roi_file) if (!$roi_file)
throw new Exception("--roi is not specified"); throw new Exception("--roi is not specified");
if (!$input) if (!$input)
throw new Exception('--input is not specified'); throw new Exception('--input is not specified');
if (!$output) if (!$output)
throw new Exception('--output is not specified'); throw new Exception('--output is not specified');
$regions = parse_roi_input($roi_file); $regions = parse_roi_input($roi_file);
$img = imageopen($input); $img = imageopen($input);
if (!$img) if (!$img)
throw new Exception("failed to open image"); throw new Exception("failed to open image");
$imgw = imagesx($img); $imgw = imagesx($img);
$imgh = imagesy($img); $imgh = imagesy($img);
foreach ($regions as $i => $region) { foreach ($regions as $i => $region) {
list($r, $g, $b) = hex2rgb($colors[$i]); list($r, $g, $b) = hex2rgb($colors[$i]);
if ($region[0]+$region[2] > $imgw || $region[1]+$region[3] > $imgh) if ($region[0]+$region[2] > $imgw || $region[1]+$region[3] > $imgh)
@ -108,7 +109,10 @@ foreach ($regions as $i => $region) {
$col = imagecolorallocatealpha($img, $r, $g, $b, 90); $col = imagecolorallocatealpha($img, $r, $g, $b, 90);
imagefilledrectangle($img, $region[0]+1, $region[1]+1, $region[0]+$region[2]-2, $region[1]+$region[3]-2, $col); imagefilledrectangle($img, $region[0]+1, $region[1]+1, $region[0]+$region[2]-2, $region[1]+$region[3]-2, $col);
} }
imagejpeg($img, $output, 97); imagejpeg($img, $output, 97);
echo "saved to $output\n"; echo "saved to $output\n";
} catch (Exception $e) {
fatal($e->getMessage()."\n");
}