gpu-basics-similarity.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. #include <iostream> // Console I/O
  2. #include <sstream> // String to number conversion
  3. #include <opencv2/core.hpp> // Basic OpenCV structures
  4. #include <opencv2/core/utility.hpp>
  5. #include <opencv2/imgproc.hpp>// Image processing methods for the CPU
  6. #include <opencv2/imgcodecs.hpp>// Read images
  7. // CUDA structures and methods
  8. #include <opencv2/cudaarithm.hpp>
  9. #include <opencv2/cudafilters.hpp>
  10. using namespace std;
  11. using namespace cv;
  12. double getPSNR(const Mat& I1, const Mat& I2); // CPU versions
  13. Scalar getMSSIM( const Mat& I1, const Mat& I2);
  14. double getPSNR_CUDA(const Mat& I1, const Mat& I2); // Basic CUDA versions
  15. Scalar getMSSIM_CUDA( const Mat& I1, const Mat& I2);
  16. //! [psnr]
  17. struct BufferPSNR // Optimized CUDA versions
  18. { // Data allocations are very expensive on CUDA. Use a buffer to solve: allocate once reuse later.
  19. cuda::GpuMat gI1, gI2, gs, t1,t2;
  20. cuda::GpuMat buf;
  21. };
  22. //! [psnr]
  23. double getPSNR_CUDA_optimized(const Mat& I1, const Mat& I2, BufferPSNR& b);
  24. //! [ssim]
  25. struct BufferMSSIM // Optimized CUDA versions
  26. { // Data allocations are very expensive on CUDA. Use a buffer to solve: allocate once reuse later.
  27. cuda::GpuMat gI1, gI2, gs, t1,t2;
  28. cuda::GpuMat I1_2, I2_2, I1_I2;
  29. vector<cuda::GpuMat> vI1, vI2;
  30. cuda::GpuMat mu1, mu2;
  31. cuda::GpuMat mu1_2, mu2_2, mu1_mu2;
  32. cuda::GpuMat sigma1_2, sigma2_2, sigma12;
  33. cuda::GpuMat t3;
  34. cuda::GpuMat ssim_map;
  35. cuda::GpuMat buf;
  36. };
  37. //! [ssim]
  38. Scalar getMSSIM_CUDA_optimized( const Mat& i1, const Mat& i2, BufferMSSIM& b);
  39. static void help()
  40. {
  41. cout
  42. << "\n--------------------------------------------------------------------------" << endl
  43. << "This program shows how to port your CPU code to CUDA or write that from scratch." << endl
  44. << "You can see the performance improvement for the similarity check methods (PSNR and SSIM)." << endl
  45. << "Usage:" << endl
  46. << "./gpu-basics-similarity referenceImage comparedImage numberOfTimesToRunTest(like 10)." << endl
  47. << "--------------------------------------------------------------------------" << endl
  48. << endl;
  49. }
  50. int main(int, char *argv[])
  51. {
  52. help();
  53. Mat I1 = imread(argv[1]); // Read the two images
  54. Mat I2 = imread(argv[2]);
  55. if (!I1.data || !I2.data) // Check for success
  56. {
  57. cout << "Couldn't read the image";
  58. return 0;
  59. }
  60. BufferPSNR bufferPSNR;
  61. BufferMSSIM bufferMSSIM;
  62. int TIMES = 10;
  63. stringstream sstr(argv[3]);
  64. sstr >> TIMES;
  65. double time, result = 0;
  66. //------------------------------- PSNR CPU ----------------------------------------------------
  67. time = (double)getTickCount();
  68. for (int i = 0; i < TIMES; ++i)
  69. result = getPSNR(I1,I2);
  70. time = 1000*((double)getTickCount() - time)/getTickFrequency();
  71. time /= TIMES;
  72. cout << "Time of PSNR CPU (averaged for " << TIMES << " runs): " << time << " milliseconds."
  73. << " With result of: " << result << endl;
  74. //------------------------------- PSNR CUDA ----------------------------------------------------
  75. time = (double)getTickCount();
  76. for (int i = 0; i < TIMES; ++i)
  77. result = getPSNR_CUDA(I1,I2);
  78. time = 1000*((double)getTickCount() - time)/getTickFrequency();
  79. time /= TIMES;
  80. cout << "Time of PSNR CUDA (averaged for " << TIMES << " runs): " << time << " milliseconds."
  81. << " With result of: " << result << endl;
  82. //------------------------------- PSNR CUDA Optimized--------------------------------------------
  83. time = (double)getTickCount(); // Initial call
  84. result = getPSNR_CUDA_optimized(I1, I2, bufferPSNR);
  85. time = 1000*((double)getTickCount() - time)/getTickFrequency();
  86. cout << "Initial call CUDA optimized: " << time <<" milliseconds."
  87. << " With result of: " << result << endl;
  88. time = (double)getTickCount();
  89. for (int i = 0; i < TIMES; ++i)
  90. result = getPSNR_CUDA_optimized(I1, I2, bufferPSNR);
  91. time = 1000*((double)getTickCount() - time)/getTickFrequency();
  92. time /= TIMES;
  93. cout << "Time of PSNR CUDA OPTIMIZED ( / " << TIMES << " runs): " << time
  94. << " milliseconds." << " With result of: " << result << endl << endl;
  95. //------------------------------- SSIM CPU -----------------------------------------------------
  96. Scalar x;
  97. time = (double)getTickCount();
  98. for (int i = 0; i < TIMES; ++i)
  99. x = getMSSIM(I1,I2);
  100. time = 1000*((double)getTickCount() - time)/getTickFrequency();
  101. time /= TIMES;
  102. cout << "Time of MSSIM CPU (averaged for " << TIMES << " runs): " << time << " milliseconds."
  103. << " With result of B" << x.val[0] << " G" << x.val[1] << " R" << x.val[2] << endl;
  104. //------------------------------- SSIM CUDA -----------------------------------------------------
  105. time = (double)getTickCount();
  106. for (int i = 0; i < TIMES; ++i)
  107. x = getMSSIM_CUDA(I1,I2);
  108. time = 1000*((double)getTickCount() - time)/getTickFrequency();
  109. time /= TIMES;
  110. cout << "Time of MSSIM CUDA (averaged for " << TIMES << " runs): " << time << " milliseconds."
  111. << " With result of B" << x.val[0] << " G" << x.val[1] << " R" << x.val[2] << endl;
  112. //------------------------------- SSIM CUDA Optimized--------------------------------------------
  113. time = (double)getTickCount();
  114. x = getMSSIM_CUDA_optimized(I1,I2, bufferMSSIM);
  115. time = 1000*((double)getTickCount() - time)/getTickFrequency();
  116. cout << "Time of MSSIM CUDA Initial Call " << time << " milliseconds."
  117. << " With result of B" << x.val[0] << " G" << x.val[1] << " R" << x.val[2] << endl;
  118. time = (double)getTickCount();
  119. for (int i = 0; i < TIMES; ++i)
  120. x = getMSSIM_CUDA_optimized(I1,I2, bufferMSSIM);
  121. time = 1000*((double)getTickCount() - time)/getTickFrequency();
  122. time /= TIMES;
  123. cout << "Time of MSSIM CUDA OPTIMIZED ( / " << TIMES << " runs): " << time << " milliseconds."
  124. << " With result of B" << x.val[0] << " G" << x.val[1] << " R" << x.val[2] << endl << endl;
  125. return 0;
  126. }
  127. //! [getpsnr]
  128. double getPSNR(const Mat& I1, const Mat& I2)
  129. {
  130. Mat s1;
  131. absdiff(I1, I2, s1); // |I1 - I2|
  132. s1.convertTo(s1, CV_32F); // cannot make a square on 8 bits
  133. s1 = s1.mul(s1); // |I1 - I2|^2
  134. Scalar s = sum(s1); // sum elements per channel
  135. double sse = s.val[0] + s.val[1] + s.val[2]; // sum channels
  136. if( sse <= 1e-10) // for small values return zero
  137. return 0;
  138. else
  139. {
  140. double mse =sse /(double)(I1.channels() * I1.total());
  141. double psnr = 10.0*log10((255*255)/mse);
  142. return psnr;
  143. }
  144. }
  145. //! [getpsnr]
  146. //! [getpsnropt]
  147. double getPSNR_CUDA_optimized(const Mat& I1, const Mat& I2, BufferPSNR& b)
  148. {
  149. b.gI1.upload(I1);
  150. b.gI2.upload(I2);
  151. b.gI1.convertTo(b.t1, CV_32F);
  152. b.gI2.convertTo(b.t2, CV_32F);
  153. cuda::absdiff(b.t1.reshape(1), b.t2.reshape(1), b.gs);
  154. cuda::multiply(b.gs, b.gs, b.gs);
  155. double sse = cuda::sum(b.gs, b.buf)[0];
  156. if( sse <= 1e-10) // for small values return zero
  157. return 0;
  158. else
  159. {
  160. double mse = sse /(double)(I1.channels() * I1.total());
  161. double psnr = 10.0*log10((255*255)/mse);
  162. return psnr;
  163. }
  164. }
  165. //! [getpsnropt]
  166. //! [getpsnrcuda]
  167. double getPSNR_CUDA(const Mat& I1, const Mat& I2)
  168. {
  169. cuda::GpuMat gI1, gI2, gs, t1,t2;
  170. gI1.upload(I1);
  171. gI2.upload(I2);
  172. gI1.convertTo(t1, CV_32F);
  173. gI2.convertTo(t2, CV_32F);
  174. cuda::absdiff(t1.reshape(1), t2.reshape(1), gs);
  175. cuda::multiply(gs, gs, gs);
  176. Scalar s = cuda::sum(gs);
  177. double sse = s.val[0] + s.val[1] + s.val[2];
  178. if( sse <= 1e-10) // for small values return zero
  179. return 0;
  180. else
  181. {
  182. double mse =sse /(double)(gI1.channels() * I1.total());
  183. double psnr = 10.0*log10((255*255)/mse);
  184. return psnr;
  185. }
  186. }
  187. //! [getpsnrcuda]
  188. //! [getssim]
  189. Scalar getMSSIM( const Mat& i1, const Mat& i2)
  190. {
  191. const double C1 = 6.5025, C2 = 58.5225;
  192. /***************************** INITS **********************************/
  193. int d = CV_32F;
  194. Mat I1, I2;
  195. i1.convertTo(I1, d); // cannot calculate on one byte large values
  196. i2.convertTo(I2, d);
  197. Mat I2_2 = I2.mul(I2); // I2^2
  198. Mat I1_2 = I1.mul(I1); // I1^2
  199. Mat I1_I2 = I1.mul(I2); // I1 * I2
  200. /*************************** END INITS **********************************/
  201. Mat mu1, mu2; // PRELIMINARY COMPUTING
  202. GaussianBlur(I1, mu1, Size(11, 11), 1.5);
  203. GaussianBlur(I2, mu2, Size(11, 11), 1.5);
  204. Mat mu1_2 = mu1.mul(mu1);
  205. Mat mu2_2 = mu2.mul(mu2);
  206. Mat mu1_mu2 = mu1.mul(mu2);
  207. Mat sigma1_2, sigma2_2, sigma12;
  208. GaussianBlur(I1_2, sigma1_2, Size(11, 11), 1.5);
  209. sigma1_2 -= mu1_2;
  210. GaussianBlur(I2_2, sigma2_2, Size(11, 11), 1.5);
  211. sigma2_2 -= mu2_2;
  212. GaussianBlur(I1_I2, sigma12, Size(11, 11), 1.5);
  213. sigma12 -= mu1_mu2;
  214. ///////////////////////////////// FORMULA ////////////////////////////////
  215. Mat t1, t2, t3;
  216. t1 = 2 * mu1_mu2 + C1;
  217. t2 = 2 * sigma12 + C2;
  218. t3 = t1.mul(t2); // t3 = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))
  219. t1 = mu1_2 + mu2_2 + C1;
  220. t2 = sigma1_2 + sigma2_2 + C2;
  221. t1 = t1.mul(t2); // t1 =((mu1_2 + mu2_2 + C1).*(sigma1_2 + sigma2_2 + C2))
  222. Mat ssim_map;
  223. divide(t3, t1, ssim_map); // ssim_map = t3./t1;
  224. Scalar mssim = mean( ssim_map ); // mssim = average of ssim map
  225. return mssim;
  226. }
  227. //! [getssim]
  228. //! [getssimcuda]
  229. Scalar getMSSIM_CUDA( const Mat& i1, const Mat& i2)
  230. {
  231. const float C1 = 6.5025f, C2 = 58.5225f;
  232. /***************************** INITS **********************************/
  233. cuda::GpuMat gI1, gI2, gs1, tmp1,tmp2;
  234. gI1.upload(i1);
  235. gI2.upload(i2);
  236. gI1.convertTo(tmp1, CV_MAKE_TYPE(CV_32F, gI1.channels()));
  237. gI2.convertTo(tmp2, CV_MAKE_TYPE(CV_32F, gI2.channels()));
  238. vector<cuda::GpuMat> vI1, vI2;
  239. cuda::split(tmp1, vI1);
  240. cuda::split(tmp2, vI2);
  241. Scalar mssim;
  242. Ptr<cuda::Filter> gauss = cuda::createGaussianFilter(vI2[0].type(), -1, Size(11, 11), 1.5);
  243. for( int i = 0; i < gI1.channels(); ++i )
  244. {
  245. cuda::GpuMat I2_2, I1_2, I1_I2;
  246. cuda::multiply(vI2[i], vI2[i], I2_2); // I2^2
  247. cuda::multiply(vI1[i], vI1[i], I1_2); // I1^2
  248. cuda::multiply(vI1[i], vI2[i], I1_I2); // I1 * I2
  249. /*************************** END INITS **********************************/
  250. cuda::GpuMat mu1, mu2; // PRELIMINARY COMPUTING
  251. gauss->apply(vI1[i], mu1);
  252. gauss->apply(vI2[i], mu2);
  253. cuda::GpuMat mu1_2, mu2_2, mu1_mu2;
  254. cuda::multiply(mu1, mu1, mu1_2);
  255. cuda::multiply(mu2, mu2, mu2_2);
  256. cuda::multiply(mu1, mu2, mu1_mu2);
  257. cuda::GpuMat sigma1_2, sigma2_2, sigma12;
  258. gauss->apply(I1_2, sigma1_2);
  259. cuda::subtract(sigma1_2, mu1_2, sigma1_2); // sigma1_2 -= mu1_2;
  260. gauss->apply(I2_2, sigma2_2);
  261. cuda::subtract(sigma2_2, mu2_2, sigma2_2); // sigma2_2 -= mu2_2;
  262. gauss->apply(I1_I2, sigma12);
  263. cuda::subtract(sigma12, mu1_mu2, sigma12); // sigma12 -= mu1_mu2;
  264. ///////////////////////////////// FORMULA ////////////////////////////////
  265. cuda::GpuMat t1, t2, t3;
  266. mu1_mu2.convertTo(t1, -1, 2, C1); // t1 = 2 * mu1_mu2 + C1;
  267. sigma12.convertTo(t2, -1, 2, C2); // t2 = 2 * sigma12 + C2;
  268. cuda::multiply(t1, t2, t3); // t3 = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))
  269. cuda::addWeighted(mu1_2, 1.0, mu2_2, 1.0, C1, t1); // t1 = mu1_2 + mu2_2 + C1;
  270. cuda::addWeighted(sigma1_2, 1.0, sigma2_2, 1.0, C2, t2); // t2 = sigma1_2 + sigma2_2 + C2;
  271. cuda::multiply(t1, t2, t1); // t1 =((mu1_2 + mu2_2 + C1).*(sigma1_2 + sigma2_2 + C2))
  272. cuda::GpuMat ssim_map;
  273. cuda::divide(t3, t1, ssim_map); // ssim_map = t3./t1;
  274. Scalar s = cuda::sum(ssim_map);
  275. mssim.val[i] = s.val[0] / (ssim_map.rows * ssim_map.cols);
  276. }
  277. return mssim;
  278. }
  279. //! [getssimcuda]
  280. //! [getssimopt]
  281. Scalar getMSSIM_CUDA_optimized( const Mat& i1, const Mat& i2, BufferMSSIM& b)
  282. {
  283. const float C1 = 6.5025f, C2 = 58.5225f;
  284. /***************************** INITS **********************************/
  285. b.gI1.upload(i1);
  286. b.gI2.upload(i2);
  287. cuda::Stream stream;
  288. b.gI1.convertTo(b.t1, CV_32F, stream);
  289. b.gI2.convertTo(b.t2, CV_32F, stream);
  290. cuda::split(b.t1, b.vI1, stream);
  291. cuda::split(b.t2, b.vI2, stream);
  292. Scalar mssim;
  293. Ptr<cuda::Filter> gauss = cuda::createGaussianFilter(b.vI1[0].type(), -1, Size(11, 11), 1.5);
  294. for( int i = 0; i < b.gI1.channels(); ++i )
  295. {
  296. cuda::multiply(b.vI2[i], b.vI2[i], b.I2_2, 1, -1, stream); // I2^2
  297. cuda::multiply(b.vI1[i], b.vI1[i], b.I1_2, 1, -1, stream); // I1^2
  298. cuda::multiply(b.vI1[i], b.vI2[i], b.I1_I2, 1, -1, stream); // I1 * I2
  299. gauss->apply(b.vI1[i], b.mu1, stream);
  300. gauss->apply(b.vI2[i], b.mu2, stream);
  301. cuda::multiply(b.mu1, b.mu1, b.mu1_2, 1, -1, stream);
  302. cuda::multiply(b.mu2, b.mu2, b.mu2_2, 1, -1, stream);
  303. cuda::multiply(b.mu1, b.mu2, b.mu1_mu2, 1, -1, stream);
  304. gauss->apply(b.I1_2, b.sigma1_2, stream);
  305. cuda::subtract(b.sigma1_2, b.mu1_2, b.sigma1_2, cuda::GpuMat(), -1, stream);
  306. //b.sigma1_2 -= b.mu1_2; - This would result in an extra data transfer operation
  307. gauss->apply(b.I2_2, b.sigma2_2, stream);
  308. cuda::subtract(b.sigma2_2, b.mu2_2, b.sigma2_2, cuda::GpuMat(), -1, stream);
  309. //b.sigma2_2 -= b.mu2_2;
  310. gauss->apply(b.I1_I2, b.sigma12, stream);
  311. cuda::subtract(b.sigma12, b.mu1_mu2, b.sigma12, cuda::GpuMat(), -1, stream);
  312. //b.sigma12 -= b.mu1_mu2;
  313. //here too it would be an extra data transfer due to call of operator*(Scalar, Mat)
  314. cuda::multiply(b.mu1_mu2, 2, b.t1, 1, -1, stream); //b.t1 = 2 * b.mu1_mu2 + C1;
  315. cuda::add(b.t1, C1, b.t1, cuda::GpuMat(), -1, stream);
  316. cuda::multiply(b.sigma12, 2, b.t2, 1, -1, stream); //b.t2 = 2 * b.sigma12 + C2;
  317. cuda::add(b.t2, C2, b.t2, cuda::GpuMat(), -12, stream);
  318. cuda::multiply(b.t1, b.t2, b.t3, 1, -1, stream); // t3 = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))
  319. cuda::add(b.mu1_2, b.mu2_2, b.t1, cuda::GpuMat(), -1, stream);
  320. cuda::add(b.t1, C1, b.t1, cuda::GpuMat(), -1, stream);
  321. cuda::add(b.sigma1_2, b.sigma2_2, b.t2, cuda::GpuMat(), -1, stream);
  322. cuda::add(b.t2, C2, b.t2, cuda::GpuMat(), -1, stream);
  323. cuda::multiply(b.t1, b.t2, b.t1, 1, -1, stream); // t1 =((mu1_2 + mu2_2 + C1).*(sigma1_2 + sigma2_2 + C2))
  324. cuda::divide(b.t3, b.t1, b.ssim_map, 1, -1, stream); // ssim_map = t3./t1;
  325. stream.waitForCompletion();
  326. Scalar s = cuda::sum(b.ssim_map, b.buf);
  327. mssim.val[i] = s.val[0] / (b.ssim_map.rows * b.ssim_map.cols);
  328. }
  329. return mssim;
  330. }
  331. //! [getssimopt]