所以我试着读下面的图片
我已经能够制作自适应阈值并检测旋转角度(我不确定是否必须旋转图像)
我正在努力的是检测包含表单的矩形.我尝试了不同的方法,比如opencv的findContours().它能找到的最大轮廓是一个带名字的盒子.
之后我决定使用HoughLinesP,但它找到了很多行,我不知道如何过滤它们.检测矩形以校正表格也很方便,之后我将能够轻松地阅读答案.
所以我已经在考虑在角落添加黑色方形标记了.但是也许有人可以给我一些如何做到正确的想法.
HoughLinesP(我使用nodejs,但我可以读取python和c):
const imageSize = {
width: gray.cols,
height: gray.rows
};
const threshold_min = 200;
const ratio_min_max = 1;
const edges = gray.canny(threshold_min,threshold_min*ratio_min_max,3);
const minLineLength = imageSize.width / 4,
maxLineGap = 10,
threshold = 100;
const lines = edges.houghLinesP(1, Math.PI/180, threshold, minLineLength, maxLineGap);
//draw lines on the output
for( let i = 0; i < lines.length; i++ ) {
const l = lines[i];
const {x,y,z,w} = l;
output.drawLine(
cv.Point(w, x),
cv.Point(y, z),
new cv.Vec(Math.random()*255,Math.random()*255,Math.random()*255),
// new cv.Vec(0,0,255),
2,
// 1
);
}
//end draw lines
最佳答案 好的,所以我能够通过扩张来检测矩形:
let {area, contour} = getMaxContour(gray);
let dilateIterations = 0;
const MAX_DILATE_ITERATIONS = 9;
while(area<MINIMAL_POSSIBLE_AREA && dilateIterations<MAX_DILATE_ITERATIONS){
dilateIterations++;
gray = gray.dilate(new cv.Mat(), new cv.Point(-1,-1), 1, cv.BORDER_CONSTANT);
let result = getMaxContour(gray);
contour = result.contour;
area = result.area;
if(DEBUG) {
writeImage(`dilated_${dilateIterations}.png`, gray);
}
}
最大轮廓代码:
const getMaxContour = (image) => {
const contours = image.findContours(cv.RETR_LIST,cv.CHAIN_APPROX_SIMPLE);
let maxAreaFound = 0;
let maxContour = [];
let contourObj = null;
console.log(`Found ${contours.length} contours.`);
contours.forEach((contour,i)=>{
// const perimeter = cv.arcLength(contour, true);
const perimeter = contour.arcLength(true);
const approx = contour.approxPolyDP(0.1*perimeter, true);
const area = contour.moments()['m00'];
if (approx.length == 4 && maxAreaFound<area){
maxAreaFound = area;
maxContour = approx;
contourObj=contour;
}
});
console.log(JSON.stringify(contourObj))
console.log(`Max contour area found ${maxAreaFound}.`);
return {
contour:maxContour,
area:maxAreaFound
};
}
在那之后,我能够突出角落并进行进一步的视角修复.