gpt4 book ai didi

c++ - 检测方 block opencv和c++

转载 作者:太空狗 更新时间:2023-10-29 21:02:58 27 4
gpt4 key购买 nike

您好,我正在从事计算机视觉项目,并尝试使用来自相机的 openCV/C++ 来检测正方形。我已经从 openCV 库下载了源代码,但似乎很难失去 fps​​。有人知道如何解决这个问题吗?下面有一个关于我的测试的视频链接,请查看: http://magicbookproject.blogspot.co.uk/2012/12/detect-paper-demo.html

这是代码,刚刚在另一篇文章中找到:

void find_squares(Mat& image, vector<vector<Point> >& squares)
{
// blur will enhance edge detection
Mat blurred(image);
medianBlur(image, blurred, 9);

Mat gray0(blurred.size(), CV_8U), gray;
vector<vector<Point> > contours;

// find squares in every color plane of the image
for (int c = 0; c < 3; c++)
{
int ch[] = {c, 0};
mixChannels(&blurred, 1, &gray0, 1, ch, 1);

// try several threshold levels
const int threshold_level = 2;
for (int l = 0; l < threshold_level; l++)
{
// Use Canny instead of zero threshold level!
// Canny helps to catch squares with gradient shading
if (l == 0)
{
Canny(gray0, gray, 10, 20, 3); //

// Dilate helps to remove potential holes between edge segments
dilate(gray, gray, Mat(), Point(-1,-1));
}
else
{
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))) > 1000 &&
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);
}
}
}
}

最佳答案

如果您不介意失去准确性,您可以加快速度。例如

// find squares in every color plane of the image
for (int c = 0; c < 3; c++)

您正在循环三个颜色平面。只需检查一种颜色(好像图像是灰度图像),速度应该提高三倍。

也可以在没有 Canny 的情况下尝试,这很慢。设置一个use_canny参数,

 if (l == 0 && use_canny)
{
Canny(gray0, gray, 10, 20, 3); //

比较有无。我得到了可以接受的结果,而且速度要快得多。

关于c++ - 检测方 block opencv和c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14221591/

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