自动发帖软件

标题: AIWROK软件技巧分享图片二值化封装使用 [打印本页]

作者: 发帖软件    时间: 11 小时前
标题: AIWROK软件技巧分享图片二值化封装使用

代码结构

  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);
  • 使用特定参数调用该函数,获取二值化后的图像。
// 引入 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);
  AIWROK软件技巧分享图片二值化封装使用 群发软件发帖工具 AIWROK软件技巧分享图片二值化封装使用 群发软件发帖工具  






欢迎光临 自动发帖软件 (http://www.fatiegongju.com/) Powered by Discuz! X3.2