我正在尝试使用opencv.js在提供的图像中查找文档(检测边缘,应用透视变换等)。我已经有了一套合理的代码(偶尔)可以检测到文档的边缘并获取其边界框。但是,我正在努力执行透视变换步骤。这里有一些帮助器(不是JS)这里和这里。不幸的是,我卡在了一些简单的问题上。我可以找到具有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 - 异常捕获被禁用
)。我可以在其他 Mat
对象上运行 minMaxLoc()
。
foundContour
是一个2通道的Mat
对象。看起来minMaxLoc
只能用于单通道的Mat
。我不确定如何进行转换,但我能够从foundContour.data32S[]
数组中获取X/Y数据。可能还有更好的方法,但这是我目前掌握的最好方法。 - BRass