如何使用 OpenCv 在图像上找到角 [英] How to find corners on a Image using OpenCv
问题描述
我试图找到图像上的角,我不需要轮廓,只需要 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.
推荐答案
首先,检查 OpenCV 发行版中的/samples/c/squares.c.这个例子提供了一个方形检测器,它应该是如何检测角状特征的一个很好的开始.然后,看看 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:
- 从 RGB 转换为灰度 (cvCvtColor)
- 平滑(cvSmooth)
- 阈值 (cvThreshold)
- 检测边缘(cvCanny)
- 查找轮廓(cvFindContours)
- 具有线性特征的近似轮廓 (cvApproxPoly)
- 找到矩形",这些结构是:具有 4 个点的多边形轮廓、足够的面积、相邻的边缘约为 90 度、相对"顶点之间的距离足够大等.
第 7 步是必要的,因为稍微嘈杂的图像会产生许多在多边形化后呈现矩形的结构.在我的应用程序中,我还必须处理出现在所需正方形内或重叠的正方形结构.我发现轮廓的面积属性和重心有助于辨别正确的矩形.
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 在图像上找到角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!