gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:46:26 26 4
gpt4 key购买 nike

我们正在编写一个程序,它从网络摄像头获取输入,减去除绿色值之外的所有颜色,找到分离的 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/

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