【问题标题】:Automatically Rotate Image Based on Text Content - PHP/JS根据文本内容自动旋转图像 - PHP/JS
【发布时间】:2015-05-21 16:37:03
【问题描述】:

我希望能够根据其文本内容自动旋转图像,以便正确(垂直)显示文本。我希望语言是 Javascript 或 PHP。

不好的例子

正确的例子

例如,GIMP 和 PS 在导入这样的图片时会这样做:

如何使用 JS/PHP 准确地自动旋转图像以使文本正确显示(如果您愿意,可以垂直显示)?

--注意--

我不想根据“EXIF 方向”数据进行旋转,而是根据图像中文本的方向进行旋转。显然,EXIF 数据只跟踪照片相对于地面的方向。

【问题讨论】:

  • 有一个 javascript 库可以为您执行此操作:stackoverflow.com/a/20600801/722617
  • 如果我没记错的话,您提供的链接没有提供我在 JS 中寻找的解决方案,并且 PHP 链接已失效。我添加了一条注释来解释我在寻找什么...

标签: javascript php ocr imagick


【解决方案1】:

我想到的一种可能的解决方案是使用 OCR 检测图像中的字符并在所有 4 个方向上测试图像(除了原始方向外,旋转 90 度 3 次)。无论哪个位置返回最高匹配字符,可能是文本的正确方向。

可以使用以下 PHP 库:https://github.com/thiagoalessio/tesseract-ocr-for-php。与imagerotate() 协作,可以根据从 OCR 返回的字符数量找出图像的最佳方向。

理论上

require_once '/path/to/TesseractOCR/TesseractOCR.php';

$filename='path/to/some/image.jpg';
$photo = // create photo from $filename
$results = array();

for ($i=0; $i<4; $i++) {
    $new = imagerotate($photo, $i*90, 0);
    $new_path = // save the new rotated photo and get path
    $tesseract = new TesseractOCR($new_path);
    $results[$i] = strlen($tesseract->recognize());
}

/* Highest output is the best orientation for the image in respects to the text in it */
echo "Original Orientation: " . $results[0];
echo "Rotated 90 degrees: " . $results[1];
echo "Rotated 180 degrees: " . $results[2];
echo "Rotated 270 degrees: " . $results[3];

优点 - 利用现有库(带有 PHP 包装器的 Tesseract、imagerotate php 函数)

缺点 - 计算密集型。一张图片需要旋转 3 次和 OCR 4 次

【讨论】:

    【解决方案2】:

    您要求的解决方案与您的示例不完全相同。起初我以为您想要一个检测文本的智能功能,但它比您的示例更简单。

    您需要查看 EXIF 数据。幸运的是,我已经多次这样做了。要校正已旋转的图像,您可以使用以下功能,我编写该功能用于校正在平板电脑/手机上拍摄但将显示在计算机上的图像。输入必须是 JPG 图像的文件名。

    function fix_image($filename, $max_dimension = 0)
    {
    
        $exif = exif_read_data($filename, 0, true);
        $o = $exif[IFD0][Orientation];
    
        // correct the image rotation
        $s = imagecreatefromjpeg($filename);
        if ($o == 1) { }
        if ($o == 3) { $s = imagerotate($s, 180, 0); }
        if ($o == 6) { $s = imagerotate($s, 270, 0); }
        if ($o == 8) { $s = imagerotate($s, 90, 0); }
    
        // export the image, rotated properly
        imagejpeg($s, $filename, 100);
        imagedestroy($s);
    
    
        if ($max_dimension > 0)
        {
    
         $i = getimagesize($filename);
    
         // reopen image for resizing
         // Use the known orientation to determine if it is portrait or landscape
         if ($i[0] > $i[1])
         {
          // landscape: make the width $max_dimension, adjust the height accordingly
          $new_width = $max_dimension;
          $new_height = $i[1] * ($max_dimension/$i[0]);
         } else {
          // portrait: make the height $max_dimension, adjust the width accordingly
          $new_height = $max_dimension;
          $new_width = $i[0] * ($max_dimension/$i[1]);
         }
    
         $s = imagecreatefromjpeg($filename);
         $n = imagecreatetruecolor($new_width, $new_height);
         imagecopyresampled($n, $s, 0, 0, 0, 0, $new_width, $new_height, $i[0], $i[1]);
         imagejpeg($n, $filename, 100);
    
         imagedestroy($n);
         imagedestroy($s);
        }
    }
    

    【讨论】:

    • 请注意我在问题中的“注意”部分。我不想要 EXIF 方向,本身...
    • 恐怕你需要比 PHP 和 javascript 更强大的东西。但是,如果一切都失败了,这对于自动旋转图像来说是一个很棒的功能。
    • 我不这么认为,我脑子里有一些想法可能会奏效,但过于密集......
    【解决方案3】:

    如果 exif 旋转不可接受,您可能需要进行一些图像处理。这永远不会 100% 准确。我不确定 karns 提出的 tessaract 解决方案是否能很好地工作,因为 tessaract 需要大量的培训,而且您可能总是会遇到未经培训的字体。此外,how to detect orientation of a scanned document? 上的评论表明 tessarct 会自动旋转图像以进行文本检测,因此您可能会在旋转后的图像上获得类似的结果。

    另一种方法是通过 php 包装器使用 opencv,例如https://github.com/mgdm/OpenCV-for-PHP(我自己没有使用过包装器)。然后,您可以做一个直线直方图,例如图片请参阅word segmentation using opencv 上接受的答案。通过这种方式,您可以确定图片是水平方向还是垂直方向。之后(并且在可能更正垂直方向的图片之后)您可以尝试确定文本是否颠倒,谷歌 detect upside down text 结果之一,例如,建议计算点在一条线的上部和下部。同样,这永远不会 100% 准确。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多