opencv+tesseract小程序

#include <string>
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/types_c.h>
#include <sys/time.h>

using namespace std;
using namespace cv;

/* remove blank of text */
inline void trim(string &s)
{
	int index = 0;
	if (!s.empty())
	{
		while((index = s.find(' ',index)) != string::npos)
		{
			s.erase(index, 1);
		}
	}
}

int main(int argc, char* argv[])
{
    string outText, price;
    string imPath = argv[1];
    Mat imgGray, result;
	int flag = 0;
    struct timeval beg,end;

    // Create Tesseract object
    tesseract::TessBaseAPI *ocr = new tesseract::TessBaseAPI();
     
    // Initialize tesseract to use English (eng) and the LSTM OCR engine. 
    ocr->Init("/home/moon/program/tmp/tessdata", "chi_sim", tesseract::OEM_LSTM_ONLY);
 
    ocr->SetVariable("tessedit_write_images", "true");
    // Set Page segmentation mode to PSM_AUTO (3)
    ocr->SetPageSegMode(tesseract::PSM_AUTO);
 
    // Open input image using OpenCV
    Mat im = cv::imread(imPath, IMREAD_COLOR);
    Rect rect_one(0, im.rows * 100 / 192, im.cols, im.rows * 138 / 1920);
    Rect rect_two(0, im.rows * 130 / 192, im.cols, im.rows * 138 / 1920);
    cout << "rows:" << im.rows << " cols:" << im.cols << endl;
    gettimeofday(&beg, NULL);
    Mat im_one = im(rect_one);
    Mat im_two = im(rect_two);
    Mat im_real(im_one.rows + im_two.rows, im_one.cols, im_one.type());
    im_one.rowRange(0, im_one.rows).copyTo(im_real.rowRange(0, im_one.rows));
    im_two.rowRange(0, im_two.rows).copyTo(im_real.rowRange(im_one.rows, im_real.rows)); 
    ocr->SetImage(im_real.data, im_real.cols, im_real.rows, 3, im_real.step);
 
    outText = string(ocr->GetUTF8Text());
    trim(outText);
    // print recognized text
    cout << outText << endl; // Destroy used object and release memory ocr->End();
    int position = outText.find("需要车型");
    if (position != outText.npos)
    {
		flag++;
		position = outText.find("已支付羊");
		if (position != outText.npos)
		{
			flag++;
			price = outText.substr(position + 12);
		}
    }
	if (flag == 0)
	{
		cout << "no order coming" << endl;
	}
	else if (flag == 1)
	{
		cout << "order is coming" << endl;
	}
	else if (flag == 2)
	{
		cout << "order price is: " << price << endl;
	}
    gettimeofday(&end, NULL);
    double time = end.tv_sec - beg.tv_sec + (double)(end.tv_usec - beg.tv_usec)/1000000.0;
    cout << "recognize time is: " << time << endl;
    return EXIT_SUCCESS;
}
Rect rect_one(0, im.cols * 1020 / 1080, im.cols / 4, im.cols * 110 / 1080);
Rect rect_two(0, im.cols * 1100 / 1080, im.cols / 4, im.cols * 228 / 1080);
Rect rect_three(im.cols * 3 / 4, im.cols * 1100 / 1080, im.cols / 4, im.cols * 228 / 1080);
Mat im_one = im(rect_one);
Mat im_two = im(rect_two);
Mat im_three = im(rect_three);
Mat im_real(im_one.rows + im_two.rows + im_three.rows, im_one.cols, im_one.type());
im_one.rowRange(0, im_one.rows).copyTo(im_real.rowRange(0, im_one.rows));
im_two.rowRange(0, im_two.rows).copyTo(im_real.rowRange(im_one.rows, im_one.rows + im_two.rows));
im_three.rowRange(0, im_three.rows).copyTo(im_real.rowRange(im_one.rows + im_two.rows, im_real.rows));
imwrite("/data/local/tmp/aaa.PNG", im_real);
string outText = imageToString(im_real.data, im_real.cols, im_real.rows, 3, im_real.step);
void transToBinary(Mat src, Mat* dst, int* time, int thresh)
{
    Mat gray;
//    ostringstream oss;
//    ostringstream oss1;
    cvtColor(src, gray, CV_BGR2GRAY);
//    oss << "gray_" << *time<< ".jpg";
//    cout << oss.str();
//    imwrite(oss.str(), gray);
    threshold(gray, *dst, thresh, 255, THRESH_BINARY);
//    oss1 << "binary_" << *time << ".jpg";
//    imwrite(oss1.str(), *dst);
 //   *time = *time + 1;
}
    gettimeofday(&beg, NULL);
    Rect rect_one(0, im.cols * 1020 / 1080, im.cols / 4, im.cols * 110 / 1080);
    Mat im_one = im(rect_one);
    Mat binary;
    transToBinary(im_one, &binary, &times, 125);

    Rect rect_two(im.cols * 3 / 4, im.cols * 1020 / 1080, im.cols / 4, im.cols * 110 / 1080);
    Mat im_two = im(rect_two);
    Mat binary_two;
    transToBinary(im_two, &binary_two, &times, 125);

    Rect rect_three(0, im.cols * 1080 / 1080, im.cols / 4, im.cols * 330 / 1080);
    Mat im_three = im(rect_three);
    Mat binary_three;
    transToBinary(im_three, &binary_three, &times, 40);

    Rect rect_four(im.cols * 3 / 4, im.cols * 1080 / 1080, im.cols / 4, im.cols * 330 / 1080);
    Mat im_four = im(rect_four);
    Mat binary_four;
    transToBinary(im_four, &binary_four, &times, 40);

    Mat tmp1(binary.rows + binary_three.rows, binary.cols, binary.type());
    Mat tmp2(binary_two.rows + binary_four.rows, binary_two.cols, binary_two.type());
    Mat im_real(tmp1.rows, tmp1.cols + tmp2.cols, tmp1.type());
    binary.rowRange(0, binary.rows).copyTo(tmp1.rowRange(0, binary.rows));
    binary_three.rowRange(0, binary_three.rows).copyTo(tmp1.rowRange(binary.rows, binary.rows + binary_three.rows));
    binary_two.rowRange(0, binary_two.rows).copyTo(tmp2.rowRange(0, binary_two.rows));
    binary_four.rowRange(0, binary_four.rows).copyTo(tmp2.rowRange(binary_two.rows, binary_two.rows + binary_four.rows));

    tmp1.colRange(0, tmp1.cols).copyTo(im_real.colRange(0, tmp1.cols));
    tmp2.colRange(0, tmp2.cols).copyTo(im_real.colRange(tmp1.cols, im_real.cols));

    imwrite("im_real.jpg", im_real);

    ocr->SetImage(im_real.data, im_real.cols, im_real.rows, 1, im_real.step);
    outText = string(ocr->GetUTF8Text());
    trim(outText);
    cout << outText << endl; // Destroy used object and release memory ocr->End();
    gettimeofday(&end, NULL);
    double time = end.tv_sec - beg.tv_sec + (double)(end.tv_usec - beg.tv_usec)/1000000.0;
    cout << "recognize time is: " << time << endl;
    return 0;
void transToBinary(Mat src, Mat* dst, int* time, int thresh_max, int thresh_min)
{
    Mat gray;
    Mat tmp;
    ostringstream oss;
    ostringstream oss1;
    ostringstream oss2;
    cvtColor(src, gray, CV_BGR2GRAY);
    oss2 << "gray_" << *time<< ".jpg";
    imwrite(oss2.str(), gray);

    threshold(gray, tmp, thresh_max, 255, THRESH_TOZERO_INV);
    oss1 << "binary_" << *time << ".jpg";
    imwrite(oss1.str(), tmp);

    threshold(tmp, *dst, thresh_min, 255, THRESH_BINARY_INV);
    oss << "binary_final_" << *time << ".jpg";
    imwrite(oss.str(), *dst);

    *time = *time + 1;
}


    gettimeofday(&beg, NULL);
#if 1
    Rect rect_one(0, im.cols * 1020 / 1080, im.cols / 4, im.cols * 110 / 1080);
    Mat im_one = im(rect_one);
    Mat binary;
    transToBinary(im_one, &binary, &times, 125, 115); //119 51

    Rect rect_two(im.cols * 3 / 4, im.cols * 1020 / 1080, im.cols / 4, im.cols * 110 / 1080);
    Mat im_two = im(rect_two);
    Mat binary_two;
    transToBinary(im_two, &binary_two, &times, 0, 255);

    Rect rect_three(0, im.cols * 1080 / 1080, im.cols / 4, im.cols * 330 / 1080);
    Mat im_three = im(rect_three);
    Mat binary_three;
    transToBinary(im_three, &binary_three, &times, 40, 30); // 34

    Rect rect_four(im.cols * 3 / 4, im.cols * 1080 / 1080, im.cols / 4, im.cols * 330 / 1080);
    Mat im_four = im(rect_four);
    Mat binary_four;
    transToBinary(im_four, &binary_four, &times, 40, 30); //34

    Mat tmp1(binary.rows + binary_three.rows, binary.cols, binary.type());
    Mat tmp2(binary_two.rows + binary_four.rows, binary_two.cols, binary_two.type());
    Mat im_real(tmp1.rows, tmp1.cols + tmp2.cols, tmp1.type());
    binary.rowRange(0, binary.rows).copyTo(tmp1.rowRange(0, binary.rows));
    binary_three.rowRange(0, binary_three.rows).copyTo(tmp1.rowRange(binary.rows, binary.rows + binary_three.rows));
    binary_two.rowRange(0, binary_two.rows).copyTo(tmp2.rowRange(0, binary_two.rows));
    binary_four.rowRange(0, binary_four.rows).copyTo(tmp2.rowRange(binary_two.rows, binary_two.rows + binary_four.rows));

    tmp1.colRange(0, tmp1.cols).copyTo(im_real.colRange(0, tmp1.cols));
    tmp2.colRange(0, tmp2.cols).copyTo(im_real.colRange(tmp1.cols, im_real.cols));

    imwrite("im_real.jpg", im_real);
