简体   繁体   English

如何识别模拟计数器的数字?

[英]How to recognize digits from the analog counter?

I'm trying to read the following kWh numbers from the counter. 我正试图从柜台读取以下千瓦时数字。 The problem is the tesseract OCR doesn't recognize the analog digits. 问题是tesseract OCR无法识别模拟数字。

计数器

The question is: will it be a better idea to make the photos of all of the digits (from 0 to 9) at different positions (I mean when digit is in the center, when it is a little at the top and the number 2 is appearing etc.) and to try image recognition instead of text recognition? 问题是:在不同的位置制作所有数字(从0到9)的照片会更好吗(我的意思是当数字位于中心时,当它位于顶部且数字2时正在出现等)并尝试图像识别而不是文本识别?

As far as I understood the difference is, that the image recognition compares the photos, while the text recognition... well I don't know... 据我所知,区别在于,图像识别比较照片,而文字识别......我不知道......

Any advice? 有什么建议?

Since the counter is not digital, but analog, we have problems at the transitions. 由于计数器不是数字的,而是模拟的,我们在转换时遇到问题。 The text/number recognition libraries can not recognize smth like that. 文本/数字识别库无法像这样识别smth。 The solution, that I've found is: Machine Learning . 我发现的解决方案是: 机器学习

Firstly I've made user to make the picture, where the numbers take 70-80% of the image (in order to remove the unneeded details). 首先,我让用户制作图片,其中数字占图像的70-80%(为了删除不需要的细节)。

Then I'm looking for parallel lines (if there are any) and cut the picture, that is between them (if the distance is big enough). 然后我正在寻找平行线(如果有的话)并剪切图片,即它们之间(如果距离足够大)。

After that I'm filtering the picture (playing with contrast, brightness, set it grayscale) and then use the filter, that makes the image to contain only two colours ( #000000 (black) and #ffffff (white)). 之后,我正在过滤图片(使用对比度,亮度,设置灰度),然后使用滤镜,使图像只包含两种颜色( #000000 (黑色)和#ffffff (白色))。 In order to find the contours easier. 为了更容易找到轮廓。

Then I find the contours by using Canny algorithm and filter them, by removing the unneeded details. 然后我通过使用Canny算法找到轮廓并通过删除不需要的细节来过滤它们。

After that I use K-Nearest-Neighbour algorithm in order to recognize the digits. 之后,我使用K-Nearest-Neighbour算法来识别数字。

But before I can recognize anything, I need to teach the algorithm, how the digits look like and what are they. 但在我能够识别任何东西之前,我需要教授算法,数字的外观以及它们是什么。

I hope it was useful! 我希望它有用!

Maybe you are not configuring tesseract right. 也许你没有正确配置tesseract。 I made a code using it that solves your problem: 我使用它编写了一个代码来解决你的问题:

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <tesseract/baseapi.h>
#include <iostream>

using namespace cv;

int main(int argc, char** argv)
{
    cv::Mat input = cv::imread("img.jpg");

    //rectangle containing just the kWh numbers
    Rect roi(358,327,532,89);

    //convert to gray scale
    Mat input_gray;
    cvtColor(input(roi),input_gray,CV_BGR2GRAY);

    //threshold image
    Mat binary_img = input_gray>200;

    //make a copy to use on findcontours
    Mat copy_binary_img = binary_img.clone();

    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    //identify each blob in order to eliminate the small ones 
    findContours(copy_binary_img, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0,0));

    //filter blobs by their sizes
    for (vector<vector<Point> >::iterator it = contours.begin(); it!=contours.end(); )
    {
        if (it->size()>20)
            it=contours.erase(it);
        else
            ++it;
    }

    //Erase blobs which have countour size smaller than 20
    for( int i = 0; i< contours.size(); i++ )
    {
        drawContours( binary_img, contours, i, 0, -1, 8, hierarchy, 0, Point() );
    }

    //initialize tesseract OCR
    tesseract::TessBaseAPI tess;
    tess.Init(NULL, "eng", tesseract::OEM_DEFAULT);

    tess.SetVariable("tessedit_char_whitelist", "0123456789-.");

    tess.SetPageSegMode(tesseract::PSM_SINGLE_BLOCK);

    //set input 
    tess.SetImage((uchar*)binary_img.data
            , binary_img.cols
            , binary_img.rows
            , 1
            , binary_img.cols);

    // Get the text
    char* out = tess.GetUTF8Text();
    std::cout << out << std::endl;
    waitKey();
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM