autojs|autojs之提取图片中的红色文字(通过找边界进行裁剪)


autojs之提取图片中的红色文字——通过找边界进行裁剪

  • 一、前言
  • 二、参考
  • 三、效果
    • 1.autojs的findcolor函数找到的左上边界的点
    • 2.找右下边界
  • 四、总结

一、前言 之前写过通过二值化的方法提取图片中的红色文字,感觉很麻烦,在看到下面按键精灵的文章后,觉得可以一试
二、参考 【按键精灵安卓版】找出不同颜色的文字(找图方向参数的应用)
事实上,autojs里没有按键精灵findcolor的功能,不能从指定的方向对图片进行搜索,也不能准确的找到红色文字最外侧的点,所以很难找到文字的边界;
而且这种方法只适用于单行的红色文字,如果文字有换行,那么需要添加更多的判断,个人感觉很不方便。
三、效果 实际功能其实没有实现,因为红色文字的边界很难找到,只是把我测试的效果给大家看一下
1.autojs的findcolor函数找到的左上边界的点
path = "/sdcard/Pictures/QQ/a.png" var img = images.read(path); log("读取图片")//循环找色,找到红色时停止并报告坐标 while (true) {var point = findColor(img, "#fd1111", { threshold: 120 }); if (point) {toastLog("找到红色#fd1111,坐标为(" + point.x + ", " + point.y + ")"); var clip = images.clip(img,point.x, point.y, 20, 20); //裁剪 images.save(clip, "./clip2.png"); break } } img.recycle(); //回收图片,截屏的不用 exit()

autojs|autojs之提取图片中的红色文字(通过找边界进行裁剪)
文章图片
可以看到找到的点并不是最左边的点
2.找右下边界 不能从指定的方向对图片进行搜索,所以只能在找到最左边届的点后,通过循环往右找,直到找到不是红字的黑色字后停止
【autojs|autojs之提取图片中的红色文字(通过找边界进行裁剪)】但是因为图片里红色的范围很大,黑色的范围也很大,并不能很好的匹配,所以这个方法的准确度可能还不如二值化的方法
autojs|autojs之提取图片中的红色文字(通过找边界进行裁剪)
文章图片

path = "/sdcard/Pictures/QQ/a.png" var img = images.read(path); log("读取图片") while (true) {var point = findColor(img, "#fd1111", { threshold: 120 }); if (point) {toastLog("找到红色#fd1111,坐标为(" + point.x + ", " + point.y + ")"); var x1 = point.x, x2 = 0, y1 = point.y, y2 = 0; for (var x = point.x, y = point.y; y < img.getHeight(); y += 1) {var color = images.pixel(img, x, y); toastLog("坐标为(" + x + ", " + y + ") " + colors.toString(color)); if (colors.isSimilar(color, "#000000", 50)) { //和黑色相似 y2 = y; //下边界 break }} for (var x = point.x, y = point.y; x < img.getWidth(); x += 1) { //x < point.x + 100 var color = images.pixel(img, x, y); toastLog("坐标为(" + x + ", " + y + ") " + colors.toString(color)); if (colors.isSimilar(color, "#000000", 50)) { //和黑色相似//if (colors.toString(color) == "#E2DCDA") {x2 = x; //右边界 break }}log(x1, y1, x2 - x1, y2 - y1) var clip = images.clip(img, x1, y1, x2 - x1, y2 - y1); images.save(clip, "./clip.png"); break } } img.recycle(); exit()

四、总结 个人认为,这个通过边界裁剪的方法也很麻烦,而且普适性还不如二值化的方法,暂且放弃这种方法。
有觉得的可行的朋友可以自己尝试一下,欢迎讨论。
还是研究一下怎么将图片中的黑色变白,保持其他颜色不变,如果有懂行的大佬,希望指点一下,感谢。

    推荐阅读