perf_warpPerspective.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. const isNodeJs = (typeof window) === 'undefined'? true : false;
  2. if (isNodeJs) {
  3. var Benchmark = require('benchmark');
  4. var cv = require('../../opencv');
  5. var HelpFunc = require('../perf_helpfunc');
  6. var Base = require('../base');
  7. } else {
  8. var paramsElement = document.getElementById('params');
  9. var runButton = document.getElementById('runButton');
  10. var logElement = document.getElementById('log');
  11. }
  12. function perf() {
  13. console.log('opencv.js loaded');
  14. if (isNodeJs) {
  15. global.cv = cv;
  16. global.fillGradient = HelpFunc.fillGradient;
  17. global.smoothBorder = HelpFunc.smoothBorder;
  18. global.combine = HelpFunc.combine;
  19. global.log = HelpFunc.log;
  20. global.decodeParams2Case = HelpFunc.decodeParams2Case;
  21. global.setBenchmarkSuite = HelpFunc.setBenchmarkSuite;
  22. global.addKernelCase = HelpFunc.addKernelCase
  23. global.cvSize = Base.getCvSize();
  24. } else {
  25. enableButton();
  26. cvSize = getCvSize();
  27. }
  28. let totalCaseNum, currentCaseId;
  29. const WarpPersSize = [cvSize.szVGA, cvSize.sz720p, cvSize.sz1080p];
  30. const InterType = ["INTER_NEAREST", "INTER_LINEAR"];
  31. const BorderMode = ["BORDER_CONSTANT", "BORDER_REPLICATE"]
  32. const combiWarpPers = combine(WarpPersSize, InterType, BorderMode);
  33. function addWarpPerspectiveCase(suite, type) {
  34. suite.add('warpPerspective', function() {
  35. cv.warpPerspective(src, dst, warpMat, sz, interType, borderMode, borderColor);
  36. }, {
  37. 'setup': function() {
  38. let sz = this.params.size;
  39. let interType = cv[this.params.interType];
  40. let borderMode = cv[this.params.borderMode];
  41. let srcSize = new cv.Size(512, 512);
  42. let borderColor = new cv.Scalar.all(150);
  43. let src = new cv.Mat(srcSize, cv.CV_8UC4);
  44. let dst = new cv.Mat(sz, cv.CV_8UC4);
  45. fillGradient(cv, src);
  46. if (borderMode == cv.BORDER_CONSTANT) {
  47. smoothBorder(cv, src, borderMode, 1);
  48. }
  49. let rotMat = cv.getRotationMatrix2D(new cv.Point(src.cols/2.0, src.rows/2.0), 30.0, 2.2);
  50. let warpMat = new cv.Mat(3, 3, cv.CV_64FC1);
  51. for(r=0; r<2; r++) {
  52. for(c=0; c<3; c++) {
  53. view = warpMat.doublePtr(r,c)
  54. view[0] = rotMat.doubleAt(r, c);
  55. }
  56. }
  57. view = warpMat.doublePtr(2,0);
  58. view[0] = 0.3/sz.width;
  59. view = warpMat.doublePtr(2,1);
  60. view[0] = 0.3/sz.height;
  61. view = warpMat.doublePtr(2,2);
  62. view[0] = 1;
  63. },
  64. 'teardown': function() {
  65. src.delete();
  66. dst.delete();
  67. warpMat.delete();
  68. }
  69. });
  70. }
  71. function addWarpPerspectiveModeCase(suite, combination, type) {
  72. totalCaseNum += combination.length;
  73. for (let i = 0; i < combination.length; ++i) {
  74. let size = combination[i][0];
  75. let interType = combination[i][1];
  76. let borderMode = combination[i][2];
  77. let params = {size: size, interType:interType, borderMode:borderMode};
  78. addKernelCase(suite, params, type, addWarpPerspectiveCase);
  79. }
  80. }
  81. function genBenchmarkCase(paramsContent) {
  82. let suite = new Benchmark.Suite;
  83. totalCaseNum = 0;
  84. currentCaseId = 0;
  85. if (/\([0-9]+x[0-9]+,[\ ]*INTER\_\w+,[\ ]*BORDER\_\w+\)/g.test(paramsContent.toString())) {
  86. let params = paramsContent.toString().match(/\([0-9]+x[0-9]+,[\ ]*INTER\_\w+,[\ ]*BORDER\_\w+\)/g)[0];
  87. let paramObjs = [];
  88. paramObjs.push({name:"size", value:"", reg:[""], index:0});
  89. paramObjs.push({name:"interType", value: "", reg:["/INTER\_\\w+/"], index:1});
  90. paramObjs.push({name:"borderMode", value: "", reg:["/BORDER\_\\w+/"], index:2});
  91. let locationList = decodeParams2Case(params, paramObjs, warpPersCombinations);
  92. for (let i = 0; i < locationList.length; i++){
  93. let first = locationList[i][0];
  94. let second = locationList[i][1];
  95. addWarpPerspectiveModeCase(suite, [warpPersCombinations[first][second]], first);
  96. }
  97. } else {
  98. log("no filter or getting invalid params, run all the cases");
  99. addWarpPerspectiveModeCase(suite, combiWarpPers, 0);
  100. }
  101. setBenchmarkSuite(suite, "warpPerspective", currentCaseId);
  102. log(`Running ${totalCaseNum} tests from warpPerspective`);
  103. suite.run({ 'async': true }); // run the benchmark
  104. }
  105. let warpPersCombinations = [combiWarpPers];
  106. if (isNodeJs) {
  107. const args = process.argv.slice(2);
  108. let paramsContent = '';
  109. if (/--test_param_filter=\([0-9]+x[0-9]+,[\ ]*INTER\_\w+,[\ ]*BORDER\_\w+\)/g.test(args.toString())) {
  110. paramsContent = args.toString().match(/\([0-9]+x[0-9]+,[\ ]*INTER\_\w+,[\ ]*BORDER\_\w+\)/g)[0];
  111. }
  112. genBenchmarkCase(paramsContent);
  113. } else {
  114. runButton.onclick = function() {
  115. let paramsContent = paramsElement.value;
  116. genBenchmarkCase(paramsContent);
  117. if (totalCaseNum !== 0) {
  118. disableButton();
  119. }
  120. }
  121. }
  122. };
  123. async function main() {
  124. if (cv instanceof Promise) {
  125. cv = await cv;
  126. perf();
  127. } else {
  128. cv.onRuntimeInitialized = perf;
  129. }
  130. }
  131. main();