【问题标题】:detection square opencv and c ++检测方opencv和c++
【发布时间】:2012-12-22 16:42:02
【问题描述】:

您好,我正在从事计算机视觉项目,并尝试使用相机中的 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);
                }
        }
    }
}

}

【问题讨论】:

  • 你能给我们看一些代码吗...你是在使用源代码提供的正方形样本吗?
  • 这里是,有什么建议吗?我知道它的图像以及如何改变它一点然后它会为相机工作。

标签: c++ opencv computer-vision


【解决方案1】:

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

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

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

【讨论】:

  • 感谢您的回复。你说尝试不使用 Canny 那么如何进行边缘检测呢?
  • @user1959079 这不是绝对必要的。在轮廓提取阶段之前,您可以通过简单的阈值处理获得足够好的结果。
  • @user1959079 看看我的更新,不用 Canny 试试,它对我有用。您可以调整其他参数以进行补偿
  • 你是对的。我在没有 Canny 的情况下尝试过,结果没有我想象的那么糟糕,而且速度加快了。非常感谢。
  • @user1959079,如果它有效,您应该将我的答案标记为“已接受”(只需单击勾号);-j
【解决方案2】:

计算机视觉的一个很好的经验法则是在进行任何密集处理之前将您的图像转换为灰度。仅在您认为绝对必要时才循环遍历颜色通道。我推荐以下模式进行对象识别:

  1. 将图像转换为灰度
  2. 将灰度图像过滤为更简单的格式(canny、阈值、边缘检测)
  3. 进行繁重的处理(检测方形)
  4. 使用处理后的值重建原始图像(绘制/存储您的正方形)

请记住,您正在为每一帧执行所有这些步骤,因此请务必删除您发现不必要的任何内容。由于此代码会经常运行,因此即使是很小的优化,您也会看到巨大的性能优势,因此值得花一些时间进行优化。

【讨论】:

  • 感谢您的回复。我的问题是从相机而不是图像中检测方块。
  • 就OpenCV而言,相机输入只是一系列单独的图像。图像来自实时​​摄像机而不是视频文件或图像目录这一事实无关紧要。
猜你喜欢
  • 2017-12-24
  • 2012-05-18
  • 1970-01-01
  • 2015-12-11
  • 2014-01-31
  • 2014-12-14
  • 1970-01-01
  • 2018-12-07
  • 2011-07-11
相关资源
最近更新 更多