opencv practice project - recognize characters in pictures

1. Process

• Read images
• Grayscale images
• Dilate and erode to remove unwanted noise from images.
• Write images after removing noise.
• Apply thresholding to obtain unique black and white images.
• Write the same image for further recognition process.
• Character recognition using Tesseract.

2. Code implementation

int main()
{
    
    
	char* path = "E:\\code\\Yolov5_Tensorrt_Win10-master\\pictures\\2.png";
	Mat src = imread(path);
	if (src.empty()) {
    
    
		return -1;
	}
	cv::Mat gray;
	cv::cvtColor(src, gray, COLOR_BGR2GRAY);
	cv::Mat kernel = cv::getStructuringElement(MORPH_RECT, Size(1, 1));
	cv::dilate(gray, gray, kernel, cv::Point2d(-1, -1), 20);
	cv::erode(gray, gray, kernel, cv::Point2d(-1, -1), 20);
	cv::adaptiveThreshold(gray, gray, 300, cv::ADAPTIVE_THRESH_GAUSSIAN_C, cv::THRESH_BINARY, 31, 2);
	cv::imshow("gray", gray);
	cv::waitKey(0);
	cv::imwrite("threshold.tiff", gray);
	
	char* outText;

	tesseract::TessBaseAPI* api = new tesseract::TessBaseAPI();
	// Initialize tesseract-ocr with English, without specifying tessdata path
	char* config = "--oem 1";
	if (api->Init("E:\\code\\Yolov5_Tensorrt_Win10-master\\build\\3rdparty\\tesseract-3.05.01", "eng", tesseract::OcrEngineMode::OEM_DEFAULT)) {
    
    
		fprintf(stderr, "Could not initialize tesseract.\n");
		exit(1);
	}

	 Open input image with leptonica library
	Pix* img_pix = pixRead("threshold.tiff");
	api->SetImage(img_pix);
	// Get OCR result
	outText = api->GetUTF8Text();
	printf("OCR output:\n%s", outText);

	// Destroy used object and release memory
	api->End();
	delete api;
	delete[] outText;
	pixDestroy(&img_pix);

	return 0;
}

3. Recognition effect

Input image
insert image description here
Currently, the recognition effect is not very good and needs to be adjusted
Reference: Lecture 71 of Python Visual Combat Project (Update).pdf Modify python code to c++

Guess you like

Origin blog.csdn.net/wyw0000/article/details/131200312