我们正在编写一个程序,从网络摄像头中获取输入,除了绿色值之外,减去所有颜色,找到分离的 BLOBs 并对它们进行编号。最终,这将被用作视频游戏的输入,但现在这并不重要。
所涉及的代码并不是实际执行所有这些操作的代码,而是重写的代码段,以测试 FindContours 如何工作。通常,在图像处理中,我们被教导图像从左上角向右下角读取,但经过一些测试,似乎它恰好相反,从右下角开始移动到左上角!
因此,问题是:FindContours 以什么顺序查找其轮廓?我的假设是正确的还是我的自己的代码让我感到困惑?
输入:Blobtest06 “组件”窗口: "组件" 窗口 控制台:控制台
所涉及的代码并不是实际执行所有这些操作的代码,而是重写的代码段,以测试 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;
}
注:抱歉语法很糟。这个网站有些难以应对,而且快到午饭时间了。