B2B网络软件

标题: AIWROK软件图像二值化的各种方法和应用场景 [打印本页]

作者: YYPOST群发软件    时间: 5 小时前
标题: AIWROK软件图像二值化的各种方法和应用场景
AIWROK软件图像二值化的各种方法和应用场景
AIWROK软件图像二值化的各种方法和应用场景 B2B网络软件

AIWROK软件图像二值化的各种方法和应用场景 B2B网络软件

AIWROK软件图像二值化的各种方法和应用场景 B2B网络软件

  1. //🍎交流 QQ 群 711841924 群一,苹果内测群,528816639
  2. //适用本文档ES5系统安卓 JavaScript引擎Rhino

  3. /**
  4. * 图片二值化示例(简化版)
  5. * 功能说明:展示在AIWROK软件中使用OpenCV进行图像二值化的各种方法和应用场景
  6. * 二值化原理:将图像转换为只有黑和白两种颜色的过程,常用于图像预处理、特征提取等
  7. */

  8. /**
  9. * 基本二值化示例
  10. * @param {number} lowThreshold 低阈值,低于此值的像素转为黑色
  11. * @param {number} highThreshold 高阈值,高于此值的像素转为白色
  12. * @returns {Object} 包含二值化结果的对象
  13. */
  14. function basicThresholdDemo(lowThreshold, highThreshold) {
  15.     printl("执行基本二值化示例...");
  16.    
  17.     // 设置默认阈值
  18.     lowThreshold = lowThreshold || 50;
  19.     highThreshold = highThreshold || 150;
  20.    
  21.     // 获取屏幕尺寸
  22.     var screenWidth = typeof screen !== 'undefined' && typeof screen.getScreenWidth === 'function' ?
  23.                      screen.getScreenWidth() : 1080;
  24.     var screenHeight = typeof screen !== 'undefined' && typeof screen.getScreenHeight === 'function' ?
  25.                       screen.getScreenHeight() : 1920;
  26.    
  27.     // 截图并获取Mat对象
  28.     var screenshot = screen.screenShotFull();
  29.     var mat = screenshot.getMat();
  30.    
  31.     if (!mat) {
  32.         return { error: true, message: "无法获取图像Mat对象" };
  33.     }
  34.    
  35.     printl("原图大小: " + screenshot.getWidth() + "x" + screenshot.getHeight());
  36.     printl("使用阈值: " + lowThreshold + " - " + highThreshold);
  37.    
  38.     // 执行二值化操作
  39.     opencv.threshold(mat, lowThreshold, highThreshold);
  40.    
  41.     // 输出结果信息
  42.     printl("二值化后Mat信息:");
  43.     printl(mat);
  44.    
  45.     printl("基本二值化完成");
  46.    
  47.     return {
  48.         error: false,
  49.         mat: mat,
  50.         message: "基本二值化成功"
  51.     };
  52. }

  53. /**
  54. * 区域二值化示例
  55. * @param {number} x 区域起始x坐标
  56. * @param {number} y 区域起始y坐标
  57. * @param {number} width 区域宽度
  58. * @param {number} height 区域高度
  59. * @param {number} threshold 二值化阈值
  60. * @returns {Object} 包含二值化结果的对象
  61. */
  62. function regionThresholdDemo(x, y, width, height, threshold) {
  63.     printl("执行区域二值化示例...");
  64.    
  65.     // 获取屏幕尺寸
  66.     var screenWidth = typeof screen !== 'undefined' && typeof screen.getScreenWidth === 'function' ?
  67.                      screen.getScreenWidth() : 1080;
  68.     var screenHeight = typeof screen !== 'undefined' && typeof screen.getScreenHeight === 'function' ?
  69.                       screen.getScreenHeight() : 1920;
  70.    
  71.     // 默认值设置
  72.     x = x || 0;
  73.     y = y || 0;
  74.     width = width || Math.floor(screenWidth / 2);
  75.     height = height || Math.floor(screenHeight / 2);
  76.     threshold = threshold || 100;
  77.    
  78.     // 确保区域参数在有效范围内
  79.     x = Math.max(0, x);
  80.     y = Math.max(0, y);
  81.     width = Math.min(screenWidth - x, width);
  82.     height = Math.min(screenHeight - y, height);
  83.    
  84.     printl("区域大小: " + width + "x" + height);
  85.     printl("区域位置: (" + x + ", " + y + ")");
  86.     printl("使用阈值: " + threshold);
  87.    
  88.     // 截图
  89.     var fullScreenshot = screen.screenShotFull();
  90.     var fullMat = fullScreenshot.getMat();
  91.    
  92.     if (!fullMat) {
  93.         return { error: true, message: "无法获取图像Mat对象" };
  94.     }
  95.    
  96.     // 使用submat方法提取区域而不是extractRegion
  97.     var mat = fullMat.submat(y, y + height, x, x + width);
  98.    
  99.     if (!mat) {
  100.         return { error: true, message: "无法获取区域图像Mat对象" };
  101.     }
  102.    
  103.     // 输出提取的区域信息
  104.     printl("提取区域后Mat信息:");
  105.     printl(mat);
  106.    
  107.     // 执行二值化操作
  108.     opencv.threshold(mat, threshold, 255);
  109.    
  110.     // 输出二值化后的结果
  111.     printl("区域二值化后Mat信息:");
  112.     printl(mat);
  113.    
  114.     printl("区域二值化完成");
  115.    
  116.     return {
  117.         error: false,
  118.         mat: mat,
  119.         message: "区域二值化成功"
  120.     };
  121. }

  122. /**
  123. * 多阈值对比二值化示例
  124. * 展示不同阈值对二值化结果的影响
  125. */
  126. function multiThresholdComparison() {
  127.     printl("执行多阈值对比二值化示例...");
  128.    
  129.     // 获取屏幕尺寸
  130.     var screenWidth = typeof screen !== 'undefined' && typeof screen.getScreenWidth === 'function' ?
  131.                      screen.getScreenWidth() : 1080;
  132.     var screenHeight = typeof screen !== 'undefined' && typeof screen.getScreenHeight === 'function' ?
  133.                       screen.getScreenHeight() : 1920;
  134.    
  135.     // 截图并获取原始Mat对象
  136.     var screenshot = screen.screenShotFull();
  137.     var originalMat = screenshot.getMat();
  138.    
  139.     if (!originalMat) {
  140.         printl("无法获取图像Mat对象");
  141.         return false;
  142.     }
  143.    
  144.     // 定义要测试的阈值组合
  145.     var thresholdSets = [
  146.         { low: 30, high: 100, name: "低阈值" },
  147.         { low: 80, high: 180, name: "中等阈值" },
  148.         { low: 120, high: 220, name: "高阈值" }
  149.     ];
  150.    
  151.     // 对每个阈值组合执行二值化
  152.     for (var i = 0; i < thresholdSets.length; i++) {
  153.         var set = thresholdSets[i];
  154.         printl("\n测试" + set.name + ": 低=" + set.low + ", 高=" + set.high);
  155.         
  156.         // 创建原始图像的副本以避免修改原图
  157.         var matCopy = originalMat.clone();
  158.         
  159.         // 执行二值化
  160.         opencv.threshold(matCopy, set.low, set.high);
  161.         
  162.         // 输出结果信息
  163.         printl(set.name + "二值化后Mat信息:");
  164.         printl(matCopy);
  165.         
  166.         // 可以在这里将结果保存或显示
  167.         printl(set.name + "二值化完成");
  168.     }
  169.    
  170.     printl("\n多阈值对比完成");
  171.     return true;
  172. }

  173. /**
  174. * 灰度转换后二值化示例
  175. * 先将图像转为灰度,再进行二值化处理
  176. */
  177. function grayThenThresholdDemo() {
  178.     printl("执行灰度转换后二值化示例...");
  179.    
  180.     // 获取屏幕尺寸
  181.     var screenWidth = typeof screen !== 'undefined' && typeof screen.getScreenWidth === 'function' ?
  182.                      screen.getScreenWidth() : 1080;
  183.     var screenHeight = typeof screen !== 'undefined' && typeof screen.getScreenHeight === 'function' ?
  184.                       screen.getScreenHeight() : 1920;
  185.    
  186.     // 截图
  187.     var screenshot = screen.screenShotFull();
  188.     var mat = screenshot.getMat();
  189.    
  190.     if (!mat) {
  191.         return { error: true, message: "无法获取图像Mat对象" };
  192.     }
  193.    
  194.     // 先转换为灰度图
  195.     printl("转换为灰度图...");
  196.     opencv.toGray(mat);
  197.    
  198.     // 输出灰度图信息
  199.     printl("灰度图Mat信息:");
  200.     printl(mat);
  201.    
  202.     // 再进行二值化
  203.     printl("执行二值化...");
  204.     opencv.threshold(mat, 80, 200);
  205.    
  206.     // 输出二值化后的结果
  207.     printl("灰度后二值化Mat信息:");
  208.     printl(mat);
  209.    
  210.     printl("灰度后二值化完成");
  211.    
  212.     return {
  213.         error: false,
  214.         mat: mat,
  215.         message: "灰度转换后二值化成功"
  216.     };
  217. }

  218. /**
  219. * 应用场景示例:二值化后进行轮廓检测
  220. * @param {number} threshold 二值化阈值
  221. */
  222. function thresholdForContourDetection(threshold) {
  223.     printl("执行二值化后轮廓检测示例...");
  224.    
  225.     threshold = threshold || 100;
  226.    
  227.     // 获取屏幕尺寸
  228.     var screenWidth = typeof screen !== 'undefined' && typeof screen.getScreenWidth === 'function' ?
  229.                      screen.getScreenWidth() : 1080;
  230.     var screenHeight = typeof screen !== 'undefined' && typeof screen.getScreenHeight === 'function' ?
  231.                       screen.getScreenHeight() : 1920;
  232.    
  233.     // 截图并转换为灰度
  234.     var screenshot = screen.screenShotFull();
  235.     var mat = screenshot.getMat();
  236.    
  237.     if (!mat) {
  238.         printl("无法获取图像Mat对象");
  239.         return false;
  240.     }
  241.    
  242.     // 转换为灰度图
  243.     opencv.toGray(mat);
  244.    
  245.     // 输出灰度图信息
  246.     printl("灰度图Mat信息:");
  247.     printl(mat);
  248.    
  249.     // 二值化
  250.     opencv.threshold(mat, threshold, 255);
  251.    
  252.     // 输出二值化后的结果
  253.     printl("二值化后Mat信息:");
  254.     printl(mat);
  255.    
  256.     // 进行轮廓检测
  257.     printl("进行轮廓检测...");
  258.     var contours = opencv.getContours(mat);
  259.    
  260.     if (contours && contours.length > 0) {
  261.         printl("找到 " + contours.length + " 个轮廓");
  262.         
  263.         // 可以在这里对轮廓进行进一步处理
  264.         for (var i = 0; i < Math.min(5, contours.length); i++) { // 只显示前5个轮廓信息
  265.             var contour = contours[i];
  266.             printl("轮廓 " + (i+1) + ": 位置(" + contour.x + "," + contour.y + "), 大小 " + contour.width + "x" + contour.height);
  267.         }
  268.     } else {
  269.         printl("未找到轮廓");
  270.     }
  271.    
  272.     printl("轮廓检测示例完成");
  273.     return true;
  274. }

  275. /**
  276. * 应用场景示例:二值化后进行OCR识别
  277. * 对文字区域进行二值化以提高OCR识别率
  278. * @param {number} x 文字区域x坐标
  279. * @param {number} y 文字区域y坐标
  280. * @param {number} width 文字区域宽度
  281. * @param {number} height 文字区域高度
  282. */
  283. function thresholdForOCRDemo(x, y, width, height) {
  284.     printl("执行二值化后OCR识别示例...");
  285.    
  286.     // 获取屏幕尺寸
  287.     var screenWidth = typeof screen !== 'undefined' && typeof screen.getScreenWidth === 'function' ?
  288.                      screen.getScreenWidth() : 1080;
  289.     var screenHeight = typeof screen !== 'undefined' && typeof screen.getScreenHeight === 'function' ?
  290.                       screen.getScreenHeight() : 1920;
  291.    
  292.     // 默认值设置 - 使用屏幕中心区域
  293.     x = x || Math.floor(screenWidth / 4);
  294.     y = y || Math.floor(screenHeight / 4);
  295.     width = width || Math.floor(screenWidth / 2);
  296.     height = height || Math.floor(screenHeight / 4);
  297.    
  298.     // 确保区域参数在有效范围内
  299.     x = Math.max(0, x);
  300.     y = Math.max(0, y);
  301.     width = Math.min(screenWidth - x, width);
  302.     height = Math.min(screenHeight - y, height);
  303.    
  304.     printl("文字区域: (" + x + "," + y + ") " + width + "x" + height);
  305.    
  306.     // 截图
  307.     var fullScreenshot = screen.screenShotFull();
  308.     var fullMat = fullScreenshot.getMat();
  309.    
  310.     if (!fullMat) {
  311.         printl("无法获取图像Mat对象");
  312.         return false;
  313.     }
  314.    
  315.     // 使用submat方法提取区域
  316.     var mat = fullMat.submat(y, y + height, x, x + width);
  317.    
  318.     if (!mat) {
  319.         printl("无法获取文字区域图像");
  320.         return false;
  321.     }
  322.    
  323.     printl("提取的文字区域Mat信息:");
  324.     printl(mat);
  325.    
  326.     printl("文字区域: (" + x + "," + y + ") " + width + "x" + height);
  327.    
  328.     // 预处理:转换为灰度
  329.     opencv.toGray(mat);
  330.    
  331.     // 输出灰度图信息
  332.     printl("文字区域灰度图信息:");
  333.     printl(mat);
  334.    
  335.     // 二值化 - 文字识别通常需要较高的阈值来突出文字
  336.     opencv.threshold(mat, 100, 200);
  337.    
  338.     // 输出二值化后的结果
  339.     printl("文字区域二值化后Mat信息:");
  340.     printl(mat);
  341.    
  342.     printl("二值化预处理完成,准备OCR识别");
  343.    
  344.     // 注意:实际使用中,这里应该调用AIWROK的OCR功能
  345.     printl("提示:在实际应用中,这里应调用ocr.recognize()或其他OCR方法");
  346.    
  347.     return true;
  348. }

  349. /**
  350. * 自适应阈值二值化模拟
  351. * 模拟自适应阈值的效果,通过对图像不同区域使用不同阈值
  352. * @param {number} blockSize 块大小
  353. */
  354. function adaptiveThresholdSimulation(blockSize) {
  355.     printl("执行自适应阈值二值化模拟...");
  356.    
  357.     blockSize = blockSize || 4; // 将图像分成blockSize x blockSize块
  358.    
  359.     // 获取屏幕尺寸
  360.     var screenWidth = typeof screen !== 'undefined' && typeof screen.getScreenWidth === 'function' ?
  361.                      screen.getScreenWidth() : 1080;
  362.     var screenHeight = typeof screen !== 'undefined' && typeof screen.getScreenHeight === 'function' ?
  363.                       screen.getScreenHeight() : 1920;
  364.    
  365.     var screenshot = screen.screenShotFull();
  366.     var mat = screenshot.getMat();
  367.    
  368.     if (!mat) {
  369.         printl("无法获取图像Mat对象");
  370.         return false;
  371.     }
  372.    
  373.     var imgWidth = screenshot.getWidth();
  374.     var imgHeight = screenshot.getHeight();
  375.    
  376.     // 计算每个块的大小
  377.     var blockW = Math.floor(imgWidth / blockSize);
  378.     var blockH = Math.floor(imgHeight / blockSize);
  379.    
  380.     printl("图像分割为 " + blockSize + "x" + blockSize + " 块,每块大小约 " + blockW + "x" + blockH);
  381.    
  382.     // 转换为灰度图
  383.     opencv.toGray(mat);
  384.    
  385.     // 输出灰度图信息
  386.     printl("整体灰度图信息:");
  387.     printl(mat);
  388.    
  389.     // 对每个块使用不同的阈值(这里使用简单的随机阈值模拟自适应效果)
  390.     for (var i = 0; i < blockSize; i++) {
  391.         for (var j = 0; j < blockSize; j++) {
  392.             // 计算块的坐标
  393.             var blockX = i * blockW;
  394.             var blockY = j * blockH;
  395.             var currentBlockW = (i === blockSize - 1) ? imgWidth - blockX : blockW;
  396.             var currentBlockH = (j === blockSize - 1) ? imgHeight - blockY : blockH;
  397.             
  398.             // 确保区域参数有效
  399.             currentBlockW = Math.min(currentBlockW, imgWidth - blockX);
  400.             currentBlockH = Math.min(currentBlockH, imgHeight - blockY);
  401.             
  402.             // 为每个块生成一个随机阈值(在一定范围内)
  403.             var blockThreshold = 80 + Math.floor(Math.random() * 60);
  404.             
  405.             // 使用submat方法提取区域
  406.             if (blockY + currentBlockH <= imgHeight && blockX + currentBlockW <= imgWidth) {
  407.                 var blockMat = mat.submat(blockY, blockY + currentBlockH, blockX, blockX + currentBlockW);
  408.                
  409.                 // 输出块信息
  410.                 printl("处理块(" + i + "," + j + "),坐标:(" + blockX + "," + blockY + "),大小:" + currentBlockW + "x" + currentBlockH);
  411.                
  412.                 // 对块进行二值化
  413.                 opencv.threshold(blockMat, blockThreshold, 200);
  414.                
  415.                 // 输出块二值化后的结果
  416.                 printl("块(" + i + "," + j + ")二值化后:");
  417.                 printl(blockMat);
  418.             }
  419.         }
  420.     }
  421.    
  422.     printl("自适应阈值模拟完成,最终Mat信息:");
  423.     printl(mat);
  424.    
  425.     return true;
  426. }

  427. /**
  428. * 主函数:运行所有二值化示例
  429. */
  430. function runAllThresholdDemos() {
  431.     printl("==================================================");
  432.     printl("                图片二值化示例集");
  433.     printl("==================================================\n");
  434.    
  435.     // 检查环境
  436.     if (typeof opencv === 'undefined') {
  437.         printl("错误: 未找到OpenCV模块,请确保在AIWROK环境中运行");
  438.         return;
  439.     }
  440.    
  441.     // 获取屏幕尺寸
  442.     var screenWidth = typeof screen !== 'undefined' && typeof screen.getScreenWidth === 'function' ?
  443.                      screen.getScreenWidth() : 1080;
  444.     var screenHeight = typeof screen !== 'undefined' && typeof screen.getScreenHeight === 'function' ?
  445.                       screen.getScreenHeight() : 1920;
  446.    
  447.     printl("当前屏幕尺寸: " + screenWidth + "x" + screenHeight);
  448.    
  449.     // 1. 基本二值化示例
  450.     printl("1. 基本二值化示例");
  451.     basicThresholdDemo(50, 150);
  452.     printl("----------------------------------------\n");
  453.    
  454.     // 2. 区域二值化示例
  455.     printl("2. 区域二值化示例");
  456.     // 使用屏幕中心的一个区域
  457.     var regionX = Math.floor(screenWidth / 4);
  458.     var regionY = Math.floor(screenHeight / 4);
  459.     var regionWidth = Math.floor(screenWidth / 2);
  460.     var regionHeight = Math.floor(screenHeight / 3);
  461.     regionThresholdDemo(regionX, regionY, regionWidth, regionHeight, 100);
  462.     printl("----------------------------------------\n");
  463.    
  464.     safeSleep(1000); // 添加延迟,避免连续操作过快
  465.    
  466.     // 3. 多阈值对比
  467.     printl("3. 多阈值对比示例");
  468.     multiThresholdComparison();
  469.     printl("----------------------------------------\n");
  470.    
  471.     // 4. 灰度后二值化
  472.     printl("4. 灰度转换后二值化示例");
  473.     grayThenThresholdDemo();
  474.     printl("----------------------------------------\n");
  475.    
  476.     safeSleep(1000); // 添加延迟
  477.    
  478.     // 5. 二值化后轮廓检测
  479.     printl("5. 二值化后轮廓检测示例");
  480.     thresholdForContourDetection(100);
  481.     printl("----------------------------------------\n");
  482.    
  483.     safeSleep(1000); // 添加延迟
  484.    
  485.     // 6. 二值化后OCR示例
  486.     printl("6. 二值化后OCR识别示例");
  487.     thresholdForOCRDemo(Math.floor(screenWidth / 4), Math.floor(screenHeight / 3), Math.floor(screenWidth / 2), Math.floor(screenHeight / 4));
  488.     printl("----------------------------------------\n");
  489.    
  490.     safeSleep(1000); // 添加延迟
  491.    
  492.     // 7. 自适应阈值模拟
  493.     printl("7. 自适应阈值二值化模拟");
  494.     adaptiveThresholdSimulation(4);
  495.     printl("----------------------------------------\n");
  496.    
  497.     printl("所有二值化示例执行完成!");
  498. }

  499. /**
  500. * 工具函数:安全延迟
  501. * @param {number} ms 延迟毫秒数
  502. */
  503. function safeSleep(ms) {
  504.     if (typeof sleep !== 'undefined' && sleep.millisecond) {
  505.         sleep.millisecond(ms);
  506.     } else if (typeof java !== 'undefined') {
  507.         java.lang.Thread.sleep(ms);
  508.     }
  509. }

  510. /**
  511. * 二值化工具类 - 提供便捷的二值化方法
  512. */
  513. var ThresholdUtils = {
  514.     /**
  515.      * 快速二值化图像
  516.      * @param {Object} options 配置选项
  517.      * @returns {Object} 二值化结果
  518.      */
  519.     quickThreshold: function(options) {
  520.         options = options || {};
  521.         
  522.         // 获取屏幕尺寸
  523.         var screenWidth = typeof screen !== 'undefined' && typeof screen.getScreenWidth === 'function' ?
  524.                          screen.getScreenWidth() : 1080;
  525.         var screenHeight = typeof screen !== 'undefined' && typeof screen.getScreenHeight === 'function' ?
  526.                           screen.getScreenHeight() : 1920;
  527.         
  528.         var screenshot = options.image || screen.screenShotFull();
  529.         var mat = screenshot.getMat();
  530.         
  531.         if (options.toGray !== false) {
  532.             opencv.toGray(mat);
  533.         }
  534.         
  535.         opencv.threshold(
  536.             mat,
  537.             options.lowThreshold || 80,
  538.             options.highThreshold || 200
  539.         );
  540.         
  541.         // 输出结果信息
  542.         printl("快速二值化结果:");
  543.         printl(mat);
  544.         
  545.         return {
  546.             success: true,
  547.             mat: mat
  548.         };
  549.     },
  550.    
  551.     /**
  552.      * 处理文字图像的二值化参数
  553.      * @returns {Object} 适合文字的二值化参数
  554.      */
  555.     getTextThresholdParams: function() {
  556.         return {
  557.             lowThreshold: 100,
  558.             highThreshold: 220,
  559.             toGray: true
  560.         };
  561.     },
  562.    
  563.     /**
  564.      * 处理图像边缘的二值化参数
  565.      * @returns {Object} 适合边缘检测的二值化参数
  566.      */
  567.     getEdgeThresholdParams: function() {
  568.         return {
  569.             lowThreshold: 50,
  570.             highThreshold: 150,
  571.             toGray: true
  572.         };
  573.     }
  574. };

  575. // 执行所有二值化示例
  576. runAllThresholdDemos();
复制代码









欢迎光临 B2B网络软件 (http://bbs.niubt.cn/) Powered by Discuz! X3.2