autojs从书的中间分割成两张

168 阅读1分钟

牙叔教程 简单易懂

群里有人问

思路

提取书本中间的线-> 切开

提取书本中间的线

Canny 边缘检测

Imgproc.Canny(grayMat, cannyMat, lowThreshold, lowThreshold * ratio, kernel_size, false);

霍夫变换直线检测

function getHoughLinesP(mat, size) {
  let lines = new Mat();
  let threshold = size / 5; // 阈值,只有获得足够交点的极坐标点才被看成是直线
  let minLineSize = size; // 最小直线长度,有默认值0,表示最低线段的长度,比这个设定参数短的线段就不能被显现出来。
  let lineGap = size; // 最大间隔,有默认值0,允许将同一行点与点之间连接起来的最大的距离。
  Imgproc.HoughLinesP(mat, lines, 1, Math.PI / 180, threshold, minLineSize, lineGap);
  return lines;
}

过滤直线

直线的横坐标应该在图片中间

function filterLineDataList(lines) {
  // { angle: 90, distance: 786, x1: 4, y1: 925, x2: 4, y2: 139 },
  let newLines = [];
  var len = lines.length;
  let centerX = img.width / 2;
  for (var i = 0; i < len; i++) {
    let line = lines[i];
    if (Math.abs(line.x1 - centerX) < img.width / 4 && Math.abs(line.x2 - centerX) < img.width / 4) {
      newLines.push(line);
    }
  }
  return newLines;
}

计算切割点

计算直线和上下两边的交点

function getFocusCoordinatesOfTwoLines(line1, line2) {
  var x1 = line1.x1;
  var y1 = line1.y1;
  var x2 = line1.x2;
  var y2 = line1.y2;
  var x3 = line2.x1;
  var y3 = line2.y1;
  var x4 = line2.x2;
  var y4 = line2.y2;
  var x =
    ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) /
    ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4));
  var y =
    ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) /
    ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4));
  return { x: x, y: y };
}

分割图片

let clipImg = images.clip(img, 0, 0, leftPageTopSideLength, img.height);
pts.add(new MatOfPoint(topLeftPoint, topRightPoint, bottomRightPoint, bottomLeftPoint, topLeftPoint));
Imgproc.fillPoly(polyMat, pts, Scalar(255), 1);
Core.bitwise_and(clipImg.mat, newPolyMat, newMat);

测试环境
手机: Mi 11 Pro
Android版本: 12
Autojs版本: 9.1.10


名人名言

思路是最重要的, 其他的百度, bing, stackoverflow, github, 安卓文档, autojs文档, 最后才是群里问问 --- 牙叔教程

声明

部分内容来自网络 本教程仅用于学习, 禁止用于其他用途

微信公众号 牙叔教程