OpenCV和C++中的检测正方形

3
嗨,我正在从相机使用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);
                }
        }
    }
}

}


你能展示一些代码吗?你是在使用提供的源代码中的squares示例工作吗? - foundry
这里有一个问题,请问有什么建议吗?我知道它是针对图像的,但如何稍微改变一下,就可以让它适用于相机。 - user1959079
2个回答

3

如果您不介意牺牲准确性,可以加快速度。例如:

// 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 这并不是绝对必要的。在轮廓提取阶段之前,使用简单的阈值处理可能会得到足够好的结果。 - Daniel Martín
@user1959079 看看我的更新,尝试一下不用Canny,这对我有用。还有其他参数可以调整来补偿。 - foundry
你说得对。我已经尝试过没有使用Canny算法,结果并不像我想象的那么糟糕,并且速度更快了。非常感谢你。 - user1959079
@user1959079,如果它有效,请将我的答案标记为“已接受”(只需单击复选框);-j - foundry
@foundry: 我也需要使用类似的代码,但我确实需要具有强大的正方形检测功能。速度对我来说也是个问题...因此我想使用您的建议和代码,但是if (l == 0 && use_canny)这句话的含义是什么? - skm

1
计算机视觉的一个好的经验法则是在进行任何密集处理之前将图像转换为灰度。仅在绝对必要时才循环遍历颜色通道。我建议使用以下模式进行对象识别:
  1. 将图像转换为灰度
  2. 将灰度图像过滤为更简单的格式(canny、阈值、边缘检测)
  3. 进行重型处理(检测正方形形状)
  4. 使用处理后的值重构原始图像(绘制/存储正方形)
请记住,您需要为每个帧执行所有这些步骤,因此一定要删除您发现不必要的内容。由于此代码将如此频繁地运行,即使进行轻微优化也会获得巨大的性能优势,因此值得花费一些时间进行优化。

1
感谢您的回复。我的问题是如何从相机中检测正方形,而不是从图片中检测。 - user1959079
就OpenCV而言,相机输入只是一系列单独的图像。这些图像来自实时相机而不是视频文件或图像目录,这一点并不重要。 - Jestin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接