'svms':('http://opencv.itseez.com/modules/ml/doc/support_vector_machines.html#%s', None),
'xmlymlpers':('http://opencv.itseez.com/modules/core/doc/xml_yaml_persistence.html#%s', None),
'huivideo' : ('http://opencv.itseez.com/modules/highgui/doc/reading_and_writing_images_and_video.html#%s', None),
- 'filtering':('http://opencv.itseez.com/modules/imgproc/doc/filtering.html#%s', None),
+ 'gpuinit' : ('http://opencv.itseez.com/modules/gpu/doc/initalization_and_information.html#%s', None),
+ 'gpudatastructure' : ('http://opencv.itseez.com/modules/gpu/doc/data_structures.html#%s', None),
+ 'gpuopmatrices' : ('http://opencv.itseez.com/modules/gpu/doc/operations_on_matrices.html#%s', None),
+ 'gpuperelement' : ('http://opencv.itseez.com/modules/gpu/doc/per_element_operations.html#%s', None),
+ 'gpuimgproc' : ('http://opencv.itseez.com/modules/gpu/doc/image_processing.html#%s', None),
+ 'gpumatrixreduct' : ('http://opencv.itseez.com/modules/gpu/doc/matrix_reductions.html#%s', None),'filtering':('http://opencv.itseez.com/modules/imgproc/doc/filtering.html#%s', None),
'point_polygon_test' : ('http://opencv.willowgarage.com/documentation/cpp/imgproc_structural_analysis_and_shape_descriptors.html#cv-pointpolygontest%s', None),
'feature_detector' : ( 'http://opencv.willowgarage.com/documentation/cpp/features2d_common_interfaces_of_feature_detectors.html#featuredetector%s', None),
- 'feature_detector_detect' : ('http://opencv.willowgarage.com/documentation/cpp/features2d_common_interfaces_of_feature_detectors.html#cv-featuredetector-detect%s', None ),
- 'surf_feature_detector' : ('http://opencv.willowgarage.com/documentation/cpp/features2d_common_interfaces_of_feature_detectors.html#surffeaturedetector%s', None ),
- 'draw_keypoints' : ('http://opencv.willowgarage.com/documentation/cpp/features2d_drawing_function_of_keypoints_and_matches.html#cv-drawkeypoints%s', None ),
- 'descriptor_extractor': ( 'http://opencv.willowgarage.com/documentation/cpp/features2d_common_interfaces_of_descriptor_extractors.html#descriptorextractor%s', None ),
- 'descriptor_extractor_compute' : ( 'http://opencv.willowgarage.com/documentation/cpp/features2d_common_interfaces_of_descriptor_extractors.html#cv-descriptorextractor-compute%s', None ),
- 'surf_descriptor_extractor' : ( 'http://opencv.willowgarage.com/documentation/cpp/features2d_common_interfaces_of_descriptor_extractors.html#surfdescriptorextractor%s', None ),
- 'draw_matches' : ( 'http://opencv.willowgarage.com/documentation/cpp/features2d_drawing_function_of_keypoints_and_matches.html#cv-drawmatches%s', None ),
- 'find_homography' : ('http://opencv.willowgarage.com/documentation/cpp/calib3d_camera_calibration_and_3d_reconstruction.html?#findHomography%s', None),
- 'perspective_transform' : ('http://opencv.willowgarage.com/documentation/cpp/core_operations_on_arrays.html?#perspectiveTransform%s', None ),
- 'flann_based_matcher' : ('http://opencv.willowgarage.com/documentation/cpp/features2d_common_interfaces_of_descriptor_matchers.html?#FlannBasedMatcher%s', None),
- 'brute_force_matcher' : ('http://opencv.willowgarage.com/documentation/cpp/features2d_common_interfaces_of_descriptor_matchers.html?#BruteForceMatcher%s', None ),
- 'flann' : ('http://opencv.willowgarage.com/documentation/cpp/flann_fast_approximate_nearest_neighbor_search.html?%s', None )
+ 'feature_detector_detect' : ('http://opencv.willowgarage.com/documentation/cpp/features2d_common_interfaces_of_feature_detectors.html#cv-featuredetector-detect%s', None ),
+ 'surf_feature_detector' : ('http://opencv.willowgarage.com/documentation/cpp/features2d_common_interfaces_of_feature_detectors.html#surffeaturedetector%s', None ),
+ 'draw_keypoints' : ('http://opencv.willowgarage.com/documentation/cpp/features2d_drawing_function_of_keypoints_and_matches.html#cv-drawkeypoints%s', None ),
+ 'descriptor_extractor': ( 'http://opencv.willowgarage.com/documentation/cpp/features2d_common_interfaces_of_descriptor_extractors.html#descriptorextractor%s', None ),
+ 'descriptor_extractor_compute' : ( 'http://opencv.willowgarage.com/documentation/cpp/features2d_common_interfaces_of_descriptor_extractors.html#cv-descriptorextractor-compute%s', None ),
+ 'surf_descriptor_extractor' : ( 'http://opencv.willowgarage.com/documentation/cpp/features2d_common_interfaces_of_descriptor_extractors.html#surfdescriptorextractor%s', None ),
+ 'draw_matches' : ( 'http://opencv.willowgarage.com/documentation/cpp/features2d_drawing_function_of_keypoints_and_matches.html#cv-drawmatches%s', None ),
+ 'find_homography' : ('http://opencv.willowgarage.com/documentation/cpp/calib3d_camera_calibration_and_3d_reconstruction.html?#findHomography%s', None),
+ 'perspective_transform' : ('http://opencv.willowgarage.com/documentation/cpp/core_operations_on_arrays.html?#perspectiveTransform%s', None ),
+ 'flann_based_matcher' : ('http://opencv.willowgarage.com/documentation/cpp/features2d_common_interfaces_of_descriptor_matchers.html?#FlannBasedMatcher%s', None),
+ 'brute_force_matcher' : ('http://opencv.willowgarage.com/documentation/cpp/features2d_common_interfaces_of_descriptor_matchers.html?#BruteForceMatcher%s', None ),
+ 'flann' : ('http://opencv.willowgarage.com/documentation/cpp/flann_fast_approximate_nearest_neighbor_search.html?%s', None )
}
--- /dev/null
+#include <iostream> // Console I/O\r
+#include <sstream> // String to number conversion\r
+\r
+#include <opencv2/core/core.hpp> // Basic OpenCV structures\r
+#include <opencv2/imgproc/imgproc.hpp>// Image processing methods for the CPU\r
+#include <opencv2/highgui/highgui.hpp>// Read images\r
+#include <opencv2/gpu/gpu.hpp> // GPU structures and methods\r
+\r
+using namespace std;\r
+using namespace cv;\r
+\r
+double getPSNR(const Mat& I1, const Mat& I2); // CPU versions\r
+Scalar getMSSIM( const Mat& I1, const Mat& I2);\r
+\r
+double getPSNR_GPU(const Mat& I1, const Mat& I2); // Basic GPU versions\r
+Scalar getMSSIM_GPU( const Mat& I1, const Mat& I2);\r
+\r
+struct BufferPSNR // Optimized GPU versions\r
+{ // Data allocations are very expensive on GPU. Use a buffer to solve: allocate once reuse later.\r
+ gpu::GpuMat gI1, gI2, gs, t1,t2;\r
+\r
+ gpu::GpuMat buf;\r
+};\r
+double getPSNR_GPU_optimized(const Mat& I1, const Mat& I2, BufferPSNR& b);\r
+\r
+struct BufferMSSIM // Optimized GPU versions\r
+{ // Data allocations are very expensive on GPU. Use a buffer to solve: allocate once reuse later.\r
+ gpu::GpuMat gI1, gI2, gs, t1,t2;\r
+\r
+ gpu::GpuMat I1_2, I2_2, I1_I2;\r
+ vector<gpu::GpuMat> vI1, vI2;\r
+\r
+ gpu::GpuMat mu1, mu2; \r
+ gpu::GpuMat mu1_2, mu2_2, mu1_mu2; \r
+\r
+ gpu::GpuMat sigma1_2, sigma2_2, sigma12; \r
+ gpu::GpuMat t3; \r
+\r
+ gpu::GpuMat ssim_map;\r
+\r
+ gpu::GpuMat buf;\r
+};\r
+Scalar getMSSIM_GPU_optimized( const Mat& i1, const Mat& i2, BufferMSSIM& b);\r
+\r
+void help()\r
+{\r
+ cout\r
+ << "\n--------------------------------------------------------------------------" << endl\r
+ << "This program shows how to port your CPU code to GPU or write that from scratch." << endl\r
+ << "You can see the performance improvement for the similarity check methods (PSNR and SSIM)." << endl\r
+ << "Usage:" << endl\r
+ << "./gpu-basics-similarity referenceImage comparedImage numberOfTimesToRunTest(like 10)." << endl\r
+ << "--------------------------------------------------------------------------" << endl\r
+ << endl;\r
+}\r
+\r
+int main(int argc, char *argv[])\r
+{\r
+ help(); \r
+ Mat I1 = imread(argv[1]); // Read the two images\r
+ Mat I2 = imread(argv[2]);\r
+\r
+ if (!I1.data || !I2.data) // Check for success\r
+ {\r
+ cout << "Couldn't read the image";\r
+ return 0;\r
+ }\r
+\r
+ BufferPSNR bufferPSNR;\r
+ BufferMSSIM bufferMSSIM;\r
+ \r
+ int TIMES; \r
+ stringstream sstr(argv[3]); \r
+ sstr >> TIMES;\r
+ double time, result;\r
+\r
+ //------------------------------- PSNR CPU ----------------------------------------------------\r
+ time = (double)getTickCount(); \r
+\r
+ for (int i = 0; i < TIMES; ++i)\r
+ result = getPSNR(I1,I2);\r
+\r
+ time = 1000*((double)getTickCount() - time)/getTickFrequency();\r
+ time /= TIMES;\r
+\r
+ cout << "Time of PSNR CPU (averaged for " << TIMES << " runs): " << time << " milliseconds."\r
+ << " With result of: " << result << endl; \r
+\r
+ //------------------------------- PSNR GPU ----------------------------------------------------\r
+ time = (double)getTickCount(); \r
+\r
+ for (int i = 0; i < TIMES; ++i)\r
+ result = getPSNR_GPU(I1,I2);\r
+\r
+ time = 1000*((double)getTickCount() - time)/getTickFrequency();\r
+ time /= TIMES;\r
+\r
+ cout << "Time of PSNR GPU (averaged for " << TIMES << " runs): " << time << " milliseconds."\r
+ << " With result of: " << result << endl; \r
+\r
+ //------------------------------- PSNR GPU Optimized--------------------------------------------\r
+ time = (double)getTickCount(); // Initial call\r
+ result = getPSNR_GPU_optimized(I1, I2, bufferPSNR);\r
+ time = 1000*((double)getTickCount() - time)/getTickFrequency();\r
+ cout << "Initial call GPU optimized: " << time <<" milliseconds."\r
+ << " With result of: " << result << endl;\r
+\r
+ time = (double)getTickCount(); \r
+ for (int i = 0; i < TIMES; ++i)\r
+ result = getPSNR_GPU_optimized(I1, I2, bufferPSNR);\r
+\r
+ time = 1000*((double)getTickCount() - time)/getTickFrequency();\r
+ time /= TIMES;\r
+\r
+ cout << "Time of PSNR GPU OPTIMIZED ( / " << TIMES << " runs): " << time \r
+ << " milliseconds." << " With result of: " << result << endl << endl; \r
+\r
+\r
+ //------------------------------- SSIM CPU -----------------------------------------------------\r
+ Scalar x;\r
+ time = (double)getTickCount(); \r
+\r
+ for (int i = 0; i < TIMES; ++i)\r
+ x = getMSSIM(I1,I2);\r
+\r
+ time = 1000*((double)getTickCount() - time)/getTickFrequency();\r
+ time /= TIMES;\r
+\r
+ cout << "Time of MSSIM CPU (averaged for " << TIMES << " runs): " << time << " milliseconds."\r
+ << " With result of B" << x.val[0] << " G" << x.val[1] << " R" << x.val[2] << endl; \r
+\r
+ //------------------------------- SSIM GPU -----------------------------------------------------\r
+ time = (double)getTickCount(); \r
+\r
+ for (int i = 0; i < TIMES; ++i)\r
+ x = getMSSIM_GPU(I1,I2);\r
+\r
+ time = 1000*((double)getTickCount() - time)/getTickFrequency();\r
+ time /= TIMES;\r
+\r
+ cout << "Time of MSSIM GPU (averaged for " << TIMES << " runs): " << time << " milliseconds."\r
+ << " With result of B" << x.val[0] << " G" << x.val[1] << " R" << x.val[2] << endl; \r
+\r
+ //------------------------------- SSIM GPU Optimized--------------------------------------------\r
+ time = (double)getTickCount(); \r
+ x = getMSSIM_GPU_optimized(I1,I2, bufferMSSIM);\r
+ time = 1000*((double)getTickCount() - time)/getTickFrequency();\r
+ cout << "Time of MSSIM GPU Initial Call " << time << " milliseconds."\r
+ << " With result of B" << x.val[0] << " G" << x.val[1] << " R" << x.val[2] << endl; \r
+\r
+ time = (double)getTickCount(); \r
+\r
+ for (int i = 0; i < TIMES; ++i)\r
+ x = getMSSIM_GPU_optimized(I1,I2, bufferMSSIM);\r
+\r
+ time = 1000*((double)getTickCount() - time)/getTickFrequency();\r
+ time /= TIMES;\r
+\r
+ cout << "Time of MSSIM GPU OPTIMIZED ( / " << TIMES << " runs): " << time << " milliseconds."\r
+ << " With result of B" << x.val[0] << " G" << x.val[1] << " R" << x.val[2] << endl << endl; \r
+ return 0;\r
+}\r
+\r
+\r
+double getPSNR(const Mat& I1, const Mat& I2)\r
+{\r
+ Mat s1; \r
+ absdiff(I1, I2, s1); // |I1 - I2|\r
+ s1.convertTo(s1, CV_32F); // cannot make a square on 8 bits\r
+ s1 = s1.mul(s1); // |I1 - I2|^2\r
+\r
+ Scalar s = sum(s1); // sum elements per channel\r
+\r
+ double sse = s.val[0] + s.val[1] + s.val[2]; // sum channels\r
+\r
+ if( sse <= 1e-10) // for small values return zero\r
+ return 0;\r
+ else\r
+ {\r
+ double mse =sse /(double)(I1.channels() * I1.total());\r
+ double psnr = 10.0*log10((255*255)/mse);\r
+ return psnr;\r
+ }\r
+}\r
+\r
+double getPSNR_GPU(const Mat& I1, const Mat& I2)\r
+{\r
+ gpu::GpuMat gI1, gI2, gs, t1,t2; \r
+\r
+ gI1.upload(I1);\r
+ gI2.upload(I2);\r
+\r
+ gI1.convertTo(t1, CV_32F);\r
+ gI2.convertTo(t2, CV_32F);\r
+\r
+ gpu::absdiff(t1.reshape(1), t2.reshape(1), gs); \r
+ gpu::multiply(gs, gs, gs);\r
+ \r
+ Scalar s = gpu::sum(gs);\r
+ double sse = s.val[0] + s.val[1] + s.val[2];\r
+\r
+ if( sse <= 1e-10) // for small values return zero\r
+ return 0;\r
+ else\r
+ {\r
+ double mse =sse /(double)(gI1.channels() * I1.total());\r
+ double psnr = 10.0*log10((255*255)/mse);\r
+ return psnr;\r
+ }\r
+}\r
+\r
+double getPSNR_GPU_optimized(const Mat& I1, const Mat& I2, BufferPSNR& b)\r
+{ \r
+ b.gI1.upload(I1);\r
+ b.gI2.upload(I2);\r
+\r
+ b.gI1.convertTo(b.t1, CV_32F);\r
+ b.gI2.convertTo(b.t2, CV_32F);\r
+\r
+ gpu::absdiff(b.t1.reshape(1), b.t2.reshape(1), b.gs);\r
+ gpu::multiply(b.gs, b.gs, b.gs);\r
+\r
+ double sse = gpu::sum(b.gs, b.buf)[0];\r
+\r
+ if( sse <= 1e-10) // for small values return zero\r
+ return 0;\r
+ else\r
+ {\r
+ double mse = sse /(double)(I1.channels() * I1.total());\r
+ double psnr = 10.0*log10((255*255)/mse);\r
+ return psnr;\r
+ }\r
+}\r
+\r
+Scalar getMSSIM( const Mat& i1, const Mat& i2)\r
+{ \r
+ const double C1 = 6.5025, C2 = 58.5225;\r
+ int d = CV_32F;\r
+\r
+ Mat I1, I2; \r
+ i1.convertTo(I1, d); // cannot calculate on one byte large values\r
+ i2.convertTo(I2, d); \r
+\r
+ Mat I2_2 = I2.mul(I2); // I2^2\r
+ Mat I1_2 = I1.mul(I1); // I1^2\r
+ Mat I1_I2 = I1.mul(I2); // I1 * I2\r
+ \r
+ Mat mu1, mu2; \r
+ GaussianBlur(I1, mu1, Size(11, 11), 1.5);\r
+ GaussianBlur(I2, mu2, Size(11, 11), 1.5);\r
+\r
+ Mat mu1_2 = mu1.mul(mu1); \r
+ Mat mu2_2 = mu2.mul(mu2); \r
+ Mat mu1_mu2 = mu1.mul(mu2);\r
+\r
+ Mat sigma1_2, sigma2_2, sigma12; \r
+\r
+ GaussianBlur(I1_2, sigma1_2, Size(11, 11), 1.5);\r
+ sigma1_2 -= mu1_2;\r
+\r
+ GaussianBlur(I2_2, sigma2_2, Size(11, 11), 1.5);\r
+ sigma2_2 -= mu2_2;\r
+\r
+ GaussianBlur(I1_I2, sigma12, Size(11, 11), 1.5);\r
+ sigma12 -= mu1_mu2;\r
+\r
+ ///////////////////////////////// FORMULA ////////////////////////////////\r
+ Mat t1, t2, t3; \r
+\r
+ t1 = 2 * mu1_mu2 + C1; \r
+ t2 = 2 * sigma12 + C2; \r
+ t3 = t1.mul(t2); // t3 = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))\r
+\r
+ t1 = mu1_2 + mu2_2 + C1; \r
+ t2 = sigma1_2 + sigma2_2 + C2; \r
+ t1 = t1.mul(t2); // t1 =((mu1_2 + mu2_2 + C1).*(sigma1_2 + sigma2_2 + C2))\r
+\r
+ Mat ssim_map;\r
+ divide(t3, t1, ssim_map); // ssim_map = t3./t1;\r
+\r
+ Scalar mssim = mean( ssim_map ); // mssim = average of ssim map\r
+ return mssim; \r
+}\r
+\r
+Scalar getMSSIM_GPU_optimized( const Mat& i1, const Mat& i2, BufferMSSIM& b)\r
+{ \r
+ int cn = i1.channels();\r
+ const float C1 = 6.5025f, C2 = 58.5225f;\r
+\r
+ b.gI1.upload(i1);\r
+ b.gI2.upload(i2);\r
+\r
+ gpu::Stream stream;\r
+\r
+ stream.enqueueConvert(b.gI1, b.t1, CV_32F);\r
+ stream.enqueueConvert(b.gI2, b.t2, CV_32F); \r
+\r
+ gpu::split(b.t1, b.vI1, stream);\r
+ gpu::split(b.t2, b.vI2, stream);\r
+ Scalar mssim;\r
+\r
+ for( int i = 0; i < b.gI1.channels(); ++i )\r
+ { \r
+ gpu::multiply(b.vI2[i], b.vI2[i], b.I2_2, stream); // I2^2\r
+ gpu::multiply(b.vI1[i], b.vI1[i], b.I1_2, stream); // I1^2\r
+ gpu::multiply(b.vI1[i], b.vI2[i], b.I1_I2, stream); // I1 * I2\r
+\r
+ gpu::GaussianBlur(b.vI1[i], b.mu1, Size(11, 11), 1.5, 0, BORDER_DEFAULT, -1, stream);\r
+ gpu::GaussianBlur(b.vI2[i], b.mu2, Size(11, 11), 1.5, 0, BORDER_DEFAULT, -1, stream);\r
+\r
+ gpu::multiply(b.mu1, b.mu1, b.mu1_2, stream); \r
+ gpu::multiply(b.mu2, b.mu2, b.mu2_2, stream); \r
+ gpu::multiply(b.mu1, b.mu2, b.mu1_mu2, stream); \r
+\r
+ gpu::GaussianBlur(b.I1_2, b.sigma1_2, Size(11, 11), 1.5, 0, BORDER_DEFAULT, -1, stream);\r
+ gpu::subtract(b.sigma1_2, b.mu1_2, b.sigma1_2, stream);\r
+ //b.sigma1_2 -= b.mu1_2; - This would result in an extra data transfer operation\r
+\r
+ gpu::GaussianBlur(b.I2_2, b.sigma2_2, Size(11, 11), 1.5, 0, BORDER_DEFAULT, -1, stream);\r
+ gpu::subtract(b.sigma2_2, b.mu2_2, b.sigma2_2, stream);\r
+ //b.sigma2_2 -= b.mu2_2;\r
+\r
+ gpu::GaussianBlur(b.I1_I2, b.sigma12, Size(11, 11), 1.5, 0, BORDER_DEFAULT, -1, stream);\r
+ gpu::subtract(b.sigma12, b.mu1_mu2, b.sigma12, stream);\r
+ //b.sigma12 -= b.mu1_mu2;\r
+\r
+ //here too it would be an extra data transfer due to call of operator*(Scalar, Mat)\r
+ gpu::multiply(b.mu1_mu2, 2, b.t1, stream); //b.t1 = 2 * b.mu1_mu2 + C1; \r
+ gpu::add(b.t1, C1, b.t1, stream);\r
+ gpu::multiply(b.sigma12, 2, b.t2, stream); //b.t2 = 2 * b.sigma12 + C2; \r
+ gpu::add(b.t2, C2, b.t2, stream); \r
+\r
+ gpu::multiply(b.t1, b.t2, b.t3, stream); // t3 = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))\r
+\r
+ gpu::add(b.mu1_2, b.mu2_2, b.t1, stream);\r
+ gpu::add(b.t1, C1, b.t1, stream);\r
+\r
+ gpu::add(b.sigma1_2, b.sigma2_2, b.t2, stream);\r
+ gpu::add(b.t2, C2, b.t2, stream);\r
+\r
+\r
+ gpu::multiply(b.t1, b.t2, b.t1, stream); // t1 =((mu1_2 + mu2_2 + C1).*(sigma1_2 + sigma2_2 + C2)) \r
+ gpu::divide(b.t3, b.t1, b.ssim_map, stream); // ssim_map = t3./t1;\r
+\r
+ stream.waitForCompletion();\r
+\r
+ Scalar s = gpu::sum(b.ssim_map, b.buf); \r
+ mssim.val[i] = s.val[0] / (b.ssim_map.rows * b.ssim_map.cols);\r
+\r
+ }\r
+ return mssim; \r
+}
\ No newline at end of file