Tesseract调用日文识别模型

调整的好的话 测试准确率80%以上

int main(int argc, char** argv)
{
	// Usage: tesscv image.png
	system("chcp 65001");
	string path = "D:/Image/8/";
	vector<string> files;
	//getAllFiles(path, files);
	//for (auto file : files)
//	{
		// Load image
		cv::Mat im = cv::imread("21.jpg");
		if (im.empty())
		{
			std::cout << "Cannot open source image!" << std::endl;
			return -1;
		}
		cv::Mat gray;
		cv::cvtColor(im, gray, CV_BGR2GRAY);
		// ...other image pre-processing here...
		Mat threImage;
		threshold(gray, threImage,150, 255, THRESH_BINARY_INV);
		
		Mat horizontalStructure = getStructuringElement(MORPH_RECT, Size(15,2));
		dilate(threImage, threImage, horizontalStructure, Point(-1, -1));
		//imshow("阈值处理", threImage);
		//waitKey(0);
		//初始化模型
		tesseract::TessBaseAPI tess;
		tess.Init(NULL, "Japanese", tesseract::OEM_DEFAULT);
		tess.SetPageSegMode(tesseract::PSM_SINGLE_BLOCK);

		vector<vector<Point>> contours;
		vector<Vec4i> hierarchy;
		findContours(threImage, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0));
		vector<Rect> boundRect(contours.size());
		for (int i = 0; i < contours.size(); i++)
		{
			if (contourArea(contours[i])>5000 || contourArea(contours[i]) < 100)
			{
				continue;
			}
			else
			{
				boundRect[i] = boundingRect(Mat(contours[i]));
				//rectangle(im, boundRect[i].tl(), boundRect[i].br(), (0, 0, 255), 1, 8, 0);
				Mat temp = gray(Rect(boundRect[i].x, boundRect[i].y-2, boundRect[i].width, boundRect[i].height+4));
				//
				copyMakeBorder(temp, temp, 5, 5, 5, 5, BORDER_REFLECT_101);
				//threshold(temp,temp,0,255,THRESH_OTSU);
				//copyMakeBorder(temp, temp, 5, 5, 5, 5, BORDER_CONSTANT,Scalar(255));
				//imshow("temp", temp);
				//waitKey(0);
				

				tess.SetImage((uchar*)temp.data, temp.cols, temp.rows, 1, temp.cols);
				char* out = tess.GetUTF8Text();
				std::cout << out << std::endl;
				imshow("temp", temp);
				waitKey(0);
				//putTextZH(im, out, Point(boundRect[i].x, boundRect[i].y - 10), Scalar(255, 0, 0),20, "华文行楷");
			}				
		}
		//imwrite("srcImage.jpg", im);
		cout << "suucess" << endl;
		waitKey(0);
		

#if 0
		// Pass it to Tesseract API
		tesseract::TessBaseAPI tess;
		tess.Init(NULL, "Japanese", tesseract::OEM_DEFAULT);
		tess.SetPageSegMode(tesseract::PSM_SINGLE_BLOCK);
		tess.SetImage((uchar*)threImage.data, threImage.cols, threImage.rows, 1, threImage.cols);

		// Get the text
		char* out = tess.GetUTF8Text();
		std::cout << out << std::endl;
#endif 
	//}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值