2015-08-28 32 views
2

enter image description here獲取號碼的開出的圖像

enter image description here

enter image description here

enter image description here

在這裏,我試圖擺脫相等大小的塊,以獲得數準備的OCR應用

第一次嘗試通過固定步驟移動的小代碼有些位置由於數字之間的空間而跳得很高,主要問題在於最後5位數字,有時候它們是2位數字,空間然後是3位數字,有時候是3位數字,空間是2位數字,最後是5位數字。這5個數字很大

第二次嘗試我使用了FindContour,當它找到對象時,我調整了矩形的大小以適應它,但問題是它沒有給我從左到右的順序或相反的數字。

那麼我該如何處理?

第一次嘗試:

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;} 
}} 

第二次嘗試:

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); 
}} 

這裏是原始圖像: enter image description here

enter image description here

enter image description here

enter image description here

回答

1

僅僅通過STD排序結果::排序

#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; 
} 

我用自適應閾值做閾值,你不需要自己來估計亮度。