如何使用OpenCv在Image上查找角点 [英] How to find corners on a Image using OpenCv

查看:222
本文介绍了如何使用OpenCv在Image上查找角点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到图像上的角落,我不需要轮廓,只需要4个角落。我将使用4个角来改变视角。

I´m trying to find the corners on a image, I don´t need the contours, only the 4 corners. I will change the perspective using 4 corners.

我正在使用Opencv,但我需要知道找到角落的步骤以及我将使用的功能。

I´m using Opencv, but I need to know the steps to find the corners and what function I will use.

我的图像将是这样的:(没有红点,我会在之后绘制点数)

My images will be like this:(without red points, I will paint the points after)

编辑:

建议后步骤,我写了代码:(注意:我没有使用纯OpenCv,我使用javaCV,但逻辑是一样的。)

After suggested steps, I writed the code: (Note: I´m not using pure OpenCv, I´m using javaCV, but the logic it´s the same).

// Load two images and allocate other structures (I´m using other image)
    IplImage colored = cvLoadImage(
            "res/scanteste.jpg",
            CV_LOAD_IMAGE_UNCHANGED);

    IplImage gray = cvCreateImage(cvGetSize(colored), IPL_DEPTH_8U, 1);
    IplImage smooth = cvCreateImage(cvGetSize(colored), IPL_DEPTH_8U, 1);

    //Step 1 - Convert from RGB to grayscale (cvCvtColor)
    cvCvtColor(colored, gray, CV_RGB2GRAY);

    //2 Smooth (cvSmooth)
    cvSmooth( gray, smooth, CV_BLUR, 9, 9, 2, 2); 

    //3 - cvThreshold  - What values?
    cvThreshold(gray,gray, 155, 255, CV_THRESH_BINARY);

    //4 - Detect edges (cvCanny) -What values?
    int N = 7;
    int aperature_size = N;
    double lowThresh = 20;
    double highThresh = 40;     
    cvCanny( gray, gray, lowThresh*N*N, highThresh*N*N, aperature_size );   

    //5 - Find contours (cvFindContours)
    int total = 0;
    CvSeq contour2 = new CvSeq(null);
    CvMemStorage storage2 = cvCreateMemStorage(0);
    CvMemStorage storageHull = cvCreateMemStorage(0);
    total = cvFindContours(gray, storage2, contour2, Loader.sizeof(CvContour.class), CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE);
    if(total > 1){
          while (contour2 != null && !contour2.isNull()) {
              if (contour2.elem_size() > 0) {
                //6 - Approximate contours with linear features (cvApproxPoly)
                  CvSeq points = cvApproxPoly(contour2,Loader.sizeof(CvContour.class), storage2, CV_POLY_APPROX_DP,cvContourPerimeter(contour2)*0.005, 0);
                  cvDrawContours(gray, points,CvScalar.BLUE, CvScalar.BLUE, -1, 1, CV_AA);

              }
              contour2 = contour2.h_next();
          }

    } 

所以,我想找到角落,但我不喜欢我不知道如何使用像cvCornerHarris等角落功能。

So, I want to find the cornes, but I don´t know how to use corners function like cvCornerHarris and others.

推荐答案

首先,查看/samples/c/squares.c在您的OpenCV发行版中。这个例子提供了一个方形探测器,它应该是如何检测角落特征的一个很好的开端。然后,看看OpenCV的面向功能的函数,如cvCornerHarris()和cvGoodFeaturesToTrack()。

First, check out /samples/c/squares.c in your OpenCV distribution. This example provides a square detector, and it should be a pretty good start on how to detect corner-like features. Then, take a look at OpenCV's feature-oriented functions like cvCornerHarris() and cvGoodFeaturesToTrack().

上述方法可以返回许多类似角落的功能 - 大部分都不是您正在寻找的真正的角落。在我的应用程序中,我必须检测已旋转或倾斜的方块(由于透视)。我的检测管道包括:

The above methods can return many corner-like features - most will not be the "true corners" you are looking for. In my application, I had to detect squares that had been rotated or skewed (due to perspective). My detection pipeline consisted of:


  1. 从RGB转换为灰度(cvCvtColor)

  2. 平滑(cvSmooth) )

  3. 阈值(cvThreshold)

  4. 检测边缘(cvCanny)

  5. 查找轮廓(cvFindContours)

  6. 具有线性特征的近似轮廓(cvApproxPoly)

  7. 找到矩形,其结构为:具有4个点的多边形轮廓,具有足够的面积,具有相邻边缘为~90度,相对顶点之间的距离足够大等等。

  1. Convert from RGB to grayscale (cvCvtColor)
  2. Smooth (cvSmooth)
  3. Threshold (cvThreshold)
  4. Detect edges (cvCanny)
  5. Find contours (cvFindContours)
  6. Approximate contours with linear features (cvApproxPoly)
  7. Find "rectangles" which were structures that: had polygonalized contours possessing 4 points, were of sufficient area, had adjacent edges were ~90 degrees, had distance between "opposite" vertices was of sufficient size, etc.

第7步是必要的,因为a略微嘈杂的图像可以产生许多在多边形化后呈矩形的结构。在我的应用程序中,我还必须处理出现在所需方块内或与所需方块重叠的方形结构。我发现轮廓的面积属性和重心有助于辨别正确的矩形。

Step 7 was necessary because a slightly noisy image can yield many structures that appear rectangular after polygonalization. In my application, I also had to deal with square-like structures that appeared within, or overlapped the desired square. I found the contour's area property and center of gravity to be helpful in discerning the proper rectangle.

这篇关于如何使用OpenCv在Image上查找角点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