如何在图像中循环查找轮廓?OpenCV 2.3

3
我们正在编写一个程序,从网络摄像头中获取输入,除了绿色值之外,减去所有颜色,找到分离的 BLOBs 并对它们进行编号。最终,这将被用作视频游戏的输入,但现在这并不重要。
所涉及的代码并不是实际执行所有这些操作的代码,而是重写的代码段,以测试 FindContours 如何工作。通常,在图像处理中,我们被教导图像从左上角向右下角读取,但经过一些测试,似乎它恰好相反,从右下角开始移动到左上角!
因此,问题是:FindContours 以什么顺序查找其轮廓?我的假设是正确的还是我的自己的代码让我感到困惑?
输入:Blobtest06 “组件”窗口: "组件" 窗口 控制台:控制台
#include <opencv2/opencv.hpp>
#include <iostream>
#include <opencv2/core/mat.hpp>
#include <Windows.h> //for sleep function

using namespace cv;
using namespace std;

void IsolateGreen(Mat mIn, Mat& mOut)
{
Mat inImg (mIn.rows, mIn.cols, CV_8UC3, Scalar(1,2,3));
inImg.data = mIn.data;
Mat channelRed (inImg.rows, inImg.cols, CV_8UC1);
Mat channelGreen (inImg.rows, inImg.cols, CV_8UC1);
Mat channelBlue (inImg.rows, inImg.cols, CV_8UC1);
Mat outImg[] = {channelRed, channelGreen, channelBlue};

int fromTo[] = { 0,2, 1,1, 2,0};
mixChannels( &inImg, 1, outImg, 3, fromTo, 3);

mOut = (channelGreen) - (channelRed + channelBlue);

threshold(mOut, mOut, 5, 255, THRESH_BINARY);

erode(mOut, mOut, Mat(), Point (-1,-1), 1);
dilate(mOut, mOut, Mat(), Point(-1,-1), 2);

}

void FindContours(Mat& mDst, Mat mGreenScale, vector<vector<Point>>& vecContours, vector<Vec4i>& vecHierarchy, Mat img)
{
//This is empty at all times. We need it to avoid crashes.
vector<Vec4i> vecHierarchy2; 

// mGreenScale = mGreenScale > 1; //// MIGHT be entirely unneeded 

mDst = img > 1;
findContours( mGreenScale, vecContours, vecHierarchy,
        CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );

/* Colors, in order:
1st. = Red
2nd. = Dark red
3rd. = Purple
4th. = Blue
5th. = Baby blue
6th. = Green
7th. = Olive green
8th. = Dark green
*/

int aRed[] = {255, 100, 128, 0, 191, 0, 202, 0};
int aGreen[] = {0, 0, 0, 0, 239, 255, 255, 100};
int aBlue[] = {0, 0, 128, 255, 255, 0, 112, 0};
string sColor[] = {"Red", "Dark red", "Purple", "Blue", "Baby blue", "Green", "Light green", "Dark green"};

//its important that we check if there is anything in vecHierarchy (else) {crash} :P
//function drawContours cannot handle an empty vedHierarchy
if (vecHierarchy != vecHierarchy2)
{
    // iterate through all the top-level contours,
    for(int idx = 0; idx >= 0; idx = vecHierarchy[idx][0] )
    { 
        // draw each connected component with its own FIXED color
        Scalar color( aBlue[idx], aGreen[idx], aRed[idx] );
        drawContours( mDst, vecContours, idx, color, /*1*/ CV_FILLED, 8, vecHierarchy );
        cout << vecContours[idx][0] << " - - " << sColor[idx] << " - - Index: " << idx << endl;
    }
}
cout << "Objects: ";
cout << vecContours.size();
cout << endl;

}




int main()
    {
    Mat img = imread("Blobtest06.png");
    Mat mGreenScale;

    //These next 5 instances ties to contourfinding                 
    cvtColor(img, mGreenScale, CV_8UC3); //sets the right rows and cols
    vector<vector<Point>> vecContours; //points to each pixel in a contour
    vector<Vec4i> vecHierarchy; //A hierarchy for the functions
    Mat mDst = Mat::zeros(mGreenScale.rows, mGreenScale.cols, CV_8UC3); //mDst image


    IsolateGreen(img, mGreenScale);
    FindContours(mDst, mGreenScale, vecContours, vecHierarchy, img);

    namedWindow( "Components", 1 );
    imshow( "Components", mDst );
    namedWindow( "Source", 1 );
    imshow( "Source", mGreenScale );

    waitKey();  
    return 0;
    } 

注:抱歉语法很糟。这个网站有些难以应对,而且快到午饭时间了。

1个回答

5
如果您关心OpenCV的实现细节(顺便说一下,这是一个开源库),您可以随时下载源代码并自行阅读。 注意:C++ API在某些方面使用了C API,包括FindCountours()。因此,如果您查看文件:modules/imgproc/src/contours.cpp的第1472行,您将看到该函数的C++实现:
1472 void cv::findContours( InputOutputArray _image, OutputArrayOfArrays _contours,
1473                    OutputArray _hierarchy, int mode, int method, Point offset )
1474 {
1475     Mat image = _image.getMat();
1476     MemStorage storage(cvCreateMemStorage());
1477     CvMat _cimage = image;
1478     CvSeq* _ccontours = 0;
1479     if( _hierarchy.needed() )
1480         _hierarchy.clear();
1481     cvFindContours(&_cimage, storage, &_ccontours, sizeof(CvContour), mode, method, offset);
1482     if( !_ccontours )
1483     {
1484         _contours.clear();
1485         return;
1486     }

需要调用C API中定义的cvFindContours()函数,该函数在同一文件的第1424行中定义。

cvFindNextContour()位于794行,描述了扫描过程本身:

793 CvSeq *
794 cvFindNextContour( CvContourScanner scanner )
795 {

你可以清楚地看到:

824     for( ; y < height; y++, img += step )
825     {
826         for( ; x < width; x++ )
827         {

谢谢你的回答 - 但是!我好像无法完全理解这个答案。我可以看到数学的计算方式,但它并没有给出任何方向的见解。它从哪个像素开始,又在哪里结束呢?顺便说一下,我是一个初学者,请多包涵 :) - David Skødt Lauritsen

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