计算image 积分图

 1 // testopencv.cpp : 定义控制台应用程序的入口点。

 2 //

 3 

 4 #include "stdafx.h"

 5 #include <opencv2\opencv.hpp>

 6 #include <fstream>

 7 #include <vector>

 8 #include <string>

 9 using namespace std;

10 using namespace cv;

11 void getFileName(string & filename,vector<string> &vstr)

12 {

13     cout<<"begin read file name from "<< filename<<endl;

14     string name;

15     ifstream in(filename);

16     while(!in.eof())

17     {

18         in>>name;

19         vstr.push_back(name);

20     }

21     cout<<"end read filename , the total image number is "<< vstr.size()<<endl;

22 }

23 void writeIntegral(vector<string> &vstr,string& filename)

24 {

25     cout<<"begin calculate the image integral"<<endl;

26     ofstream out(filename,ofstream::binary);

27     int num = 0;

28     vector<string>::iterator begin = vstr.begin();

29     for( ;begin != vstr.end(); begin++)

30     {

31         //第二个参数为0读进来的image都是灰度的

32         Mat img = imread(*begin,0);

33         if(img.empty())

34         {

35             cout<<"read image "<<*begin<<" failed"<<endl;

36             continue;

37         }

38         num++;

39         Mat gray_img;

40  

41         CvMat sour_image=gray_img;

42         CvMat *inte_image = cvCreateMat(gray_img.rows+1,gray_img.cols+1,CV_32F);

43         cvIntegral(&sour_image,inte_image);

44 

45         

46         float *f = inte_image->data.fl;

47     

48         out.write((char*)(f),inte_image->width*inte_image->height*sizeof(float));

49     }

50     out.close();

51     cout<<"total image:"<<vstr.size()<<" cvIntegral number:"<<num<<endl;

52 }

53 int _tmain(int argc, _TCHAR* argv[])

54 {

55     vector<string> v;

56     getFileName(string("face.txt"),v);

57     writeIntegral(v,string("face.dat"));

58     waitKey();

59     return 0;

60 }

 

你可能感兴趣的:(image)