2015-12-09 27 views
-4

我需要识别图像中的文本,然后裁剪出矩形文本缩小图像尺寸OpenCV的用PHP - 文字识别

的第一步是认识到文本,我已经找到了解决办法在这里

Extracting text OpenCV

但如何编译和运行PHP呢?我不熟悉C

如何编译代码,以便您可以将参数传递给程序像exec('opencv_test input.jpg output.jpg')

而是绘制矩形围绕每一个文本块中的程序应该分析所有文本块,并获得两个坐标在图像应该被裁剪..然后裁剪图像的输出文件写入到输出目的地

代码

#include "stdafx.h" 

#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <opencv2/imgproc/imgproc.hpp> 
#include <iostream> 

using namespace cv; 
using namespace std; 

#define INPUT_FILE    "1.jpg" 
#define OUTPUT_FOLDER_PATH  string("") 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    Mat large = imread(INPUT_FILE); 
    Mat rgb; 
    // downsample and use it for processing 
    pyrDown(large, rgb); 
    Mat small; 
    cvtColor(rgb, small, CV_BGR2GRAY); 
    // morphological gradient 
    Mat grad; 
    Mat morphKernel = getStructuringElement(MORPH_ELLIPSE, Size(3, 3)); 
    morphologyEx(small, grad, MORPH_GRADIENT, morphKernel); 
    // binarize 
    Mat bw; 
    threshold(grad, bw, 0.0, 255.0, THRESH_BINARY | THRESH_OTSU); 
    // connect horizontally oriented regions 
    Mat connected; 
    morphKernel = getStructuringElement(MORPH_RECT, Size(9, 1)); 
    morphologyEx(bw, connected, MORPH_CLOSE, morphKernel); 
    // find contours 
    Mat mask = Mat::zeros(bw.size(), CV_8UC1); 
    vector<vector<Point>> contours; 
    vector<Vec4i> hierarchy; 
    findContours(connected, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0)); 
    // filter contours 
    for(int idx = 0; idx >= 0; idx = hierarchy[idx][0]) 
    { 
     Rect rect = boundingRect(contours[idx]); 
     Mat maskROI(mask, rect); 
     maskROI = Scalar(0, 0, 0); 
     // fill the contour 
     drawContours(mask, contours, idx, Scalar(255, 255, 255), CV_FILLED); 
     // ratio of non-zero pixels in the filled region 
     double r = (double)countNonZero(maskROI)/(rect.width*rect.height); 

     if (r > .45 /* assume at least 45% of the area is filled if it contains text */ 
      && 
      (rect.height > 8 && rect.width > 8) /* constraints on region size */ 
      /* these two conditions alone are not very robust. better to use something 
      like the number of significant peaks in a horizontal projection as a third condition */ 
      ) 
     { 
      rectangle(rgb, rect, Scalar(0, 255, 0), 2); 
     } 
    } 
    imwrite(OUTPUT_FOLDER_PATH + string("rgb.jpg"), rgb); 

    return 0; 
} 
+0

你需要编写自己的包装。 http://www.xarg.org/project/php-facedetect/ – GPPK

+0

我感到惊讶与5 downvotes!问题并不坏,并要求如何从PHP调用C++代码! –

+1

@HumamHelfawi,那是每日生活在stackoverflow – clarkk

回答

0

你可以看到GPPK评论。它应该适合你。但是,如果你需要一些快速的方式,而没有太多额外的库和包装。只需将你的C++代码编译成一个可执行文件(比如exe或者你的平台上的任何文件)并且放置一个参数可能性(将图像路径作为参数传递)。然后,我认为你可以从你的PHP代码中调用它。据我所知PHP是服务器端,所以它应该是一件容易的事情。但是,您可能会要求您的托管服务或服务器管理员的权限,以使您可以在服务器上运行exe文件。

+0

好吧,但你如何编译代码? :) – clarkk

+0

由您的平台的任何C++编译器。告诉任何C++开发人员你在哪里工作,他会在5分钟内为你做 –