ait-survey

OpenCV-Average

Dec 7th, 2013
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. #include <opencv2/core/core.hpp>
  2. #include <opencv2/imgproc/imgproc.hpp>
  3. #include <opencv2/highgui/highgui.hpp>
  4. #include <iostream>
  5. #include <cstdlib>
  6. #include <fstream>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <iomanip>
  11.  
  12. using namespace cv;
  13. using namespace std;
  14.  
  15. int
  16. main(int argc, char *argv[])
  17. {
  18.   FILE *fpo;
  19.   int i;
  20.   float up;
  21.   float total;
  22.   float average;
  23.  
  24.   //ofstream out_img;
  25.   //out_img.open(argv[2],ios::out | ios::binary);
  26.   fpo=fopen(argv[2],"wb");
  27.  
  28.   //cout << "check0\n" <<endl;
  29.  
  30.   up=atof(argv[3]);
  31.  
  32.   Mat src_img = imread(argv[1], 2);
  33.   //Mat src_img = imread("depth_0010-01.tif", 2);
  34.   if(src_img.empty()) return -1;
  35.   //Mat dst_img1;
  36.   Mat dst_img2(src_img.rows*up, src_img.cols*up, CV_32FC1);
  37.  
  38.   //cout << "check0-1\n" <<endl;
  39.  
  40.   //float *buffer;
  41.   //buffer=(float *)malloc(dst_img2.rows*dst_img2.cols*sizeof(float));
  42.  
  43.   //cout << "check0-2\n" <<endl;
  44.  
  45.   // INTER_LINER(バイリニア補間)でのサイズ変更
  46.   //resize(src_img, dst_img1, Size(), 0.3, 0.3);
  47.   // INTER_CUBIC(バイキュービック補間)でのサイズ変更
  48.   //resize(src_img, dst_img2, dst_img2.size(), INTER_CUBIC);
  49.   resize(src_img,dst_img2,Size(),up,up,0);
  50.  
  51.   //namedWindow("resize image1", CV_WINDOW_AUTOSIZE|CV_WINDOW_FREERATIO);
  52.  namedWindow("resize image2", CV_WINDOW_AUTOSIZE|CV_WINDOW_FREERATIO);
  53.   //imshow("resize image1", dst_img1);
  54.   imshow("resize image2", dst_img2);
  55.  
  56.   //cout << "check1\n" <<endl;
  57.  
  58.   //cout << dst_img2.rows*dst_img2.cols << endl;
  59.  
  60.  float dn;
  61. /*
  62.   for(int i=0;i<src_img.rows*3; i++)
  63.   {
  64.      for(int j=0;j<src_img.cols*3; j++)
  65.      {
  66.         //out_img.put(dst_img2.data[i]);
  67.        // dn=(int)(dst_img2.data[i*dst_img2.cols+j]*10);
  68.  
  69.          // cout << dst_img2.data[i*dst_img2.cols+j] <<endl;
  70.         //cout << i <<endl;
  71.     //printf("%f\n",dn);
  72.    
  73.     //out_img.put(dst_img2.data[i*dst_img2.cols+j]);
  74.         dn=dst_img2.data[i*src_img.cols*3+j];
  75.         //out_img.write(dn,1);
  76.        //out_img <<
  77.        //fwrite(
  78.        //printf("%f\n",dn);
  79.        fwrite(&dn,sizeof(float),1,fpo);
  80.    
  81.      }
  82.   }
  83. */
  84.   total=0;
  85.  
  86.   //out_img.write(dst_img2.data,dst_img2.rows*dst_img2.cols);
  87.   for(i=0;i<dst_img2.rows*dst_img2.cols;i++)
  88.   {
  89.      total=total+dst_img2.data[i];
  90.   }
  91.  
  92.   average=total/100000000/dst_img2.rows*dst_img2.cols;
  93.   //printf("\nThe average depth is %fm.",average);
  94.   cout << endl << "The average depth is " << fixed << setprecision(2) << average << "m." << endl;
  95.  
  96.  
  97.   fwrite(dst_img2.data,sizeof(float),dst_img2.rows*dst_img2.cols,fpo);
  98.   //cout << "check2\n" <<endl;
  99.   //cout << "check3\n" <<endl;  
  100.  
  101.   //imwrite("output.tif",dst_img2);
  102.   fclose(fpo);
  103.    
  104.   waitKey(0);
  105.  
  106. }
Add Comment
Please, Sign In to add comment