gpt4 book ai didi

image-processing - 如何使用 OpenCv 在图像上查找角点

转载 作者:太空宇宙 更新时间:2023-11-03 20:36:42 25 4
gpt4 key购买 nike

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

我使用的是 Opencv,但我需要知道找到角点的步骤以及我将使用的函数。

我的图像将是这样的:(没有红点,我会在之后画点) enter image description here

编辑:

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

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

enter image description here

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

enter image description here

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

enter image description here

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

enter image description here

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

enter image description here

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

}

enter image description here

所以,我想找到拐角,但我不知道如何使用 cvCornerHarris 等拐角函数。

最佳答案

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

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

  1. 从 RGB 转换为灰度 (cvCvtColor)
  2. 平滑 (cvSmooth)
  3. 阈值(cvThreshold)
  4. 检测边缘(cvCanny)
  5. 寻找等高线 (cvFindContours)
  6. 具有线性特征的近似轮廓(cvApproxPoly)
  7. 找到具有以下结构的“矩形”:具有包含 4 个点的多边形轮廓、具有足够的面积、具有约 90 度的相邻边缘、具有足够大小的“相对”顶点之间的距离等。

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

关于image-processing - 如何使用 OpenCv 在图像上查找角点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7263621/

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