事先說明,本人純粹是小白。寫這篇博客也是相信小白惜小白,順帶鍛煉一下寫作能力(畢設需求),因此,有問題還是靠度娘吧! 作為剛接觸程序的新人,看網上大佬們分享的程序,在對他們頂膜礼拜的同時,卻不經懷有抱怨的心情。因為大部分大佬們,不知是想鍛煉我們,還是單純的嫌麻煩,亦或者對于他們來說,這根本就不是什么難懂的地方,我只想說,你們為什么就不寫注釋呢(血淚聚下)。這篇博客是在網上查找的程序基礎上,標注自己的理解,或許有不當的地方,請大家指正,好了,我就不啰嗦了(應該也沒人看)以下是干貨。 我是利用bp神經網絡進行對圖片分類判別,神經網絡的原理在網上有現成的,不想去搜的朋友可以看這一篇http://blog.csdn.net/garfielder007/article/details/51004919,原理稍稍理解后,我們就可以進行程序的實現了。 首先在VS中建立工程,名字隨意,我這里起名為bpnet;在建好的工程文件夾中放入訓練的文件,我這里分成兩類,一類是清晰圖片,另一類是模糊圖片,都放在charSamples,如圖,0文件夾為清晰,1文件夾為模糊
如圖;
添加源程序:
#include%20<windows.h>#include%20<iostream>#include%20<opencv2/opencv.hpp>using%20namespace%20cv;using%20namespace%20std;char*%20WcharToChar(const%20wchar_t*%20wp)%20%20{%20%20%20%20%20%20char%20*m_char;%20%20%20%20int%20len=%20WideCharToMultiByte(CP_ACP,0,wp,wcslen(wp),NULL,0,NULL,NULL);%20%20%20%20%20%20m_char=new%20char[len+1];%20%20%20%20%20%20WideCharToMultiByte(CP_ACP,0,wp,wcslen(wp),m_char,len,NULL,NULL);%20%20%20%20%20%20m_char[len]='/0';%20%20%20%20%20%20return%20m_char;%20%20}%20%20wchar_t*%20CharToWchar(const%20char*%20c)%20%20{%20%20%20%20%20%20%20wchar_t%20*m_wchar;%20%20%20%20int%20len%20=%20MultiByteToWideChar(CP_ACP,0,c,strlen(c),NULL,0);%20%20%20%20%20%20m_wchar=new%20wchar_t[len+1];%20%20%20%20%20%20MultiByteToWideChar(CP_ACP,0,c,strlen(c),m_wchar,len);%20%20%20%20%20%20m_wchar[len]='/0';%20%20%20%20%20%20return%20m_wchar;%20%20}%20%20wchar_t*%20StringToWchar(const%20string&%20s)%20%20{%20%20%20%20%20%20const%20char*%20p=s.c_str();%20%20%20%20%20%20return%20CharToWchar(p);%20%20}%20%20int%20main(){ const%20string%20fileform%20=%20"*.jpg";//文件格式 const string perfileReadPath = "charSamples"; //文件前置路徑 const int sample_mun_perclass = 40;//訓練字符每類數量 const int class_mun = 2;//訓練字符類數 const int image_cols = 8;//圖片分為列,可自行調整,這里只是隨意寫的 const int image_rows = 16;//圖片分為行 string fileReadName,fileReadPath; char temp[256]; float trainingData[class_mun*sample_mun_perclass] [image_cols*image_rows] = {{0}};//每一行一個訓練樣本 float labels[class_mun*sample_mun_perclass][class_mun] = {{0}};//訓練樣本標簽 for(int i=0;i<=class_mun-1;++i)//不同類 { //讀取每個類文件夾下所有圖像 int j = 0;//每一類下讀取圖像計數個數 sPRintf(temp,"%d",i);//按順序讀圖 fileReadPath = perfileReadPath + "/" + temp + "/" + fileform;//文件讀取路徑 cout<<"文件夾"<<i<<endl; HANDLE hFile; LPCTSTR lpFileName = StringToWchar(fileReadPath);//指定搜索目錄和文件類型 WIN32_FIND_DATA pNextInfo;//搜索得到的文件信息將儲存在pNextInfo中; hFile = FindFirstFile(lpFileName,&pNextInfo); if(hFile == INVALID_HANDLE_VALUE)//搜索失敗 { exit(-1); } //循環讀取 do { if(pNextInfo.cFileName[0] == '.')//過濾.和.. continue; j++;//讀取一張圖 printf("%s/n",WcharToChar(pNextInfo.cFileName)); //對讀入的圖片進行處理 Mat srcImage = imread( perfileReadPath + "/" + temp + "/" + WcharToChar(pNextInfo.cFileName),CV_LOAD_IMAGE_GRAYSCALE);//讀入圖像 Mat resizeImage; Mat trainImage; resize(srcImage,resizeImage,Size(image_cols,image_rows),(0,0),(0,0),CV_INTER_AREA);//使用象素關系重采樣。當圖像縮小時候,該方法可以避免波紋出現 threshold(resizeImage,trainImage,0,255,CV_THRESH_BINARY|CV_THRESH_OTSU);//對灰度圖像進行閾值操作得到二值圖像 for(int k = 0; k<image_rows*image_cols; ++k)//每個圖片 { trainingData[i*sample_mun_perclass+(j-1)][k] = (float)trainImage.data[k];//將二值化數據拷貝到trainingData中 } } while (FindNextFile(hFile,&pNextInfo) && j<sample_mun_perclass);//如果設置讀入的圖片數量,則以設置的為準,如果圖片不夠,則讀取文件夾下所有圖片 } // 設置訓練數據Mat輸入 Mat trainingDataMat(class_mun*sample_mun_perclass, image_rows*image_cols, CV_32FC1, trainingData);// 設置訓練數據Mat //cout<<trainingDataMat<<endl; cout<<"trainingDataMat——OK!"<<endl; // 設置標簽數據Mat輸出 for(int i=0;i<=class_mun-1;++i) { for(int j=0;j<=sample_mun_perclass-1;++j) { for(int k = 0;k<class_mun;++k) { if(k==i) labels[i*sample_mun_perclass + j][k] = 1; else labels[i*sample_mun_perclass + j][k] = 0; } } } Mat labelsMat(class_mun*sample_mun_perclass, class_mun, CV_32FC1,labels);//設置標簽數據Mat cout<<"labelsMat:"<<endl; cout<<labelsMat<<endl; cout<<"labelsMat——OK!"<<endl; //訓練代碼 cout<<"training start...."<<endl; CvANN_MLP bp;//bp神經網絡 //設置bp神經網絡的參數 CvANN_MLP_TrainParams params;//類,參數 params.train_method=CvANN_MLP_TrainParams::BACKPROP;//訓練方法 params.bp_dw_scale=0.001;//權值更新率 params.bp_moment_scale=0.1;//權值更新沖量 params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,10000,0.0001); //設置結束條件term_crit:終止條件, //它包括了兩項,迭代次數(CV_TERMCRIT_ITER)和誤差最小值(CV_TERMCRIT_EPS),一旦有一個達到條件就終止訓練。 //設置bp神經網絡 Mat layerSizes=(Mat_<int>(1,5) << image_rows*image_cols,128,128,128,class_mun);//1個輸入,1個輸出,3個隱藏層,隱藏層數也根據內容自己調整 bp.create(layerSizes,CvANN_MLP::SIGMOID_SYM,1.0,1.0);//CvANN_MLP::SIGMOID_SYM節點使用的函數 cout<<"training...."<<endl; bp.train(trainingDataMat, labelsMat, Mat(),Mat(), params);//trainingDataMat:輸入矩陣,存儲了所有訓練樣本的特征 //labelsMat:輸出矩陣,每個樣本所屬的種類每一行表示一個樣本的預期輸出結果, bp.save("bpnet.xml"); //保存訓練 cout<<"training finish...bpnet.xml saved "<<endl; /*CvANN_MLP bp;//bp神經網絡 bp.load("bpnet.xml")*/;//這是網絡訓練好后直接調用 //測試神經網絡 cout<<"測試:"<<endl; Mat test_image = imread("test.jpg",CV_LOAD_IMAGE_GRAYSCALE);//讀入測試圖 Mat test_temp; resize(test_image,test_temp,Size(image_cols,image_rows),(0,0),(0,0),CV_INTER_AREA);//使用象素關系重采樣。當圖像縮小時候,該方法可以避免波紋出現 threshold(test_temp,test_temp,0,255,CV_THRESH_BINARY|CV_THRESH_OTSU);//二值化 Mat_<float>sampleMat(1,image_rows*image_cols); for(int i = 0; i<image_rows*image_cols; ++i) { sampleMat.at<float>(0,i) = (float)test_temp.at<uchar>(i/8,i%8); //將test數據(unchar)copy到sampleMat(float)中圖像特征 } Mat responseMat; bp.predict(sampleMat,responseMat); //過調用predict函數,我們得到一個輸出向量,它是一個1*nClass的行向量, //其中每一列說明它與該類的相似程度(0-1之間),也可以說是置信度 Point maxLoc; double maxVal = 0; minMaxLoc(responseMat,NULL,&maxVal,NULL,&maxLoc);//最小最大值 string judge = ""; if(maxLoc.x == 0) judge = "清晰"; if(maxLoc.x == 1) judge = "模糊"; cout<<"識別結果:"<<judge<<endl; imshow("test_image",test_image); waitKey(0); return 0;}測試圖:
運行結果:
以上,我的第一篇博客就寫完了,建議配合大神們給出的原理理解會更好!
新聞熱點
疑難解答