問題描述
OCR:沒有得到想要的結果 (OCR : Not getting desired result)
我有這張圖片。我正在嘗試對這張圖片中的字母進行 OCR。對於字母“9”和“R”,我沒有得到想要的結果。首先我裁剪了這些字母, & 並執行以下命令。
tesseract 9.png stdout ‑psm 8
.
它只是返回“。”
所有其他字母的 OCR 都可以正常工作,但對於這兩個字母則不行(不過,我認為它們的圖像質量沒那麼差)。任何建議/幫助表示讚賞。
參考解法
方法 1:
I've no experience with tesseract myself, but replicating the character and adding some background works on https://www.newocr.com/
which uses tesseract internally, according to google result.
So I used this as input:
which gives the correct result on that web‑app: 99999999
, while the single character doesn't work. Maybe you can verify this with your tesseract implementation and maybe it helps you to adjust your isolated extracted characters to work with tesseract. e.g. try to stitch multiple duplicates of your extracted contour next to each other to improve tesseract output ‑ since you know how often you stitched the contour next to each other you'll know that the output might be correct if it recognizes the same character that often times..
same works for
The border looks important, without enough border it will recognize P
. In general afaik you should try to replace background and foreground by pure black and pure white! Not sure what kind of preprocessing the web‑app uses...
this code can be used to repeat an image with C++ and OpenCV, but it won't add a border around. To do that you would work very similar but with some additional steps and you would have to assign some color to the border.
EDIT: I've updated the code to use a border of 4 pixel in each direction (you can adjust the variable) and with black background color.
This code is very easy and should be very similar for opencv in java, python, etc.
int main(int argc, char * argv[])
{
//cv::Mat input = cv::imread("../inputData/ocrR.png");
if(argc != 3)
{
std::cout << "usage: .exe filename #Repetitions" << std::endl;
return 0;
}
std::string filename = argv[1];
int nRepetitions = atoi(argv[2]);
cv::Mat inputImage = cv::imread(filename);
if(inputImage.empty())
{
std::cout << "image file " << filename << " could not be loaded" << std::endl;
return 0;
}
// you instead should try to extract the background color from the image (e.g. from the image border)
cv::Scalar backgroundColor(0,0,0);
// size of the border in each direction
int border = 4;
cv::Mat repeatedImage = cv::Mat(inputImage.rows + 2*border, nRepetitions*inputImage.cols + 2*border, inputImage.type() , backgroundColor);
cv::Rect roi = cv::Rect(border,border,inputImage.cols, inputImage.rows);
for(int i=0; i<nRepetitions; ++i)
{
// copy original image to subimage of repeated image
inputImage.copyTo(repeatedImage(roi));
// update roi position
roi.x += roi.width;
}
// now here you could send your repeated image to tesseract library and test whether nRepetitions times a letter was found.
cv::imwrite("repeatedImage.png", repeatedImage);
cv::imshow("repeated image" , repeatedImage);
cv::waitKey(0);
return 0;
}
giving this result:
方法 2:
I had a tiny bit more success than you... I did a "Connected Components Analysis" to extract the individual letters, then put a border around each extracted letter and appended them all together into a single horizontal line which gave me this:
And if I then run tesseract
I get:
VQQTRF
(by Bhushan、Micka、Mark Setchell)