Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.97 KB | None | 0 0
  1. //g++ -std=c++11 `pkg-config --cflags opencv` Fix.cpp -o Saliency `pkg-config --libs opencv`
  2.  
  3. #include "opencv2/highgui/highgui.hpp"
  4. #include "opencv2/core/core.hpp"
  5. #include "opencv2/imgproc/imgproc.hpp"
  6.  
  7. #include "opencv2/core/cuda.hpp"
  8. #include "opencv2/cudaimgproc.hpp"
  9. #include "opencv2/cudafilters.hpp"
  10. #include "opencv2/cudawarping.hpp"
  11. #include "opencv2/cudaarithm.hpp"
  12.  
  13. #include <time.h>
  14. #include <cmath>
  15. #include <vector>
  16. #include <iostream>
  17. #include <string>
  18.  
  19. using namespace cv;
  20. using namespace cv::cuda;
  21. using namespace std;
  22.  
  23. void Perform_Saliency (Mat& Input, Mat& Saliency);
  24.  
  25. int main( int argc, char** argv )
  26. {
  27.   // VideoCapture stream(0);
  28.  
  29.   for(int i = 1; i < argc; i++){
  30.     Mat Input, ObjFound, Saliency;
  31.     vector<Rect> boundRect;
  32.     const string name = argv[i];
  33.  
  34.     while(true){
  35.       //stream >> Input;
  36.       Input = imread(name);
  37.       imshow("Input", Input);
  38.       int k = waitKey(1);
  39.  
  40.       if (char(k) == '1'){
  41.  
  42.     clock_t time = clock();
  43.  
  44.     Saliency = Mat(Input.size(), Input.type());
  45.  
  46.     Perform_Saliency (Input, Saliency);
  47.    
  48.     printf("Time taken: %.2fs\n", (double)(clock() - time) / CLOCKS_PER_SEC);
  49.  
  50.     imshow("Saliency", Saliency);
  51.  
  52.       }
  53.       else if (char(k) == '/'){break;}
  54.  
  55.     }
  56.   }
  57.   return 0;
  58. }
  59.  
  60. void Perform_Saliency (Mat& Input, Mat& Saliency){
  61.  
  62.   int Pyramid_Size = 5;
  63.  
  64.   cv::cvtColor(Input, Input , CV_BGR2GRAY);
  65.  
  66.   GpuMat compare(Input);
  67.  
  68.   GaussianBlur(Input, Input, Size(5,5), 0,0);
  69.  
  70.   GpuMat DownUp(Input);
  71.  
  72.   //  DownUp = Input.clone();
  73.   DownUp.convertTo(DownUp, CV_32F);
  74.   compare.convertTo(compare, CV_32F);
  75.  
  76.   for (int i = 0; i < Pyramid_Size; i++){
  77.     pyrDown(DownUp, DownUp, Size(DownUp.cols/2, DownUp.rows/2));
  78.   }
  79.  
  80.   for (int i = 0; i < Pyramid_Size; i++){
  81.     pyrUp(DownUp, DownUp, Size(DownUp.cols*2, DownUp.rows*2));
  82.     GpuMat a = GpuMat(DownUp.size(), CV_32F, 20.0f);
  83.     cuda::add(a, DownUp, DownUp);
  84.   }
  85.  
  86.   Mat cpu_DownUp;
  87.   Mat cpu_compare;
  88.  
  89.   // Problem : For whatever reason sending a GpuMat back to Mat results in an error saying it wasn't implented and that I should try to explicitly cast it...
  90.   // GpuMat compare(Input) is an example where a GpuMat is set as a Mat (stuff processed on cpu).
  91.   // Also, there is no option in GpuMat that I could find that let me do pixel operations.
  92.  
  93.   DownUp.download(cpu_DownUp);
  94.   compare.download(cpu_compare);
  95.  
  96.   for (int x = 0; x < DownUp.rows; x++){
  97.     for (int y = 0; y < DownUp.cols; y++){
  98.  
  99.       float a,b,c;
  100.       a = cpu_DownUp.at<float>(x,y);
  101.       b = cpu_compare.at<float>(x,y);
  102.  
  103.       if (a < b) {c = 1.0 - a/b;}
  104.       else       {c = 1.0 - b/a;}
  105.      
  106.       Saliency.at<float>(x,y) = sqrt(c);
  107.  
  108.       if (x == 100 && y == 100){cout << "a = " << a << " b = " << b << " c = " << c << endl;}
  109.      
  110.     }
  111.   }
  112.  
  113. }
  114.  
  115. /*
  116.  
  117.  for (int a = 0; a < DownUp.rows; a++){
  118.       for (int b = 0; b < DownUp.cols; b++){
  119.     float hold = DownUp.at<float> (a,b);
  120.     DownUp.at<float>(a,b) = hold + 20;
  121.       }
  122.     }
  123.  
  124.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement