douou1872 2017-03-10 23:55
浏览 71
已采纳

PHP - 快速循环图像像素

Good afternoon SO Community,

I've been working on a project that calls for some optical character recognition. I am trying to keep the project light and portable, so installing third party programs won't be an option here.

Anyways, I decided to write my own OCR in PHP, but it loops through the image EXTREMELY slow. The way I'm currently doing it, is two nested for loops. I'm trying to loop through a given image (In this case, the image is a PNG. 263x55 pixels), and write the rgba to a text file. (Format: 'rgba(0, 0, 0, 0)'). The alpha is between 0 and 127 since it is using PHP.

My code works, but is very slow, and the image really isn't that large. Can you think of any way I could speed this up?

Thanks in advance,

Tim

<?php
// To prevent the script from timing out
ini_set('max_execution_time', 0);
If (isset($_GET["Image"])) {
    $pImage = $_GET["Image"];
} Else {
    $pImage = "1";
}
parseImage($pImage);

// END TEST SYSTEM

Function parseImage($ImgNum) {
    Echo "Parsing Image $ImgNum";
    $logFile = "Image$ImgNum.txt";
    $fHandle = fopen($logFile, "w");
    If ($ImgNum != 1 AND $ImgNum != 2 AND $ImgNum != 3 AND $ImgNum != 4 AND $ImgNum != 5 AND $ImgNum != 6) {
        Echo "Error: Image number is invalid.";
        Exit();
    }

    // Start Optical Character Recognition
    $Image = "https://www.example.com/img/Image$ImgNum.png";
    $size   = getimagesize($Image);
    $width  = $size[0];
    $height = $size[1];
    $ctrH = 0;
    $ctrW = 0;

    for($x=1;$x<=$width;$x++) {
        for($y=1;$y<=$height;$y++) {
            $pixel = getPixel($Image, $x, $y);
            fwrite($fHandle, $pixel . "
");
            $ctrH ++;
        }
        $ctrW ++;
    }
    fclose($fHandle);

    Echo "Analyzing <a href='$Image'>$Image</a><br />";
    Echo $ctrW . "px wide<br />";
    Echo ($ctrH / $ctrW) . "px tall<br />";
}


function getPixel($image, $x, $y) {
    // Echo "<br />Reading $image. X: $x - Y: $y<br />";
    $im = imagecreatefrompng($image);
    $rgb = imagecolorat($im, $x, $y);
    $colors = imagecolorsforindex($im, $rgb);
    $r = $colors["red"];
    $g = $colors["green"];
    $b = $colors["blue"];
    $a = $colors["alpha"];
    $print = "Pixel (" . $x . "x" . $y . "): rgba($r, $g, $b, $a)";
    return $print;
}
?>
  • 写回答

1条回答 默认 最新

  • donglei3370 2017-03-11 00:30
    关注

    Your issue is that you are creating the image every time you lookup the value of a pixel by having imagecreatefrompng inside your getPixel function. Move this outside your getPixel function and nested loops, and pass it in instead.

    This way you do not carry the overhead of exploding the image into memory, looking up a pixel, then having that work destroyed by the garbage collector as the function exits, only to do it all over again next pixel.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器