perf_sobel.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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.combine = HelpFunc.combine;
  17. global.log = HelpFunc.log;
  18. global.decodeParams2Case = HelpFunc.decodeParams2Case;
  19. global.setBenchmarkSuite = HelpFunc.setBenchmarkSuite;
  20. global.addKernelCase = HelpFunc.addKernelCase;
  21. global.cvSize = Base.getCvSize();
  22. } else {
  23. enableButton();
  24. cvSize = getCvSize();
  25. }
  26. let totalCaseNum, currentCaseId;
  27. const SobelSize = [cvSize.szODD, cvSize.szQVGA, cvSize.szVGA];
  28. const Sobel3x3dxdy = ["(0,1)", "(1,0)", "(1,1)", "(0,2)", "(2,0)", "(2,2)"];
  29. const Sobeldxdy = ["(0,1)", "(1,0)", "(1,1)", "(0,2)", "(2,0)"];
  30. const BorderType3x3 = ["BORDER_REPLICATE", "BORDER_CONSTANT"];
  31. const BorderType3x3ROI = ["BORDER_DEFAULT", "BORDER_REPLICATE|BORDER_ISOLATED", "BORDER_CONSTANT|BORDER_ISOLATED"];
  32. const BorderType = ["BORDER_REPLICATE", "BORDER_CONSTANT", "BORDER_REFLECT", "BORDER_REFLECT101"];
  33. const BorderTypeROI = ["BORDER_DEFAULT", "BORDER_REPLICATE|BORDER_ISOLATED", "BORDER_CONSTANT|BORDER_ISOLATED", "BORDER_REFLECT|BORDER_ISOLATED", "BORDER_REFLECT101|BORDER_ISOLATED"]
  34. const combiSobelBorder3x3 = combine(SobelSize, ["CV_16SC1", "CV_32FC1"], Sobel3x3dxdy, BorderType3x3);
  35. const combiSobelBorder3x3ROI = combine(SobelSize, ["CV_16SC1", "CV_32FC1"], Sobel3x3dxdy, BorderType3x3ROI);
  36. const combiSobelBorder5x5 = combine(SobelSize, ["CV_16SC1", "CV_32FC1"], Sobeldxdy, BorderType);
  37. const combiSobelBorder5x5ROI = combine(SobelSize, ["CV_16SC1", "CV_32FC1"], Sobeldxdy, BorderTypeROI);
  38. function addSobelCase(suite, type) {
  39. suite.add('sobel', function() {
  40. cv.Sobel(src, dst, ddepth, dx, dy, ksize, 1, 0, borderType);
  41. }, {
  42. 'setup': function() {
  43. let size = this.params.size;
  44. let ddepth = cv[this.params.ddepth];
  45. let dxdy = this.params.dxdy;
  46. let ksize = this.params.ksize;
  47. let type = this.params.type;
  48. let src, dst;
  49. if (type %2 == 0) {
  50. src = new cv.Mat(size[1], size[0], cv.CV_8U);
  51. dst = new cv.Mat(size[1], size[0], ddepth);
  52. } else {
  53. src = new cv.Mat(size[1]+10, size[0]+10, cv.CV_8U);
  54. dst = new cv.Mat(size[1]+10, size[0]+10, ddepth);
  55. src = src.colRange(5, size[0]+5);
  56. src = src.rowRange(5, size[1]+5);
  57. dst = dst.colRange(5, size[0]+5);
  58. dst = dst.rowRange(5, size[1]+5);
  59. }
  60. let dx = parseInt(dxdy[1]);
  61. let dy = parseInt(dxdy[3]);
  62. let borderTypeArray = this.params.borderType;
  63. let borderType;
  64. if (borderTypeArray.length == 1) {
  65. borderType = cv[borderTypeArray[0]];
  66. } else {
  67. borderType = cv[borderTypeArray[0]] | cv[borderTypeArray[1]];
  68. }
  69. },
  70. 'teardown': function() {
  71. src.delete();
  72. dst.delete();
  73. }
  74. });
  75. }
  76. function addSobelModeCase(suite, combination, type) {
  77. totalCaseNum += combination.length;
  78. for (let i = 0; i < combination.length; ++i) {
  79. let size = combination[i][0];
  80. let ddepth = combination[i][1];
  81. let dxdy = combination[i][2];
  82. let borderType = combination[i][3];
  83. let sizeArray = [size.width, size.height];
  84. let ksize;
  85. if (type < 2) {
  86. ksize = 3;
  87. } else {
  88. ksize = 5;
  89. }
  90. let borderTypeArray = borderType.split("|");
  91. let params = {size: sizeArray, ddepth: ddepth, dxdy: dxdy, ksize:ksize, borderType:borderTypeArray, type:type};
  92. addKernelCase(suite, params, type, addSobelCase);
  93. }
  94. }
  95. function genBenchmarkCase(paramsContent) {
  96. let suite = new Benchmark.Suite;
  97. totalCaseNum = 0;
  98. currentCaseId = 0;
  99. let params = "";
  100. let paramObjs = [];
  101. paramObjs.push({name:"size", value:"", reg:[""], index:0});
  102. paramObjs.push({name:"ddepth", value:"", reg:["/CV\_[0-9]+[FSUfsu]C1/g"], index:1});
  103. paramObjs.push({name:"dxdy", value:"", reg:["/\\([0-2],[0-2]\\)/"], index:2});
  104. if (/\([0-9]+x[0-9]+,[\ ]*\w+,[\ ]*\([0-2],[0-2]\),[\ ]*\w+\)/g.test(paramsContent.toString())) {
  105. params = paramsContent.toString().match(/\([0-9]+x[0-9]+,[\ ]*\w+,[\ ]*\([0-2],[0-2]\),[\ ]*\w+\)/g)[0];
  106. paramObjs.push({name:"boderType", value:"", reg:["/BORDER\_\\w+/"], index:3});
  107. } else if (/\([0-9]+x[0-9]+,[\ ]*\w+,[\ ]*\([0-2],[0-2]\),[\ ]*\w+\|\w+\)/g.test(paramsContent.toString())) {
  108. params = paramsContent.toString().match(/\([0-9]+x[0-9]+,[\ ]*\w+,[\ ]*\([0-2],[0-2]\),[\ ]*\w+\|\w+\)/g)[0];
  109. paramObjs.push({name:"boderType", value:"", reg:["/BORDER\_\\w+\\|BORDER\_\\w+/"], index:3});
  110. }
  111. if (params != ""){
  112. let locationList = decodeParams2Case(params, paramObjs,sobelCombinations);
  113. for (let i = 0; i < locationList.length; i++){
  114. let first = locationList[i][0];
  115. let second = locationList[i][1];
  116. addSobelModeCase(suite, [sobelCombinations[first][second]], first);
  117. }
  118. } else {
  119. log("no filter or getting invalid params, run all the cases");
  120. addSobelModeCase(suite, combiSobelBorder3x3, 0);
  121. addSobelModeCase(suite, combiSobelBorder3x3ROI, 1);
  122. addSobelModeCase(suite, combiSobelBorder5x5, 2);
  123. addSobelModeCase(suite, combiSobelBorder5x5ROI, 3);
  124. }
  125. setBenchmarkSuite(suite, "sobel", currentCaseId);
  126. log(`Running ${totalCaseNum} tests from Sobel`);
  127. suite.run({ 'async': true }); // run the benchmark
  128. }
  129. let sobelCombinations = [combiSobelBorder3x3, combiSobelBorder3x3ROI, combiSobelBorder5x5, combiSobelBorder5x5ROI];
  130. if (isNodeJs) {
  131. const args = process.argv.slice(2);
  132. let paramsContent = '';
  133. if (/--test_param_filter=\([0-9]+x[0-9]+,[\ ]*\w+,[\ ]*\([0-2],[0-2]\),[\ ]*\w+\)/g.test(args.toString())) {
  134. paramsContent = args.toString().match(/\([0-9]+x[0-9]+,[\ ]*\w+,[\ ]*\([0-2],[0-2]\),[\ ]*\w+\)/g)[0];
  135. } else if (/--test_param_filter=\([0-9]+x[0-9]+,[\ ]*\w+,[\ ]*\([0-2],[0-2]\),[\ ]*\w+\|\w+\)/g.test(args.toString())) {
  136. paramsContent = args.toString().match(/\([0-9]+x[0-9]+,[\ ]*\w+,[\ ]*\([0-2],[0-2]\),[\ ]*\w+\|\w+\)/g)[0];
  137. }
  138. genBenchmarkCase(paramsContent);
  139. } else {
  140. runButton.onclick = function() {
  141. let paramsContent = paramsElement.value;
  142. genBenchmarkCase(paramsContent);
  143. if (totalCaseNum !== 0) {
  144. disableButton();
  145. }
  146. }
  147. }
  148. };
  149. async function main() {
  150. if (cv instanceof Promise) {
  151. cv = await cv;
  152. perf();
  153. } else {
  154. cv.onRuntimeInitialized = perf;
  155. }
  156. }
  157. main();