opencv.js透视变换

时间:2018-07-25 22:19:19

标签: javascript opencv

我正在尝试使用opencv.js在提供的图像中查找文档(检测边缘,应用透视变换等。

我有一套合理的代码,可以(有时)检测文档的边缘并为此抓取边框。但是,我正在努力进行透视变换步骤。有一些帮助程序(不适用于JS)herehere

不幸的是,我陷入了简单的事情。我可以找到具有4条边的匹配Mat。显示表明它是准确的。但是,我不知道如何从该Mat中获取一些简单的X / Y信息。我以为minMaxLoc()是一个不错的选择,但是我在匹配Mat的传递过程中始终遇到错误。知道为什么我可以绘制foundContour并从中获取边界框信息,但是我无法在其上调用minMaxLoc吗?

代码:

//<Get Image>
//<Convert to Gray, do GaussianBlur, and do Canny edge detection>
let contours = new cv.MatVector();
cv.findContours(matDestEdged, contours, hierarchy, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE);

//<Sort resulting contours by area to get largest>

let foundContour = null;
for (let sortableContour of sortableContours) {
  let peri = cv.arcLength(sortableContour.contour, true);
  let approx = new cv.Mat();
  cv.approxPolyDP(sortableContour.contour, approx, 0.1 * peri, true);

  if (approx.rows == 4) {
    console.log('found it');
    foundContour = approx
    break;
  }
  else {
    approx.delete();
  }
}

//<Draw foundContour and a bounding box to ensure it's accurate>

//TODO: Do a perspective transform
let result = cv.minMaxLoc(foundContour);

上面的最后一行会导致运行时错误(Uncaught (in promise): 6402256 - Exception catching is disabled)。我可以在其他minMaxLoc()个对象上运行Mat

1 个答案:

答案 0 :(得分:3)

对于希望在OpenCV.JS中执行此操作的其他人,我上面的评论似乎仍然准确。找到的轮廓不能与minMaxLoc一起使用,但是可以将X / Y数据拉出data32S[]。这应该是完成此透视图转换所需的全部。下面是一些代码。

//Find all contours
let contours = new cv.MatVector();
let hierarchy = new cv.Mat();
cv.findContours(matDest, contours, hierarchy, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE);

//Get area for all contours so we can find the biggest
let sortableContours: SortableContour[] = [];
for (let i = 0; i < contours.size(); i++) {
  let cnt = contours.get(i);
  let area = cv.contourArea(cnt, false);
  let perim = cv.arcLength(cnt, false);

  sortableContours.push(new sortableContour({ areaSize: area, perimiterSize: perim, contour: cnt }));
}

//Sort 'em
sortableContours = sortableContours.sort((item1, item2) => { return (item1.areaSize > item2.areaSize) ? -1 : (item1.areaSize < item2.areaSize) ? 1 : 0; }).slice(0, 5);

//Ensure the top area contour has 4 corners (NOTE: This is not a perfect science and likely needs more attention)
let approx = new cv.Mat();
cv.approxPolyDP(sortableContours[0].contour, approx, .05 * sortableContours[0].perimiterSize, true);

if (approx.rows == 4) {
  console.log('Found a 4-corner approx');
  foundContour = approx;
}
else{
  console.log('No 4-corner large contour!');
  return;
}

//Find the corners
//foundCountour has 2 channels (seemingly x/y), has a depth of 4, and a type of 12.  Seems to show it's a CV_32S "type", so the valid data is in data32S??
let corner1 = new cv.Point(foundContour.data32S[0], foundContour.data32S[1]);
let corner2 = new cv.Point(foundContour.data32S[2], foundContour.data32S[3]);
let corner3 = new cv.Point(foundContour.data32S[4], foundContour.data32S[5]);
let corner4 = new cv.Point(foundContour.data32S[6], foundContour.data32S[7]);

//Order the corners
let cornerArray = [{ corner: corner1 }, { corner: corner2 }, { corner: corner3 }, { corner: corner4 }];
//Sort by Y position (to get top-down)
cornerArray.sort((item1, item2) => { return (item1.corner.y < item2.corner.y) ? -1 : (item1.corner.y > item2.corner.y) ? 1 : 0; }).slice(0, 5);

//Determine left/right based on x position of top and bottom 2
let tl = cornerArray[0].corner.x < cornerArray[1].corner.x ? cornerArray[0] : cornerArray[1];
let tr = cornerArray[0].corner.x > cornerArray[1].corner.x ? cornerArray[0] : cornerArray[1];
let bl = cornerArray[2].corner.x < cornerArray[3].corner.x ? cornerArray[2] : cornerArray[3];
let br = cornerArray[2].corner.x > cornerArray[3].corner.x ? cornerArray[2] : cornerArray[3];

//Calculate the max width/height
let widthBottom = Math.hypot(br.corner.x - bl.corner.x, br.corner.y - bl.corner.y);
let widthTop = Math.hypot(tr.corner.x - tl.corner.x, tr.corner.y - tl.corner.y);
let theWidth = (widthBottom > widthTop) ? widthBottom : widthTop;
let heightRight = Math.hypot(tr.corner.x - br.corner.x, tr.corner.y - br.corner.y);
let heightLeft = Math.hypot(tl.corner.x - bl.corner.x, tr.corner.y - bl.corner.y);
let theHeight = (heightRight > heightLeft) ? heightRight : heightLeft;

//Transform!
let finalDestCoords = cv.matFromArray(4, 1, cv.CV_32FC2, [0, 0, theWidth - 1, 0, theWidth - 1, theHeight - 1, 0, theHeight - 1]); //
let srcCoords = cv.matFromArray(4, 1, cv.CV_32FC2, [tl.corner.x, tl.corner.y, tr.corner.x, tr.corner.y, br.corner.x, br.corner.y, bl.corner.x, bl.corner.y]);
let dsize = new cv.Size(theWidth, theHeight);
let M = cv.getPerspectiveTransform(srcCoords, finalDestCoords)
cv.warpPerspective(matDestTransformed, finalDest, M, dsize, cv.INTER_LINEAR, cv.BORDER_CONSTANT, new cv.Scalar());

作为参考,这是我用于SortableContour的类定义。上面的代码仅供参考,而不是可以单独运行的代码。

export class SortableContour {
    perimiterSize: number;
    areaSize: number;
    contour: any;

    constructor(fields: Partial<SortableContour>) {
      Object.assign(this, fields);
    }
  }