鼠标操作事件,截取摄像头视频中区域,图片预处理,识别数字

  • 通过回调函数截取摄像头视频感兴趣区域(仪器数字区域),并进行预处理,然后识别,代码如下
  • 效果图如下

//鼠标操作事件,截取摄像头视频中区域,识别图片
#include <opencv2/core/core.hpp>  
#include <opencv2/highgui/highgui.hpp>  
#include <opencv2\imgproc\imgproc.hpp>
#include <stdio.h>
#include <iostream>
#include "baseapi.h"   
using namespace cv;
using namespace std;
  
cv::Mat frame,frame1,dst,img,tmp,imageEnhance;  
int ImgNum = 40;
char ImagesName[100];

#pragma  comment(lib,"libtesseract302d.lib") 

void on_mouse(int event,int x,int y,int flags,void *ustc);
Mat disposeimg(Mat imgsrc);

void main()  
{  
    VideoCapture cap(0);
    if(!cap.isOpened())
    {
        printf("error");
        return;
    }
    cap.set(CV_CAP_PROP_FRAME_WIDTH,1200);
    cap.set(CV_CAP_PROP_FRAME_HEIGHT,900);
    //cap.set(CV_CAP_PROP_BRIGHTNESS,10);
    //printf("brightness = %.2f\n", cap.get(CV_CAP_PROP_BRIGHTNESS));
    while(1)
    {
        
        cap>>frame;
        flip(frame,frame1,-1);
        frame1.copyTo(img);
        frame1.copyTo(tmp);
        waitKey(50);
        namedWindow("img");//定义一个img窗口  
        setMouseCallback("img",on_mouse,0);//调用回调函数  
        imshow("img",img);
        if(27==waitKey(30))//按ESC键退出
            break;
    }
    cap.release();
}  
void on_mouse(int event,int x,int y,int flags,void *ustc)//event鼠标事件代号,x,y鼠标坐标,flags拖拽和键盘操作的代号  
{  
    static Point pre_pt = (-1,-1);//初始坐标  
    static Point cur_pt = (-1,-1);//实时坐标  
    char temp[16];  
    if (event == CV_EVENT_LBUTTONDOWN)//左键按下,读取初始坐标,并在图像上该点处划圆  
    {  
        frame1.copyTo(img);//将原始图片复制到img中  
        sprintf(temp,"(%d,%d)",x,y);  
        //cout<<x<<endl<<y<<endl;
        pre_pt = Point(x,y);  
       // putText(img,temp,pre_pt,FONT_HERSHEY_SIMPLEX,0.5,Scalar(0,0,0,255),1,8);//在窗口上显示坐标  
        circle(img,pre_pt,2,Scalar(255,0,0,0),CV_FILLED,CV_AA,0);//划圆  
        //imshow("img",img);  
    }  
    /*
    else if (event == CV_EVENT_MOUSEMOVE && !(flags & CV_EVENT_FLAG_LBUTTON))//左键没有按下的情况下鼠标移动的处理函数  
    {  
        img.copyTo(tmp);//将img复制到临时图像tmp上,用于显示实时坐标  
        sprintf(temp,"(%d,%d)",x,y);  
        cur_pt = Point(x,y);  
        putText(tmp,temp,cur_pt,FONT_HERSHEY_SIMPLEX,0.5,Scalar(0,0,0,255));//只是实时显示鼠标移动的坐标  
        imshow("img",tmp);  
    }  
    */
    else if (event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))//左键按下时,鼠标移动,则在图像上划矩形  
    {  
        img.copyTo(tmp);  
        sprintf(temp,"(%d,%d)",x,y);  
        cur_pt = Point(x,y);  
        putText(tmp,temp,cur_pt,FONT_HERSHEY_SIMPLEX,0.5,Scalar(0,0,0,255));  
        rectangle(tmp,pre_pt,cur_pt,Scalar(0,255,0,0),1,8,0);//在临时图像上实时显示鼠标拖动时形成的矩形  
        imshow("img",tmp);  
    }  
    else if (event == CV_EVENT_LBUTTONUP)//左键松开,将在图像上划矩形  
    {  
        frame1.copyTo(img);  
        sprintf(temp,"(%d,%d)",x,y);  
        cur_pt = Point(x,y);  
        //putText(img,temp,cur_pt,FONT_HERSHEY_SIMPLEX,0.5,Scalar(0,0,0,255));  
        circle(img,pre_pt,2,Scalar(255,0,0,0),CV_FILLED,CV_AA,0);  
        rectangle(img,pre_pt,cur_pt,Scalar(0,255,0,0),1,8,0);//根据初始点和结束点,将矩形画到img上  
        imshow("img",img);  
        img.copyTo(tmp);  
        //截取矩形包围的图像,并保存到dst中  
        int width = abs(pre_pt.x - cur_pt.x);  
        int height = abs(pre_pt.y - cur_pt.y);  
        if (width == 0 || height == 0)  
        {  
            printf("width == 0 || height == 0"); 
            return;  
        }  
        ImgNum=ImgNum+1;
        dst = frame1(Rect(min(cur_pt.x,pre_pt.x),min(cur_pt.y,pre_pt.y),width,height));
        sprintf_s(ImagesName, "%s%.2d%s", "C:/Users/hzaihua/Desktop/mid1/", ImgNum, ".jpg");
        //imwrite(ImagesName,dst);
       // namedWindow("dst");  
        //imshow("dst",dst);
        //imwrite("30.jpg",dst);

        Mat dst1=disposeimg(dst);//5636-0和5636-1预处理
        //imwrite(ImagesName,dst1);
        //imshow("dst1",dst1);
        imwrite(ImagesName,dst1);

        //imageEnhance=image_enhance(dst);
        //imwrite(ImagesName,dst);
        
        //数字识别
        tesseract::TessBaseAPI  api;
        //api.Init(NULL, "eng", tesseract::OEM_DEFAULT);//初始化,设置语言包 
        api.Init(NULL, "num", tesseract::OEM_DEFAULT);//初始化,设置语言包 
        
        api.SetVariable("tessedit_char_whitelist", ".0123456789dB=LFSIp");//白名单过滤
        STRING text_out;
        
        const char* p = ImagesName;
        api.ProcessPages(p, NULL, 0, &text_out);
        
        int conf = api.MeanTextConf();
        //cout<<ImagesName<<endl;  
        //cout<<conf<<" "<<text_out.string()<<endl;  
        cout<<text_out.string()<<endl;  
       // waitKey(0);   
    }  
    /*
    else if(!(flags))//鼠标没有按下
    {
        frame1.copyTo(img);
        cur_pt = Point(x,y);  
        putText(tmp,temp,cur_pt,FONT_HERSHEY_SIMPLEX,0.5,Scalar(0,0,0,255));//只是实时显示鼠标移动的坐标  
        pre_pt.x=985;pre_pt.y=566;
        cur_pt.x=1147;cur_pt.y=628;
        int width = abs(pre_pt.x - cur_pt.x);
        int height = abs(pre_pt.y - cur_pt.y);
        ImgNum=ImgNum+1;
        dst = frame1(Rect(min(cur_pt.x,pre_pt.x),min(cur_pt.y,pre_pt.y),width,height));
        sprintf_s(ImagesName, "%s%.2d%s", "C:/Users/hzaihua/Desktop/mid1/", ImgNum, ".jpg");
        Mat dst1=disposeimg(dst);
        imshow("dst1",dst1);
        imwrite(ImagesName,dst1);

        //imageEnhance=image_enhance(dst);
        //imwrite(ImagesName,dst);
        
        //数字识别
        tesseract::TessBaseAPI  api;
        //api.Init(NULL, "eng", tesseract::OEM_DEFAULT);//初始化,设置语言包 
        api.Init(NULL, "num", tesseract::OEM_DEFAULT);//初始化,设置语言包 
        
        api.SetVariable("tessedit_char_whitelist", ".0123456789");//白名单过滤
        STRING text_out;

        const char* p = ImagesName;
        api.ProcessPages(p, NULL, 0, &text_out);
        //cout<<ImagesName<<endl;  
        cout<<text_out.string()<<endl;  
    }
    */
}  
//图像预处理
Mat disposeimg(Mat imgsrc)
{

    Mat imageLog(imgsrc.size(), CV_32FC3);    
    for (int i = 0; i < imgsrc.rows; i++)    
    {        
        for (int j = 0; j <imgsrc.cols; j++)        
        {            
            imageLog.at<Vec3f>(i, j)[0] = log(float(1+ imgsrc.at<Vec3b>(i, j)[0]));        
            imageLog.at<Vec3f>(i, j)[1] = log(float(1+ imgsrc.at<Vec3b>(i, j)[1]));            
            imageLog.at<Vec3f>(i, j)[2] = log(float(1 + imgsrc.at<Vec3b>(i, j)[2]));        
        }    
    }    

    // 用到的图像增强算法可参考 https://blog.csdn.net/dcrmg/article/details/53677739
    //归一化到0~255        
    normalize(imageLog, imageLog, 0, 255, CV_MINMAX);     
    //转换成8bit图像显示        
    convertScaleAbs(imageLog, imageLog);    
    
    imshow("i",imageLog);
    
    Mat gray;
    cvtColor(imageLog,gray,COLOR_RGB2GRAY);
    
    Mat otsu2_image,otsu1_image,otsu_image,gray1;

    //threshold(gray, otsu_image, 105, 255, CV_THRESH_BINARY);
    cv::adaptiveThreshold(gray, otsu_image,255,ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY,91,0);
    //腐蚀
    Mat erode_image;
    Mat element2 = getStructuringElement(MORPH_CROSS, Size(3, 3));
    erode(otsu_image, erode_image, element2);

    Mat dst;
    GaussianBlur(erode_image, dst, Size(3, 3),0);

    return dst;
    
}
 

猜你喜欢

转载自blog.csdn.net/kangshuaibing/article/details/82898716