//    int start_rows = getIndex(im_real, 0);
//    int end_rows = getIndex(im_real, im_real.rows);
//    if (start_rows >= end_rows)
//    {
        ocr->SetImage(im_real.data, im_real.cols, im_real.rows, 1, im_real.step);

#endif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮助您用 Java 使用 OpenCVTesseract 识别验证码文字并标注文字坐标。 首先,您需要安装 OpenCVTesseract 的 Java 绑定,并将它们添加到您的项目中。然后,您可以使用 OpenCV 读入图像并使用 Tesseract 识别文字。 下面是一个基本的示例代码,它可以帮助您开始使用 OpenCVTesseract 进行文字识别: ``` import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfRect; import org.opencv.core.Point; import org.opencv.core.Rect; import org.opencv.core.Scalar; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; import org.opencv.objdetect.CascadeClassifier; import com.google.common.io.Files; import net.sourceforge.tess4j.ITesseract; import net.sourceforge.tess4j.Tesseract; import net.sourceforge.tess4j.TesseractException; import java.io.File; import java.io.IOException; public class TextRecognition { public static void main(String[] args) throws IOException, TesseractException { // Load the OpenCV library System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // Load the image file Mat image = Imgcodecs.imread("captcha.png"); // Convert the image to grayscale Mat imageGray = new Mat(); Imgproc.cvtColor(image, imageGray, Imgproc.COLOR_BGR2GRAY); // Use a cascade classifier to detect the characters in the image CascadeClassifier classifier = new CascadeClassifier("char_classifier.xml"); MatOfRect characterRegions = new MatOfRect(); classifier.detectMultiScale(imageGray, characterRegions); // Loop through the character regions and recognize the text in each region ITesseract tesseract = new Tesseract(); tesseract.setLanguage("eng"); for (Rect rect : characterRegions.toArray()) { // Crop the character region from the image Mat characterRegion = new Mat(imageGray, rect); // Perform OTSU thresholding on the character region Mat characterRegionThreshold = new Mat(); Imgproc.th

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值