滑鼠操作事件,擷取攝像頭視訊中區域,圖片預處理,識別數字
- 通過回撥函式擷取攝像頭視訊感興趣區域(儀器數字區域),並進行預處理,然後識別,程式碼如下
- 效果圖如下
//滑鼠操作事件,擷取攝像頭視訊中區域,識別圖片 #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; }