Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //g++ -std=c++11 `pkg-config --cflags opencv` Fix.cpp -o Saliency `pkg-config --libs opencv`
- #include "opencv2/highgui/highgui.hpp"
- #include "opencv2/core/core.hpp"
- #include "opencv2/imgproc/imgproc.hpp"
- #include "opencv2/core/cuda.hpp"
- #include "opencv2/cudaimgproc.hpp"
- #include "opencv2/cudafilters.hpp"
- #include "opencv2/cudawarping.hpp"
- #include "opencv2/cudaarithm.hpp"
- #include <time.h>
- #include <cmath>
- #include <vector>
- #include <iostream>
- #include <string>
- using namespace cv;
- using namespace cv::cuda;
- using namespace std;
- void Perform_Saliency (Mat& Input, Mat& Saliency);
- int main( int argc, char** argv )
- {
- // VideoCapture stream(0);
- for(int i = 1; i < argc; i++){
- Mat Input, ObjFound, Saliency;
- vector<Rect> boundRect;
- const string name = argv[i];
- while(true){
- //stream >> Input;
- Input = imread(name);
- imshow("Input", Input);
- int k = waitKey(1);
- if (char(k) == '1'){
- clock_t time = clock();
- Saliency = Mat(Input.size(), Input.type());
- Perform_Saliency (Input, Saliency);
- printf("Time taken: %.2fs\n", (double)(clock() - time) / CLOCKS_PER_SEC);
- imshow("Saliency", Saliency);
- }
- else if (char(k) == '/'){break;}
- }
- }
- return 0;
- }
- void Perform_Saliency (Mat& Input, Mat& Saliency){
- int Pyramid_Size = 5;
- cv::cvtColor(Input, Input , CV_BGR2GRAY);
- GpuMat compare(Input);
- GaussianBlur(Input, Input, Size(5,5), 0,0);
- GpuMat DownUp(Input);
- // DownUp = Input.clone();
- DownUp.convertTo(DownUp, CV_32F);
- compare.convertTo(compare, CV_32F);
- for (int i = 0; i < Pyramid_Size; i++){
- pyrDown(DownUp, DownUp, Size(DownUp.cols/2, DownUp.rows/2));
- }
- for (int i = 0; i < Pyramid_Size; i++){
- pyrUp(DownUp, DownUp, Size(DownUp.cols*2, DownUp.rows*2));
- GpuMat a = GpuMat(DownUp.size(), CV_32F, 20.0f);
- cuda::add(a, DownUp, DownUp);
- }
- Mat cpu_DownUp;
- Mat cpu_compare;
- // 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...
- // GpuMat compare(Input) is an example where a GpuMat is set as a Mat (stuff processed on cpu).
- // Also, there is no option in GpuMat that I could find that let me do pixel operations.
- DownUp.download(cpu_DownUp);
- compare.download(cpu_compare);
- for (int x = 0; x < DownUp.rows; x++){
- for (int y = 0; y < DownUp.cols; y++){
- float a,b,c;
- a = cpu_DownUp.at<float>(x,y);
- b = cpu_compare.at<float>(x,y);
- if (a < b) {c = 1.0 - a/b;}
- else {c = 1.0 - b/a;}
- Saliency.at<float>(x,y) = sqrt(c);
- if (x == 100 && y == 100){cout << "a = " << a << " b = " << b << " c = " << c << endl;}
- }
- }
- }
- /*
- for (int a = 0; a < DownUp.rows; a++){
- for (int b = 0; b < DownUp.cols; b++){
- float hold = DownUp.at<float> (a,b);
- DownUp.at<float>(a,b) = hold + 20;
- }
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement