openCV IOS 工程 识别数字

http://opencv.org 官网下载ios framework库



https://sourceforge.net/projects/libjpeg-turbo/files/1.4.0/

下载libjpeg-turbo-1.4.0.dmg

 在终端运行:lipo -info /opt/libjpeg-turbo/lib/libjpeg.a 将会出现以下内容:
 Architectures in the fat file: /opt/libjpeg-turbo/lib/libjpeg.a are: i386 x86_64 armv6 armv7 armv7s arm64
 然后将/opt/libjpeg-turbo/lib/libjpeg.a 加到你的工程里面


正式开始:

一、首先对图片进行预处理

对图片进行灰度化二值化

 //image源文件
    // 1.将iOS的UIImage转成c++图片(数据:矩阵)
    cv::Mat mat_image_gray;
    UIImageToMat(image, mat_image_gray);
    
    // 2. 将c++彩色图片转成灰度图片
    // 参数一:数据源
    // 参数二:目标数据
    // 参数三:转换类型
    cv::Mat mat_image_dst;
    cvtColor(mat_image_gray, mat_image_dst, cv::COLOR_BGRA2GRAY);
    
    // 3.灰度 -> 可显示的图片
    cvtColor(mat_image_dst, mat_image_gray, cv::COLOR_GRAY2BGR);
    
    cv::Mat dst;
    double thresh = 100;
    int maxVal = 255;
    cv::threshold(mat_image_dst, dst, thresh, maxVal, cv::THRESH_BINARY);
    
    // 4. 将c++处理之后的图片转成iOS能识别的UIImage
    return MatToUIImage(dst);


二:勾勒轮廓:


 cv::Mat edges;
        cv::Canny(gray, edges, 0, 50);
        // Fill image with white color
        cvImage.setTo(cv::Scalar::all(255));
        // Change color on edges
        cvImage.setTo(cv::Scalar(0, 128, 255, 255), edges);

三:腐蚀:

erode(dst,dst,cv::Mat(27,27,CV_8U),cv::Point(-1,-1),2);



四:轮廓检测

 cv::Mat c = dst.clone();
    vector<vector<cv::Point>> contours;
    findContours(c, contours, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE,cvPoint(0, 0));
    //除去太长或者太短的轮廓
    int cmin = 35;
    int cmax = 800;
    
    vector<vector<cv::Point> >::iterator itc = contours.begin();
    
    while(itc != contours.end())
    {
        cv::Rect rect = cv::boundingRect(*itc);
        
        if(itc->size() < cmin || itc->size() > cmax || rect.width < 40 || rect.height < 40){
            //std::cout << "EraseSize: " << itc->size() << std::endl;
            itc = contours.erase(itc);
        }
        else{
            ++itc;};
        
    }


五:取出区域

 cv::vector<cv::Rect> rects;
    cv::Rect rr = cv::Rect(0,0,0,0);
    std::vector<std::vector<cv::Point>>::const_iterator itContours = contours.begin();
    for ( ; itContours!=contours.end(); ++itContours){
        cv::Rect rect = boundingRect(*itContours);
        rects.push_back(rect);
        std::cout << "Size: " << rect << std::endl;
        if (rect.width > rr.width && rect.width > rect.height * 5) {
            rr = rect;
        }
    }


识别:

  

  
    G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:@"eng"];
    
    // Optionaly: You could specify engine to recognize with.
    // G8OCREngineModeTesseractOnly by default. It provides more features and faster
    // than Cube engine. See G8Constants.h for more information.
    //tesseract.engineMode = G8OCREngineModeTesseractOnly;
    
    // Set up the delegate to receive Tesseract's callbacks.
    // self should respond to TesseractDelegate and implement a
    // "- (BOOL)shouldCancelImageRecognitionForTesseract:(G8Tesseract *)tesseract"
    // method to receive a callback to decide whether or not to interrupt
    // Tesseract before it finishes a recognition.
    tesseract.delegate = self;
    
    // Optional: Limit the character set Tesseract should try to recognize from
    tesseract.charWhitelist = @"0123456789";
    
    // This is wrapper for common Tesseract variable kG8ParamTesseditCharWhitelist:
    // [tesseract setVariableValue:@"0123456789" forKey:kG8ParamTesseditCharBlacklist];
    // See G8TesseractParameters.h for a complete list of Tesseract variables
    
    // Optional: Limit the character set Tesseract should not try to recognize from
    //tesseract.charBlacklist = @"OoZzBbSs";
    
    // Specify the image Tesseract should recognize on
    tesseract.image = [[UIImage imageNamed:@"4.jpg"] g8_blackAndWhite];
    
    // Optional: Limit the area of the image Tesseract should recognize on to a rectangle
    tesseract.rect = CGRectMake(20, 20, 100, 100);
    
    // Optional: Limit recognition time with a few seconds
    tesseract.maximumRecognitionTime = 2.0;
    
    // Start the recognition
    [tesseract recognize];
    
    // Retrieve the recognized text
    NSLog(@"text:%@", [tesseract recognizedText]);


未完待续


猜你喜欢

转载自blog.csdn.net/ssyyjj88/article/details/75130979