 | |  |  | AIWROK软件图像二值化的各种方法和应用场景
- //🍎交流 QQ 群 711841924 群一,苹果内测群,528816639
- //适用本文档ES5系统安卓 JavaScript引擎Rhino
- /**
- * 图片二值化示例(简化版)
- * 功能说明:展示在AIWROK软件中使用OpenCV进行图像二值化的各种方法和应用场景
- * 二值化原理:将图像转换为只有黑和白两种颜色的过程,常用于图像预处理、特征提取等
- */
- /**
- * 基本二值化示例
- * @param {number} lowThreshold 低阈值,低于此值的像素转为黑色
- * @param {number} highThreshold 高阈值,高于此值的像素转为白色
- * @returns {Object} 包含二值化结果的对象
- */
- function basicThresholdDemo(lowThreshold, highThreshold) {
- printl("执行基本二值化示例...");
-
- // 设置默认阈值
- lowThreshold = lowThreshold || 50;
- highThreshold = highThreshold || 150;
-
- // 获取屏幕尺寸
- var screenWidth = typeof screen !== 'undefined' && typeof screen.getScreenWidth === 'function' ?
- screen.getScreenWidth() : 1080;
- var screenHeight = typeof screen !== 'undefined' && typeof screen.getScreenHeight === 'function' ?
- screen.getScreenHeight() : 1920;
-
- // 截图并获取Mat对象
- var screenshot = screen.screenShotFull();
- var mat = screenshot.getMat();
-
- if (!mat) {
- return { error: true, message: "无法获取图像Mat对象" };
- }
-
- printl("原图大小: " + screenshot.getWidth() + "x" + screenshot.getHeight());
- printl("使用阈值: " + lowThreshold + " - " + highThreshold);
-
- // 执行二值化操作
- opencv.threshold(mat, lowThreshold, highThreshold);
-
- // 输出结果信息
- printl("二值化后Mat信息:");
- printl(mat);
-
- printl("基本二值化完成");
-
- return {
- error: false,
- mat: mat,
- message: "基本二值化成功"
- };
- }
- /**
- * 区域二值化示例
- * @param {number} x 区域起始x坐标
- * @param {number} y 区域起始y坐标
- * @param {number} width 区域宽度
- * @param {number} height 区域高度
- * @param {number} threshold 二值化阈值
- * @returns {Object} 包含二值化结果的对象
- */
- function regionThresholdDemo(x, y, width, height, threshold) {
- printl("执行区域二值化示例...");
-
- // 获取屏幕尺寸
- var screenWidth = typeof screen !== 'undefined' && typeof screen.getScreenWidth === 'function' ?
- screen.getScreenWidth() : 1080;
- var screenHeight = typeof screen !== 'undefined' && typeof screen.getScreenHeight === 'function' ?
- screen.getScreenHeight() : 1920;
-
- // 默认值设置
- x = x || 0;
- y = y || 0;
- width = width || Math.floor(screenWidth / 2);
- height = height || Math.floor(screenHeight / 2);
- threshold = threshold || 100;
-
- // 确保区域参数在有效范围内
- x = Math.max(0, x);
- y = Math.max(0, y);
- width = Math.min(screenWidth - x, width);
- height = Math.min(screenHeight - y, height);
-
- printl("区域大小: " + width + "x" + height);
- printl("区域位置: (" + x + ", " + y + ")");
- printl("使用阈值: " + threshold);
-
- // 截图
- var fullScreenshot = screen.screenShotFull();
- var fullMat = fullScreenshot.getMat();
-
- if (!fullMat) {
- return { error: true, message: "无法获取图像Mat对象" };
- }
-
- // 使用submat方法提取区域而不是extractRegion
- var mat = fullMat.submat(y, y + height, x, x + width);
-
- if (!mat) {
- return { error: true, message: "无法获取区域图像Mat对象" };
- }
-
- // 输出提取的区域信息
- printl("提取区域后Mat信息:");
- printl(mat);
-
- // 执行二值化操作
- opencv.threshold(mat, threshold, 255);
-
- // 输出二值化后的结果
- printl("区域二值化后Mat信息:");
- printl(mat);
-
- printl("区域二值化完成");
-
- return {
- error: false,
- mat: mat,
- message: "区域二值化成功"
- };
- }
- /**
- * 多阈值对比二值化示例
- * 展示不同阈值对二值化结果的影响
- */
- function multiThresholdComparison() {
- printl("执行多阈值对比二值化示例...");
-
- // 获取屏幕尺寸
- var screenWidth = typeof screen !== 'undefined' && typeof screen.getScreenWidth === 'function' ?
- screen.getScreenWidth() : 1080;
- var screenHeight = typeof screen !== 'undefined' && typeof screen.getScreenHeight === 'function' ?
- screen.getScreenHeight() : 1920;
-
- // 截图并获取原始Mat对象
- var screenshot = screen.screenShotFull();
- var originalMat = screenshot.getMat();
-
- if (!originalMat) {
- printl("无法获取图像Mat对象");
- return false;
- }
-
- // 定义要测试的阈值组合
- var thresholdSets = [
- { low: 30, high: 100, name: "低阈值" },
- { low: 80, high: 180, name: "中等阈值" },
- { low: 120, high: 220, name: "高阈值" }
- ];
-
- // 对每个阈值组合执行二值化
- for (var i = 0; i < thresholdSets.length; i++) {
- var set = thresholdSets[i];
- printl("\n测试" + set.name + ": 低=" + set.low + ", 高=" + set.high);
-
- // 创建原始图像的副本以避免修改原图
- var matCopy = originalMat.clone();
-
- // 执行二值化
- opencv.threshold(matCopy, set.low, set.high);
-
- // 输出结果信息
- printl(set.name + "二值化后Mat信息:");
- printl(matCopy);
-
- // 可以在这里将结果保存或显示
- printl(set.name + "二值化完成");
- }
-
- printl("\n多阈值对比完成");
- return true;
- }
- /**
- * 灰度转换后二值化示例
- * 先将图像转为灰度,再进行二值化处理
- */
- function grayThenThresholdDemo() {
- printl("执行灰度转换后二值化示例...");
-
- // 获取屏幕尺寸
- var screenWidth = typeof screen !== 'undefined' && typeof screen.getScreenWidth === 'function' ?
- screen.getScreenWidth() : 1080;
- var screenHeight = typeof screen !== 'undefined' && typeof screen.getScreenHeight === 'function' ?
- screen.getScreenHeight() : 1920;
-
- // 截图
- var screenshot = screen.screenShotFull();
- var mat = screenshot.getMat();
-
- if (!mat) {
- return { error: true, message: "无法获取图像Mat对象" };
- }
-
- // 先转换为灰度图
- printl("转换为灰度图...");
- opencv.toGray(mat);
-
- // 输出灰度图信息
- printl("灰度图Mat信息:");
- printl(mat);
-
- // 再进行二值化
- printl("执行二值化...");
- opencv.threshold(mat, 80, 200);
-
- // 输出二值化后的结果
- printl("灰度后二值化Mat信息:");
- printl(mat);
-
- printl("灰度后二值化完成");
-
- return {
- error: false,
- mat: mat,
- message: "灰度转换后二值化成功"
- };
- }
- /**
- * 应用场景示例:二值化后进行轮廓检测
- * @param {number} threshold 二值化阈值
- */
- function thresholdForContourDetection(threshold) {
- printl("执行二值化后轮廓检测示例...");
-
- threshold = threshold || 100;
-
- // 获取屏幕尺寸
- var screenWidth = typeof screen !== 'undefined' && typeof screen.getScreenWidth === 'function' ?
- screen.getScreenWidth() : 1080;
- var screenHeight = typeof screen !== 'undefined' && typeof screen.getScreenHeight === 'function' ?
- screen.getScreenHeight() : 1920;
-
- // 截图并转换为灰度
- var screenshot = screen.screenShotFull();
- var mat = screenshot.getMat();
-
- if (!mat) {
- printl("无法获取图像Mat对象");
- return false;
- }
-
- // 转换为灰度图
- opencv.toGray(mat);
-
- // 输出灰度图信息
- printl("灰度图Mat信息:");
- printl(mat);
-
- // 二值化
- opencv.threshold(mat, threshold, 255);
-
- // 输出二值化后的结果
- printl("二值化后Mat信息:");
- printl(mat);
-
- // 进行轮廓检测
- printl("进行轮廓检测...");
- var contours = opencv.getContours(mat);
-
- if (contours && contours.length > 0) {
- printl("找到 " + contours.length + " 个轮廓");
-
- // 可以在这里对轮廓进行进一步处理
- for (var i = 0; i < Math.min(5, contours.length); i++) { // 只显示前5个轮廓信息
- var contour = contours[i];
- printl("轮廓 " + (i+1) + ": 位置(" + contour.x + "," + contour.y + "), 大小 " + contour.width + "x" + contour.height);
- }
- } else {
- printl("未找到轮廓");
- }
-
- printl("轮廓检测示例完成");
- return true;
- }
- /**
- * 应用场景示例:二值化后进行OCR识别
- * 对文字区域进行二值化以提高OCR识别率
- * @param {number} x 文字区域x坐标
- * @param {number} y 文字区域y坐标
- * @param {number} width 文字区域宽度
- * @param {number} height 文字区域高度
- */
- function thresholdForOCRDemo(x, y, width, height) {
- printl("执行二值化后OCR识别示例...");
-
- // 获取屏幕尺寸
- var screenWidth = typeof screen !== 'undefined' && typeof screen.getScreenWidth === 'function' ?
- screen.getScreenWidth() : 1080;
- var screenHeight = typeof screen !== 'undefined' && typeof screen.getScreenHeight === 'function' ?
- screen.getScreenHeight() : 1920;
-
- // 默认值设置 - 使用屏幕中心区域
- x = x || Math.floor(screenWidth / 4);
- y = y || Math.floor(screenHeight / 4);
- width = width || Math.floor(screenWidth / 2);
- height = height || Math.floor(screenHeight / 4);
-
- // 确保区域参数在有效范围内
- x = Math.max(0, x);
- y = Math.max(0, y);
- width = Math.min(screenWidth - x, width);
- height = Math.min(screenHeight - y, height);
-
- printl("文字区域: (" + x + "," + y + ") " + width + "x" + height);
-
- // 截图
- var fullScreenshot = screen.screenShotFull();
- var fullMat = fullScreenshot.getMat();
-
- if (!fullMat) {
- printl("无法获取图像Mat对象");
- return false;
- }
-
- // 使用submat方法提取区域
- var mat = fullMat.submat(y, y + height, x, x + width);
-
- if (!mat) {
- printl("无法获取文字区域图像");
- return false;
- }
-
- printl("提取的文字区域Mat信息:");
- printl(mat);
-
- printl("文字区域: (" + x + "," + y + ") " + width + "x" + height);
-
- // 预处理:转换为灰度
- opencv.toGray(mat);
-
- // 输出灰度图信息
- printl("文字区域灰度图信息:");
- printl(mat);
-
- // 二值化 - 文字识别通常需要较高的阈值来突出文字
- opencv.threshold(mat, 100, 200);
-
- // 输出二值化后的结果
- printl("文字区域二值化后Mat信息:");
- printl(mat);
-
- printl("二值化预处理完成,准备OCR识别");
-
- // 注意:实际使用中,这里应该调用AIWROK的OCR功能
- printl("提示:在实际应用中,这里应调用ocr.recognize()或其他OCR方法");
-
- return true;
- }
- /**
- * 自适应阈值二值化模拟
- * 模拟自适应阈值的效果,通过对图像不同区域使用不同阈值
- * @param {number} blockSize 块大小
- */
- function adaptiveThresholdSimulation(blockSize) {
- printl("执行自适应阈值二值化模拟...");
-
- blockSize = blockSize || 4; // 将图像分成blockSize x blockSize块
-
- // 获取屏幕尺寸
- var screenWidth = typeof screen !== 'undefined' && typeof screen.getScreenWidth === 'function' ?
- screen.getScreenWidth() : 1080;
- var screenHeight = typeof screen !== 'undefined' && typeof screen.getScreenHeight === 'function' ?
- screen.getScreenHeight() : 1920;
-
- var screenshot = screen.screenShotFull();
- var mat = screenshot.getMat();
-
- if (!mat) {
- printl("无法获取图像Mat对象");
- return false;
- }
-
- var imgWidth = screenshot.getWidth();
- var imgHeight = screenshot.getHeight();
-
- // 计算每个块的大小
- var blockW = Math.floor(imgWidth / blockSize);
- var blockH = Math.floor(imgHeight / blockSize);
-
- printl("图像分割为 " + blockSize + "x" + blockSize + " 块,每块大小约 " + blockW + "x" + blockH);
-
- // 转换为灰度图
- opencv.toGray(mat);
-
- // 输出灰度图信息
- printl("整体灰度图信息:");
- printl(mat);
-
- // 对每个块使用不同的阈值(这里使用简单的随机阈值模拟自适应效果)
- for (var i = 0; i < blockSize; i++) {
- for (var j = 0; j < blockSize; j++) {
- // 计算块的坐标
- var blockX = i * blockW;
- var blockY = j * blockH;
- var currentBlockW = (i === blockSize - 1) ? imgWidth - blockX : blockW;
- var currentBlockH = (j === blockSize - 1) ? imgHeight - blockY : blockH;
-
- // 确保区域参数有效
- currentBlockW = Math.min(currentBlockW, imgWidth - blockX);
- currentBlockH = Math.min(currentBlockH, imgHeight - blockY);
-
- // 为每个块生成一个随机阈值(在一定范围内)
- var blockThreshold = 80 + Math.floor(Math.random() * 60);
-
- // 使用submat方法提取区域
- if (blockY + currentBlockH <= imgHeight && blockX + currentBlockW <= imgWidth) {
- var blockMat = mat.submat(blockY, blockY + currentBlockH, blockX, blockX + currentBlockW);
-
- // 输出块信息
- printl("处理块(" + i + "," + j + "),坐标:(" + blockX + "," + blockY + "),大小:" + currentBlockW + "x" + currentBlockH);
-
- // 对块进行二值化
- opencv.threshold(blockMat, blockThreshold, 200);
-
- // 输出块二值化后的结果
- printl("块(" + i + "," + j + ")二值化后:");
- printl(blockMat);
- }
- }
- }
-
- printl("自适应阈值模拟完成,最终Mat信息:");
- printl(mat);
-
- return true;
- }
- /**
- * 主函数:运行所有二值化示例
- */
- function runAllThresholdDemos() {
- printl("==================================================");
- printl(" 图片二值化示例集");
- printl("==================================================\n");
-
- // 检查环境
- if (typeof opencv === 'undefined') {
- printl("错误: 未找到OpenCV模块,请确保在AIWROK环境中运行");
- return;
- }
-
- // 获取屏幕尺寸
- var screenWidth = typeof screen !== 'undefined' && typeof screen.getScreenWidth === 'function' ?
- screen.getScreenWidth() : 1080;
- var screenHeight = typeof screen !== 'undefined' && typeof screen.getScreenHeight === 'function' ?
- screen.getScreenHeight() : 1920;
-
- printl("当前屏幕尺寸: " + screenWidth + "x" + screenHeight);
-
- // 1. 基本二值化示例
- printl("1. 基本二值化示例");
- basicThresholdDemo(50, 150);
- printl("----------------------------------------\n");
-
- // 2. 区域二值化示例
- printl("2. 区域二值化示例");
- // 使用屏幕中心的一个区域
- var regionX = Math.floor(screenWidth / 4);
- var regionY = Math.floor(screenHeight / 4);
- var regionWidth = Math.floor(screenWidth / 2);
- var regionHeight = Math.floor(screenHeight / 3);
- regionThresholdDemo(regionX, regionY, regionWidth, regionHeight, 100);
- printl("----------------------------------------\n");
-
- safeSleep(1000); // 添加延迟,避免连续操作过快
-
- // 3. 多阈值对比
- printl("3. 多阈值对比示例");
- multiThresholdComparison();
- printl("----------------------------------------\n");
-
- // 4. 灰度后二值化
- printl("4. 灰度转换后二值化示例");
- grayThenThresholdDemo();
- printl("----------------------------------------\n");
-
- safeSleep(1000); // 添加延迟
-
- // 5. 二值化后轮廓检测
- printl("5. 二值化后轮廓检测示例");
- thresholdForContourDetection(100);
- printl("----------------------------------------\n");
-
- safeSleep(1000); // 添加延迟
-
- // 6. 二值化后OCR示例
- printl("6. 二值化后OCR识别示例");
- thresholdForOCRDemo(Math.floor(screenWidth / 4), Math.floor(screenHeight / 3), Math.floor(screenWidth / 2), Math.floor(screenHeight / 4));
- printl("----------------------------------------\n");
-
- safeSleep(1000); // 添加延迟
-
- // 7. 自适应阈值模拟
- printl("7. 自适应阈值二值化模拟");
- adaptiveThresholdSimulation(4);
- printl("----------------------------------------\n");
-
- printl("所有二值化示例执行完成!");
- }
- /**
- * 工具函数:安全延迟
- * @param {number} ms 延迟毫秒数
- */
- function safeSleep(ms) {
- if (typeof sleep !== 'undefined' && sleep.millisecond) {
- sleep.millisecond(ms);
- } else if (typeof java !== 'undefined') {
- java.lang.Thread.sleep(ms);
- }
- }
- /**
- * 二值化工具类 - 提供便捷的二值化方法
- */
- var ThresholdUtils = {
- /**
- * 快速二值化图像
- * @param {Object} options 配置选项
- * @returns {Object} 二值化结果
- */
- quickThreshold: function(options) {
- options = options || {};
-
- // 获取屏幕尺寸
- var screenWidth = typeof screen !== 'undefined' && typeof screen.getScreenWidth === 'function' ?
- screen.getScreenWidth() : 1080;
- var screenHeight = typeof screen !== 'undefined' && typeof screen.getScreenHeight === 'function' ?
- screen.getScreenHeight() : 1920;
-
- var screenshot = options.image || screen.screenShotFull();
- var mat = screenshot.getMat();
-
- if (options.toGray !== false) {
- opencv.toGray(mat);
- }
-
- opencv.threshold(
- mat,
- options.lowThreshold || 80,
- options.highThreshold || 200
- );
-
- // 输出结果信息
- printl("快速二值化结果:");
- printl(mat);
-
- return {
- success: true,
- mat: mat
- };
- },
-
- /**
- * 处理文字图像的二值化参数
- * @returns {Object} 适合文字的二值化参数
- */
- getTextThresholdParams: function() {
- return {
- lowThreshold: 100,
- highThreshold: 220,
- toGray: true
- };
- },
-
- /**
- * 处理图像边缘的二值化参数
- * @returns {Object} 适合边缘检测的二值化参数
- */
- getEdgeThresholdParams: function() {
- return {
- lowThreshold: 50,
- highThreshold: 150,
- toGray: true
- };
- }
- };
- // 执行所有二值化示例
- runAllThresholdDemos();
复制代码
| |  | |  |
|