找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1865|回复: 0

AIWROK软件技巧分享图片二值化封装使用

[复制链接]
发表于 2025-4-24 09:51:39 | 显示全部楼层 |阅读模式
AIWROK软件技巧分享图片二值化封装使用


// 引入 Android 和 OpenCV 的类
importPackage(org.opencv.core);
importPackage(org.opencv.imgproc);
importPackage(org.opencv.android);
importClass(android.graphics.Bitmap);


function screenshotAndBinarize(width, height, quality, threshold1, threshold2) {
// 进行屏幕截图
var bitmap = screen.screenShot(width, height, quality).getBitmap();

// 将 Bitmap 转换为 OpenCV 的 Mat
var mat = new Mat();
Utils.bitmapToMat(bitmap, mat);


// 进行二值化处理
var binaryMat = new Mat();
Imgproc.threshold(mat, binaryMat, threshold1, threshold2, Imgproc.THRESH_BINARY);
printl(binaryMat);


// 将二值化后的 Mat 转换回 Bitmap
var binaryBitmap = Bitmap.createBitmap(binaryMat.cols(), binaryMat.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(binaryMat, binaryBitmap);
printl(binaryBitmap);


return binaryBitmap;
}


// 调用封装的函数
var resultBitmap = screenshotAndBinarize(640, 960, 100, 50, 150);

代码结构

  1. 引入库和类
    • importPackage 和 importClass 用于导入OpenCV和Android中相关类和包,这在使用其函数时是必需的。
  1. 函数定义
    • screenshotAndBinarize: 封装了对屏幕截图进行二值化处理的所有步骤。
  1. 函数参数
    • width: 截图的宽度。
    • height: 截图的高度。
    • quality: 截图的质量。
    • threshold1: 二值化过程中使用的阈值1。
    • threshold2: 二值化过程中使用的阈值2。

代码步骤

  1. 屏幕截图
var bitmap = screen.screenShot(width, height, quality).getBitmap();
  • 使用给定的宽度、高度和质量参数进行屏幕截图,结果是一个 Bitmap 对象。
  1. Bitmap 转 Mat
var mat = new Mat();
Utils.bitmapToMat(bitmap, mat);
  • 创建一个 Mat 对象。
    • Utils.bitmapToMat 将 Bitmap 转换为 OpenCV 的 Mat 对象,便于后续的图像处理操作。
  1. 二值化处理
var binaryMat = new Mat();
Imgproc.threshold(mat, binaryMat, threshold1, threshold2, Imgproc.THRESH_BINARY);
printl(binaryMat);
    • 初始化一个新的 Mat 对象 binaryMat 用于存储处理后的图像。
    • 使用 Imgproc.threshold 方法对图像进行二值化处理。
    • threshold1 和 threshold2 是用于二值化的阈值。
    • Imgproc.THRESH_BINARY 表示采用二值化处理方法。
  1. Mat 转 Bitmap
var binaryBitmap = Bitmap.createBitmap(binaryMat.cols(), binaryMat.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(binaryMat, binaryBitmap);
printl(binaryBitmap);
  • 创建一个新的 Bitmap 对象,用于存储二值化后的图像。
  • 将 binaryMat 转换回 Bitmap。
  1. 返回值
return binaryBitmap;
  • 函数的返回值是处理后的 Bitmap 对象。

调用示例

var resultBitmap = screenshotAndBinarize(640, 960, 100, 50, 150);
  • 使用特定参数调用该函数,获取二值化后的图像。
1.png
1.png
2.png
3.png
4.png
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|

GMT+8, 2026-10-23 17:58

安卓脚本软件 · 键鼠盒子 · HID · 机械臂

快速回复 返回顶部 返回列表

随机推荐 · 热帖 / 新帖 / 随便看看

社区会员(随机展示)