gpt4 book ai didi

javascript - opencv.js 透视变换

转载 作者:行者123 更新时间:2023-11-28 14:31:09 26 4
gpt4 key购买 nike

我正在尝试使用 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);

上面的最后一行会导致运行时错误(未捕获( promise 中):6402256 - 异常捕获已禁用)。我可以在其他 Mat 对象上运行 minMaxLoc()

最佳答案

对于其他想要在 OpenCV.JS 中执行此操作的人来说,我上面的评论似乎仍然是准确的。找到的轮廓不能与minMaxLoc一起使用,但可以从data32S[]中提取X/Y数据。这应该就是进行视角转换所需的全部内容。一些代码如下。

//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);
}
}

关于javascript - opencv.js 透视变换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51528462/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com