关闭

OpenCV-简单数字识别OCR

标签: opencv
79人阅读 评论(0) 收藏 举报
分类:

参考:
https://stackoverflow.com/questions/9413216/simple-digit-recognition-ocr-in-opencv-python


1、数据

这里写图片描述
digit.png

这里写图片描述
dig.png

2、C++

2.1 创建样本和标签数据的代码

#include "opencv2/opencv.hpp"

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{

    //Process image to extract contour
    Mat thr, gray, con;
    Mat src = imread("digit.png", 1);
    cvtColor(src, gray, CV_BGR2GRAY);
    threshold(gray, thr, 200, 255, THRESH_BINARY_INV); //Threshold to find contour
    thr.copyTo(con);

    // Create sample and label data
    vector< vector <Point> > contours; // Vector for storing contour
    vector< Vec4i > hierarchy;
    Mat sample;
    Mat response_array;
    findContours(con, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE); //Find contour

    for (int i = 0; i< contours.size(); i = hierarchy[i][0]) // iterate through first hierarchy level contours
    {
        Rect r = boundingRect(contours[i]); //Find bounding rect for each contour
        rectangle(src, Point(r.x, r.y), Point(r.x + r.width, r.y + r.height), Scalar(0, 0, 255), 2, 8, 0);
        Mat ROI = thr(r); //Crop the image
        Mat tmp1, tmp2;
        resize(ROI, tmp1, Size(10, 10), 0, 0, INTER_LINEAR); //resize to 10X10
        tmp1.convertTo(tmp2, CV_32FC1); //convert to float
        sample.push_back(tmp2.reshape(1, 1)); // Store  sample data
        imshow("src", src);
        int c = waitKey(0); // Read corresponding label for contour from keyoard
        c -= 0x30;     // Convert ascii to intiger value
        response_array.push_back(c); // Store label to a mat
        rectangle(src, Point(r.x, r.y), Point(r.x + r.width, r.y + r.height), Scalar(0, 255, 0), 2, 8, 0);
    }

    // Store the data to file
    Mat response, tmp;
    tmp = response_array.reshape(1, 1); //make continuous
    tmp.convertTo(response, CV_32FC1); // Convert  to float

    FileStorage Data("TrainingData.yml", FileStorage::WRITE); // Store the sample data in a file
    Data << "data" << sample;
    Data.release();

    FileStorage Label("LabelData.yml", FileStorage::WRITE); // Store the label data in a file
    Label << "label" << response;
    Label.release();
    cout << "Training and Label data created successfully....!! " << endl;

    imshow("src", src);
    waitKey();

    return 0;
}

2.2 训练和测试代码

#include "opencv2/opencv.hpp"
#include<opencv2\ml\ml.hpp>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{

    Mat thr, gray, con;
    Mat src = imread("dig.png", 1);
    cvtColor(src, gray, CV_BGR2GRAY);
    threshold(gray, thr, 200, 255, THRESH_BINARY_INV); // Threshold to create input
    thr.copyTo(con);


    // Read stored sample and label for training
    Mat sample;
    Mat response, tmp;
    FileStorage Data("TrainingData.yml", FileStorage::READ); // Read traing data to a Mat
    Data["data"] >> sample;
    Data.release();

    FileStorage Label("LabelData.yml", FileStorage::READ); // Read label data to a Mat
    Label["label"] >> response;
    Label.release();

    Ptr<ml::KNearest>  knn(ml::KNearest::create());
    //ml::KNearest knn();
    knn->train(sample, ml::ROW_SAMPLE,response); // Train with sample and responses
    cout << "Training compleated.....!!" << endl;

    vector< vector <Point> > contours; // Vector for storing contour
    vector< Vec4i > hierarchy;

    //Create input sample by contour finding and cropping
    findContours(con, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
    Mat dst(src.rows, src.cols, CV_8UC3, Scalar::all(0));

    for (int i = 0; i< contours.size(); i = hierarchy[i][0]) // iterate through each contour for first hierarchy level .
    {
        Rect r = boundingRect(contours[i]);
        Mat ROI = thr(r);
        Mat tmp1, tmp2;
        resize(ROI, tmp1, Size(10, 10), 0, 0, INTER_LINEAR);
        tmp1.convertTo(tmp2, CV_32FC1);
        Mat response;
        float p = knn->findNearest(tmp2.reshape(1, 1), 1, response);
        char name[4];
        sprintf(name, "%d", (int)p);
        putText(dst, name, Point(r.x, r.y + r.height), 0, 1, Scalar(0, 255, 0), 2, 8);
    }

    imshow("src", src);
    imshow("dst", dst);
    imwrite("dest.jpg", dst);
    waitKey();

    return 0;
}

3、Python

import sys

import numpy as np
import cv2

im = cv2.imread('digit.png')
im3 = im.copy()

gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),0)
thresh = cv2.adaptiveThreshold(blur,255,1,1,11,2)

#################      Now finding Contours         ###################

image,contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

samples =  np.empty((0,100))
responses = []
keys = [i for i in range(48,58)]

for cnt in contours:
    if cv2.contourArea(cnt)>50:
        [x,y,w,h] = cv2.boundingRect(cnt)

        if  h>28:
            cv2.rectangle(im,(x,y),(x+w,y+h),(0,0,255),2)
            roi = thresh[y:y+h,x:x+w]
            roismall = cv2.resize(roi,(10,10))
            cv2.imshow('norm',im)
            key = cv2.waitKey(0)

            if key == 27:  # (escape to quit)
                sys.exit()
            elif key in keys:
                responses.append(int(chr(key)))
                sample = roismall.reshape((1,100))
                samples = np.append(samples,sample,0)

responses = np.array(responses,np.float32)
responses = responses.reshape((responses.size,1))
print ("training complete")

np.savetxt('generalsamples.data',samples)
np.savetxt('generalresponses.data',responses)
import cv2
import numpy as np

#######   training part    ############### 
samples = np.loadtxt('generalsamples.data',np.float32)
responses = np.loadtxt('generalresponses.data',np.float32)
responses = responses.reshape((responses.size,1))

model = cv2.KNearest()
model.train(samples,responses)

############################# testing part  #########################

im = cv2.imread('dig.png')
out = np.zeros(im.shape,np.uint8)
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(gray,255,1,1,11,2)

image,contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

for cnt in contours:
    if cv2.contourArea(cnt)>50:
        [x,y,w,h] = cv2.boundingRect(cnt)
        if  h>28:
            cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),2)
            roi = thresh[y:y+h,x:x+w]
            roismall = cv2.resize(roi,(10,10))
            roismall = roismall.reshape((1,100))
            roismall = np.float32(roismall)
            retval, results, neigh_resp, dists = model.find_nearest(roismall, k = 1)
            string = str(int((results[0][0])))
            cv2.putText(out,string,(x,y+h),0,1,(0,255,0))

cv2.imshow('im',im)
cv2.imshow('out',out)
cv2.waitKey(0)
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:16892次
    • 积分:2053
    • 等级:
    • 排名:千里之外
    • 原创:57篇
    • 转载:138篇
    • 译文:115篇
    • 评论:0条
    文章分类