c++ - FindContours 如何循环遍历图像? OpenCV 2.3

标签 c++ visual-studio-2010 opencv image-processing

我们正在编写一个程序,它从网络摄像头获取输入,减去除绿色值之外的所有颜色,找到分离的 BLOB 并计算它们。最终,这将用作视频游戏的输入,但现在这无关紧要。

有问题的代码并不是实际执行所有这些操作的代码,而是一段重写的代码,用于测试 FindContours 的实际工作原理。 通常,对于图像处理,我们一直认为图像是从左上角到右下角读取的,但经过一些测试,它似乎恰恰相反,从右下角开始移动到左上角!

所以这里的问题是:FindContours 按什么顺序找到它的 Contours?我的假设是正确的还是我自己的代码让我感到困惑?

输入:Blobtest06

"Components" window

Console

#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;
    } 

PS:抱歉语法太糟糕了。该网站很难,而且刚好是午餐时间。

最佳答案

如果您关心 OpenCV 的实现细节,顺便说一句,它是一个开源库,您可以随时 download the source并自己阅读。

警告:C++ API 将 C API 用于某些事情,包括 FindCountours()。因此,如果您检查文件:modules/imgproc/src/contours.cpp line 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         {

关于c++ - FindContours 如何循环遍历图像? OpenCV 2.3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8166024/

相关文章:

opencv - 什么 cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC1);

opencv - 如何在不使用 opencv 中的 fitEllipse() 的情况下检测图像中的椭圆?

c++ - 如何在 Boost Unit 中正确定义派生单位

c++ - Eclipse c++ makefile 项目输出

c++ - 为什么 argc 不是常量?

c++ - 使用 C++ 可变参数模板参数时遇到问题

visual-studio - 如何使Visual Studio 2010突出显示变量实例

visual-studio-2010 - 使用 MSBuild 和 CruiseControl .NET 构建和部署 VS2010 数据库项目

c++ - 令人困惑的缺少类型说明符错误

python - 我无法安装Opencv