php - PHP:计算图像中的对象组
问题描述
我需要自动计算此图像中的点。我们可以假设这些点总是红色的(如果有帮助的话,也可以是黑色的)并且在白色背景上总是这个大小。位置和计数经常变化
起初我以为我可以使用 Tesseract OCR 并且可以计算句点或项目符号,但无论我如何操作图像或白名单字符,结果都是空的。
我尝试的另一件事是使用 Imagick,有一个连接组件功能,但在我的测试中我使用了相同的代码并且没有得到任何结果......
我查看了 OpenCV,但 PHP 的唯一端口使用主要用中文编写的文档,并且不支持完整的库,因此将 Hough Transform 抛出窗口。
下面的代码: 最后,我冒险使用 PHP 读取图像并循环遍历像素并找到某个阈值之外的任何内容,但我坚持如何将单个像素实际分组到一个对象。-- 一个注意事项是,尽管没有内存限制和执行时间限制,但我无法让它与全尺寸图像一起使用(即使它们已经按比例缩小)。
我在这上面花了一天半的时间,它看起来很简单,形状一致,但没有......
// creating the initial image
$starting_img = imagecreatefrompng("converted.png");
//Dimentions
$imgDims = getimagesize($source_image);
$scanWidth = $imgDims[0];
$scanHeight = $imgDims[1];
//New image
$final = imagecreatetruecolor($scanWidth,$scanHeight);
$white = imagecolorallocate($final, 255, 255, 255);
imagefill($final,0,0,$white);
$imageData = array();
for ($y=1; $y < $scanHeight - 1; $y++) {
// Get First Row, the scan each row col
$currentRow = array();
for ($x=1; $x < $scanWidth - 1; $x++) {
// Get first col in row 1 and check its data
$rgb = imagecolorat($starting_img,$x,$y);
$color = array(
'r' => ($rgb >> 16) & 255,
'g' => ($rgb >> 8) & 255,
'b' => $rgb & 255
);
$currentCol = array(
'x' => $x,
'y' => $y,
'r' => $color['r'],
'g' => $color['g'],
'b' => $color['b'],
);
//Is the pixel red?
// if ($color['r'] > 200 && $color['g'] < 100 && $color['b'] < 100) {
array_push($currentRow, $currentCol);
// }
}
//Does this row have any red pixels?
// if (count($currentRow) > 1) {
array_push($imageData, $currentRow);
// }
}
foreach ($imageData as $currentRow => $row) {
foreach ($row as $currentCol => $col) {
$newColoredPixel = imagecolorallocate($final,$col['r'],$col['g'],$col['b']);
// adding the new pixel to the new image
imagesetpixel($final,$col['x'],$col['y'],$newColoredPixel);
}
}
file_put_contents('imageData.txt', print_r($imageData, true));
imagepng($final,"final.png");
imagedestroy($final);
imagedestroy($starting_img);
解决方案
好问题!
如果 blob 不重叠 - 您可以通过公式计算它们:
blob_count = total_blob_area / blob_unit_area
在您的情况下, blob 是一个圆圈,因此公式变为:
blob_count = total_blob_area / (π * r^2)
total_blob_area 只是一个带红色的像素数,但您必须凭经验找到 blob 单位直径。
顺便说一句,仅当图像是真彩色类型时,函数 imagecolorat() 才返回 RGB 值,但是您给定的 PNG 具有索引颜色空间,因此能够从中提取真正的 RGB 值 - 您必须将 imagecolorat() 的输出传递给imagecolorsforindex() .
上面描述的blob计数方法的代码是这样的:
function countBlobs($imfile, $blob_diameter) {
$blob_area = pi()*pow($blob_diameter/2, 2);
$im = imagecreatefrompng($imfile);
list($width, $height, $type, $attr) = getimagesize($imfile);
$total_blob_area = 0;
for ($x=0; $x < $width; $x++) {
for ($y=0; $y < $height; $y++) {
$rgb = imagecolorat($im, $x, $y);
$colors = imagecolorsforindex($im, $rgb);
$avg = ($colors['red']+$colors['green']+$colors['blue'])/3;
if ($avg < 150)
{
$total_blob_area++;
}
}
}
$blobs = $total_blob_area / $blob_area;
return round($blobs);
}
echo ('blobs : ' . countBlobs('countblobs.png', 16));
您可以使用 GIMP 中的距离测量工具找到 Blob 单位直径。但是,如果您的 blob 可以具有不同的形状/大小 - 那么您将需要对 blob 单位面积进行某种平均。
推荐阅读
- exe - 我需要一个 exe 文件来锁定一个 sertan 文件,直到他们登录到他们的帐户
- python - 使用 Lambda-python 对 DynamoDB 表的 sed 识别响应
- visual-studio-code - VSCode Latex-workshop:onFileChange 没有 AutoBuild
- git - 找不到 GIT 日志漂亮格式的任何文件占位符
- python - reportlab SimpleDocTemplate - 设置具有可变行数的表格的固定高度
- c++ - 创建从 sql 到 Picture 的 blob 流
- amazon-web-services - AWS 中 Cassandra 的节点连接问题
- reactjs - RN - 在样式声明中使用道具
- drupal - Drupal 8,打印表单并将数据发送到数据库
- performance - StatelessWidget 与在性能方面返回 Widget 的函数