opencv - 用内部元素检测纸质照片上的矩形

opencv - Detecting a rectangle on a photo of paper with inner elements

所以我正在尝试阅读下面的图片。

我已经能够制作自适应阈值并检测旋转角度(我不确定是否必须旋转图像)

我正在努力解决的问题是检测包含表单的矩形。我尝试了不同的方法,例如 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
  };
}

在那之后,我能够突出显示角落并进行进一步的透视修复。