首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >从图像中提取数字

从图像中提取数字
EN

Stack Overflow用户
提问于 2015-08-28 10:58:54
回答 1查看 124关注 0票数 2

在这里,我正在尝试获取相等大小的块,以便为OCR应用程序准备好数字

第一次尝试通过按固定步长移动的小代码,在某些位置由于数字之间的空格而跳得很高,主要问题是最后5位,有时是2个数字,然后是3个数字,有时是3个数字,然后是空格,然后是2个数字,如果5个数字很大,最后可能是5个数字

第二次尝试使用FindContour,当它找到对象时,我调整矩形的大小以适合它,但问题是它没有按从左到右或相反的顺序给我数字。

那么我该如何处理呢?

第一次尝试:

代码语言:javascript
代码运行次数:0
运行
复制
void DetectEqualRectangles(Mat image){
 resize(image,image,Size(810,52));
int k=0;
for(int i=0;i<14;i++){
    rectangle(image,Point(k,0),Point(45+k,52),Scalar(0,0,255),1,8,0);
    imshow("1",image);
    waitKey(0);
    if(i==0){k+=70;}
    else if(i==2){k+=71;}
    else if(i==4){k+=75;}
    else if(i==6){k+=78;}
    else if(i==8){k+=76;}
    else{k+=50;}
}}

第二次尝试:

代码语言:javascript
代码运行次数:0
运行
复制
void DetectUsingContours(Mat image){
resize(image,image,Size(810,52));
Mat gray;int BrightnessIndicator=0;
cvtColor(image,gray,CV_BGR2GRAY);

GaussianBlur(gray,gray,Size(5,5),3,0); // applying a gaussianBlur
BrightnessIndicator=EstimateBrighteness(image); // getting the approximate value for the brightness

cout<<BrightnessIndicator<<endl;
threshold(gray,gray,BrightnessIndicator-33,255,CV_THRESH_BINARY_INV); //thresholding
imshow("s",gray);

vector< vector<Point> > Contour;
findContours(gray,Contour,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE);     //finding outer contours
cout<<Contour.size();
for(int i=0;i<Contour.size();i++){

   Rect bounding = boundingRect(Contour[i]);  // draw a rectangle
   if(bounding.x>15 && bounding.x<image.cols-50){bounding.x-=15;bounding.width=50;}
   else if(bounding.x>image.cols-50){bounding.x=image.cols-40;bounding.width=40;}
   else{bounding.x=0;bounding.width=50;}


    bounding.y-=bounding.y;
    bounding.height=image.rows;
  //  rectangle(image,bounding,Scalar(0,255,0),1,8,0);

    Mat CroppedImage=image(bounding);
    stringstream ss;
    ss<<"C:\\Users\\cdc\\Desktop\\GSC\\ExtractingNumbers\\"<<i<<".jpg";
    imwrite(ss.str(),CroppedImage);
    imshow("5",image);
    imshow("23",CroppedImage);
    waitKey(0);
}}

这是原始图像:

EN

回答 1

Stack Overflow用户

发布于 2015-09-04 12:58:56

只需按std::sort对结果进行排序

代码语言:javascript
代码运行次数:0
运行
复制
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>

#include <algorithm>
#include <iostream>
#include <sstream>

using namespace cv;
using namespace std;

void DetectUsingContours(Mat &image)
{
    resize(image,image,Size(810,52));
    Mat gray;
    cvtColor(image,gray,CV_BGR2GRAY);

    GaussianBlur(gray,gray,Size(5,5),3,0); // applying a gaussianBlur
    threshold(gray, gray,0, 255,
              CV_THRESH_BINARY_INV | CV_THRESH_OTSU); //thresholding
    imshow("s",gray);

    vector< vector<Point> > Contour;
    findContours(gray,Contour,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE);     //finding outer contours
    cout<<Contour.size();
    std::vector<cv::Rect> rects;
    for(size_t i=0;i<Contour.size();i++){

        Rect bounding = boundingRect(Contour[i]);  // draw a rectangle
        if(bounding.x>15 && bounding.x<image.cols-50){bounding.x-=15;bounding.width=50;}
        else if(bounding.x>image.cols-50){bounding.x=image.cols-40;bounding.width=40;}
        else{bounding.x=0;bounding.width=50;}

        bounding.y-=bounding.y;
        bounding.height=image.rows;
        rects.emplace_back(bounding);
    }

    auto func = [](cv::Rect const &lhs, cv::Rect const &rhs)
    {
      return lhs.x < rhs.x;
    };
    std::sort(std::begin(rects), std::end(rects), func);
    for(size_t i = 0; i != rects.size(); ++i){
        Mat CroppedImage=image(rects[i]);
        stringstream ss;
        ss<<"C:/Users/cdc/Desktop/GSC/ExtractingNumbers/"<<i<<".jpg";
        imwrite(ss.str(),CroppedImage);
        imshow("5",image);
        imshow("23",CroppedImage);
        waitKey(0);
    }
}

int main()
{
    DetectUsingContours(cv::imread("tVVEl.jpg"));

    return 0;
}

我使用自适应阈值来做阈值处理,你不需要自己估计亮度。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32262667

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档