gpt4 book ai didi

c++ - opencv/c++ 我如何只检测之前检测到的方形区域内的关键点

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

我正在做我的项目,我遇到了一个问题,我不知道如何只从一个以前检测过的方形区域检测关键点。下面是我的演示,到目前为止,我的代码会检测正方形内外的关键点:https://www.youtube.com/watch?feature=player_embedded&v=3U8V6PhMnZ8

这是我找到正方形的代码:

       const int threshold_level = 2;
for (int l = 0; l < threshold_level; l++)
{

gray = gray0 >= (l+1) * 255 / threshold_level;

// Find contours and store them in a list
findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

// Test contours
vector<Point> approx;
for (size_t i = 0; i < contours.size(); i++)
{
// approximate contour with accuracy proportional
// to the contour perimeter
approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true);

// Note: absolute value of an area is used because
// area may be positive or negative - in accordance with the
// contour orientation
if (approx.size() == 4 &&
fabs(contourArea(Mat(approx))) > 3000 &&
isContourConvex(Mat(approx)))
{
double maxCosine = 0;

for (int j = 2; j < 5; j++)
{
double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
maxCosine = MAX(maxCosine, cosine);
}

if (maxCosine < 0.3)
squares.push_back(approx);
}
}
}

这是我绘制正方形和角点的代码:

 const Point* p = &squares[i][0];
int n = (int)squares[i].size();
Point p1 = squares[i][0];
Point p2 = squares[i][1];
Point p3 = squares[i][2];
Point p4 = squares[i][3];
cout<<"p1 is "<<p1<<" p2 is "<<p2<<" p3 is "<<p3<<" p4 is "<<p4<<endl;
circle(image, squares[i][0], 3, Scalar(0,0,255), 5, 8, 0);
circle(image, squares[i][1], 3, Scalar(0,255,255), 5, 8, 0);
circle(image, squares[i][2], 3, Scalar(255,0,255), 5, 8, 0);
circle(image, squares[i][3], 3, Scalar(255,255,0), 5, 8, 0);

polylines(image, &p, &n, 1, true, Scalar(0,255,0), 3, CV_AA);

这是我检测关键点的代码:

    Mat gray_image;
vector<KeyPoint> keyPoints;
cvtColor(image, gray_image, CV_BGR2GRAY);
FastFeatureDetector fast(60);
fast.detect(gray_image,keyPoints);
drawKeypoints(image, keyPoints,image, Scalar::all(255), DrawMatchesFlags::DRAW_OVER_OUTIMG);

最佳答案

您可以使用裁剪图像

Rect r(left,top,width,height);//我们感兴趣的部分图片

Mat roi(fullImage, r);//将创建对原始图像的矩形 r 的引用。请注意,它不是拷贝。

关于c++ - opencv/c++ 我如何只检测之前检测到的方形区域内的关键点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14379212/

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