图像裁剪和比较


Image crop and comparision

当我向数据库提交从pdf中提取的新图像时,它应该是原始图像的裁剪图像。如果该图像已经存在于数据库中,则不应插入该图像,如果未插入,则我必须为此生成一个标识值。

标识值也将插入到与图像相同的表中。

这里面有钥匙。表格页面具有以下标识

pid、qid、pidentifierval、图像

$record = array('pid' => "NULL",
        'qid' => $qid,
        'pidentifierval' => $pid,
        'image' => $crop,
        'rotation' => 0);

函数newquestionary($filename,$desc=",$type="pngmono"){

global $db;
if ($desc == "") $desc = $filename;
//generate temp file
$tmp = tempnam(TEMPORARY_DIRECTORY, "FORM");
//print "Creating PNG files<br/>";
//use ghostscript to convert to PNG
exec(GS_BIN . " -sDEVICE=$type -r300 -sOutputFile='"$tmp'"%d.png -dNOPAUSE -dBATCH '"$filename'"");
//add to questionnaire table
//
//create form entry in DB
//
$db->StartTrans();
$sql = "INSERT INTO questionnaires (qid,description,sheets)
    VALUES (NULL,'$desc',0)";
$db->Execute($sql);
$qid = $db->Insert_Id();
//Number of imported pages
$pages = 0;
//read pages from 1 to n - stop when n does not exist
$n = 1;
$file = $tmp . $n . ".png";
while (file_exists($file))
{       
    $data = file_get_contents($file);
    $images = split_scanning($data);
    unset($image);
    unset($data);
    foreach($images as $data)
    {
        //header Cropped Function
       // Original image
        $filename = $data;
      // Get dimensions of the original image
       list($current_width, $current_height) = getimagesize($filename);
     // The x and y coordinates on the original image where we
     // will begin cropping the image
      $left = 50;
      $top = 50;
    // This will be the final size of the image (e.g. how many pixels
   // left and down we will be going)
   $crop_width = 200;
   $crop_height = 200;
     // Resample the image
   $canvas = imagecreatetruecolor($crop_width, $crop_height);
   $current_image = imagecreatefromjpeg($filename);
   imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height);
   $crop = imagepng($canvas, $filename, 100);
//check for header Cropped Image

我应该在这里做什么函数?

        $pid = $pid.$pages;
        if ($pid)
        {
            $pages++;
                $record = defaultpage($qid,$pid,$crop);
                $db->AutoExecute('pages',$record,'INSERT'); 
        }
        else
            print T_("INVALID - IGNORING BLANK PAGE");
        unset($data);
        unset($crop);

我很困惑,我应该如何检查和比较数据库中是否存在图像。请帮助

用于裁剪图像

我找到了的解决方案

$data = '1.png';
list($current_width, $current_height) = getimagesize($data);
            // The x and y co-ordinates on the original image where we will begin cropping the image
            $left = 1100;
            $top = 30;
            // final size of image
            $crop_width = 200;
            $crop_height = 200;
            //Resample the Image
            $canvas = imagecreatefromtruecolor($crop_width, $crop_height);
            $current_image = imagecreatefrompng($data);
            imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height);
            imagepng($canvas, $data, 100);

用于比较两个图像

array image_compare(mixed $image1, mixed $image2, int $RTolerance, int $GTolerance, int $BTolerance, int $WarningTolerance, int $ErrorTolerance)

参数图像1-包含映像路径的字符串,或GD库已创建的映像的资源。这是要比较的第一张图像。图像2-一个字符串,包含第二个映像的路径,或者GD库已经创建的第二个图像的资源。这将是要比较的第二张图像。R公差,G公差,B公差(0-255)-指定抛出标志之前红色、绿色或蓝色通道的最大偏差。警告容差(0-100)-返回警告之前的通道差异百分比。容错(0-100)-返回错误(标志)之前的通道差异百分比。

返回值将返回一个数组,其中包含以下信息:

像素(按颜色)-像素数量*3(用于R、G和B通道中的每一个)。像素超出规格-(如果每个红色、绿色和蓝色的像素在xTolerance之外变化。其中x=R/G/B)如果任何通道超过阈值,则该数字将递增。百分比差异-PixelsOutOfSpec与PixelsByColors之间的差异百分比警告级别和错误级别-如果百分比大到足以触发指定的警告或错误级别。

为了比较我必须工作,我正在考虑使用数组。使用的Select查询从数据库中获取图像,使用while循环并在数组中获取结果,调用数组键以将图像存储在变量中,并在上面的比较函数中使用if-else条件,我认为这将起作用。你们是怎么想的?

对于图像比较,无扫描图像可以100%准确,因此图像比较并不容易。这是一项相当繁忙的任务。

经过大量的研究和工作,我得出了一个观点,如果100%有必要进行图像比较。我将不得不使用博士OpenCV库。如果我必须允许某种容错,上面的类可以正常工作。我的工作可以使用Php-tesseract实现。我只是简单地使用了tesseract OCR。我使用重影脚本将PDF转换为png,裁剪图像,使用谷歌代码网站上的Php-tesseract OCR库将图像的特定部分转换为文本。将该文本调用到变量中,使用正则表达式我能够检查变量中是否存在特定文本,并根据我的要求在条件中使用它。

把这看作是我问题的结束。

为了方便访问者,我正在粘贴我的代码段,以便可以使用它。

// Cropping the image
        // Get dimensions of the original image

        list($current_width, $current_height) = getimagesize($file);
       // The x and y co-ordinates on the original image where we will begin cropping the image
       $left = 1100;
       $top = 30;
       // final size of image
       $crop_width = 700;
       $crop_height = 200;
       //Resample the Image
       $canvas = imagecreatetruecolor($crop_width,$crop_height);
       $current_image = imagecreatefrompng($file);
       imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height);
       imagepng($canvas, $file, 1);
        // Note you will have to install Php tesseract Library before making the API Call.
        $api= new TessBaseAPI;
        $api->Init(".","eng",$mode_or_oem=OEM_DEFAULT);
        $api->SetPageSegMode(PSM_AUTO);
        $mImgFile = $file;
        $handle=fopen($mImgFile,"rb");
        $mBuffer=fread($handle,filesize($mImgFile));
        //print strlen($mBuffer);
        $result=ProcessPagesBuffer($mBuffer,strlen($mBuffer)*4,$api);
        //print $result;
        $result = ProcessPagesFileStream($mImgFile,$api);
        //print "result(ProcessPagesFileStream)=";
        print $result;
        $txtchk = 'FORM';
        if(preg_match("/$txtchk/i", $result)) {
        echo true;
        }

我希望这对很多人都有帮助。