65,208
社区成员
发帖
与我相关
我的任务
分享if(!Frame.empty())
{
learningFrameNum++;
if(learningFrameNum < 100)
{
gaussBGModel.updateBG(learningFrameNum,Frame,0.01f);
gaussBGModel.getBGImage(bg);
//printf("%f %f\r\n",352, 288);
Vec3b v = bg(40,150);
printf("frameNum = %d r = %d g = %d b = %d\r\n",learningFrameNum,v[0],v[1],v[2]);
GaussModel gm = gaussBGModel.gmm[40*352+150];
for ( int i = 0;i < gm.currModeNum; i++)
{
printf("frameNum = %d weight[%d] = %f variance[%d] = %f r = %f g = %f b = %f\r\n", learningFrameNum,i,gm.weight[i],i,gm.variance[i],gm.mean[i][0],gm.mean[i][1],gm.mean[i][2]);
}
}
else
{
gaussBGModel.detectFG(learningFrameNum,Frame,grayMask);
namedWindow("fg",0);
imshow("fg",grayMask);
waitKey(1);
morphologyEx(grayMask,grayMask,CV_MOP_OPEN,Mat(),Point(-1,-1),2);
morphologyEx(grayMask,grayMask,CV_MOP_CLOSE,Mat(),Point(-1,-1),2);
vector<vector<Point> > currContours;
//currContours.resize( 2000);
findContours(grayMask, currContours, cv::noArray(), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
if(currContours.size() <= 0)
{
//continue;
}
threshold(grayMask,grayMask,0,255,CV_THRESH_BINARY);
grayUpdateRectMask.setTo(0);
for(unsigned i=0; i < currContours.size(); i++)
{
Rect rect = boundingRect(currContours[i]);
rectangle(grayUpdateRectMask,rect,Scalar(255),CV_FILLED);
}
gaussBGModel.updateBG(learningFrameNum,Frame,0.01f,grayUpdateRectMask);
}
//namedWindow("grayUpdateRectMask",0);
// imshow("grayUpdateRectMask",grayUpdateRectMask);
// waitKey(1);
//string name =( _T("video"));
namedWindow("video",0);
imshow("video",Frame);
waitKey(1);
//namedWindow("grayMask",0);
// imshow("grayMask",grayMask);
// waitKey(1);
gaussBGModel.getBGImage(bg);
namedWindow( "bg",0);
imshow("bg",bg);
waitKey(1);
//if(waitKey(5) == 27)
{
//break;
}
}#include "gaussbgmodel.h"
#include "stdafx.h"
GaussBGModel::GaussBGModel()
//{
//}
GaussBGModel::~GaussBGModel()
//{
//
//}
void GaussBGModel::prepare(int Rows, int Cols)
{
mRows = Rows;
mCols = Cols;
// 创建背景模型
gmm.clear();
gmm.resize(mRows * mCols);
}
void GaussBGModel::detectFG(int FrameNum, const Mat3b &CurrFrame, Mat1b &GrayFGMask)
{
// 预处理掩码矩阵
Mat1b currMask = Mat1b(mRows, mCols, BGPixel);
// 遍历当前帧中的像素
for(int row = 0; row < CurrFrame.rows; row++)
{
for(int col = 0; col < CurrFrame.cols; col++)
{
// 确定该像素对应的高斯模型的下标
int index = row * CurrFrame.cols + col;
// 进行前景检测
uchar pixelValue = gmm[index].detect(CurrFrame(row, col));
// 给掩码矩阵赋值
currMask(row, col) = pixelValue;
}
}
currMask.copyTo(GrayFGMask);
}
void GaussBGModel::updateBG(int FrameNum, const Mat3b &CurrFrame, float LearningRate, InputArray BlobMask, InputArray RectMask)
{
// 更新率小于等于0.f,返回
if( LearningRate <= 0.f )
{
return;
}
// 得到前景掩码矩阵
Mat1b blobMask = BlobMask.getMat();
Mat1b rectMask = RectMask.getMat();
// 如为空,则不需要检测前景团块掩码
bool checkBlobMaskFlag = blobMask.empty()? false : true;
// 如果为空,则不需要考虑前景外接矩形掩码
bool checkRectMaskFlag = rectMask.empty()? false : true;
for( int row = 0; row < CurrFrame.rows; row++ )
{
for( int col = 0; col < CurrFrame.cols; col++ )
{
// 确定该像素对应的高斯模型的下标
int index = row * CurrFrame.cols + col;
// 对于当前像素实际分量数量小于0的、前景掩码所在像素(!=0)不做更新
if( gmm[index].currModeNum < 0 || ( checkBlobMaskFlag && blobMask(row, col) ) )
{
continue;
}
if( checkRectMaskFlag && rectMask(row,col) )
{
// 在团块外接矩形内部的,用很低的更新率
gmm[index].build(CurrFrame(row, col), 0.001f);
}
else
{
// 用全局的更新率
gmm[index].build(CurrFrame(row, col), LearningRate);
}
}
}
}
// 返回背景图像(彩色)
void GaussBGModel::getBGImage(Mat3b& BGImage) const
{
if( mRows < 1 )
{
return;
}
BGImage.create(mRows, mCols);
for(int row = 0; row < mRows; row++)
{
for(int col = 0; col < mCols; col++)
{
int index = row * mCols + col;
BGImage(row, col) = gmm[index].bgImage();
}
}
}
然后这是.h文件
#ifndef GAUSSBGMODEL_H
#define GAUSSBGMODEL_H
#include "gaussmodel.h"
using namespace std;
class GaussBGModel
{
public:
GaussBGModel();
~GaussBGModel();
/// 预处理
void prepare(int Rows, int Cols);
/// 检测前景
void detectFG(int FrameNum, const Mat3b &CurrFrame, Mat1b &GrayFGMask);
/// 更新背景模型
void updateBG(int FrameNum, const Mat3b &CurrFrame, float LearningRate = 0.01,
InputArray BlobMask = cv::noArray(), InputArray RectMask = cv::noArray());
/// 返回背景图像(彩色)
void getBGImage(Mat3b &BGImage) const;
public:
int mRows,mCols; ///< 行列数
vector<GaussModel> gmm; ///< 背景模型
};
#endif // GAUSSBGMODEL_H
~
可是运行后报这样的错误
错误 21 error C2653: “GaussBGModel”: 不是类或命名空间名称 d:\mybackup\我的文档\visual studio 2010\projects\show\show\gaussbgmodel.cpp 4
错误 22 error C2653: “GaussBGModel”: 不是类或命名空间名称 d:\mybackup\我的文档\visual studio 2010\projects\show\show\gaussbgmodel.cpp 8~
可是我头文件里已经写了类~为啥还报错呢 #if _ITERATOR_DEBUG_LEVEL == 2
if (size() <= _Pos)
{ // report error
_DEBUG_ERROR("vector subscript out of range");
_SCL_SECURE_OUT_OF_RANGE;
}
是在这边错了啊
[/quote]
堆栈定位到你自己的代码看看哪行[/quote]
vector<vector<Point> > currContours;
//currContours.resize( 2000);
findContours(grayMask, currContours, cv::noArray(), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
if(currContours.size() <= 0)
{
//continue;
}
我想这边有问题~应该是 #if _ITERATOR_DEBUG_LEVEL == 2
if (size() <= _Pos)
{ // report error
_DEBUG_ERROR("vector subscript out of range");
_SCL_SECURE_OUT_OF_RANGE;
}
是在这边错了啊
[/quote]
堆栈定位到你自己的代码看看哪行[/quote]
vector<vector<Point> > currContours; //currContours.resize( 2000); findContours(grayMask, currContours, cv::noArray(), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); if(currContours.size() <= 0) { //continue; } 这边的代码错了,可是我加了currContours.resize(2000); 还是不行啊 #if _ITERATOR_DEBUG_LEVEL == 2
if (size() <= _Pos)
{ // report error
_DEBUG_ERROR("vector subscript out of range");
_SCL_SECURE_OUT_OF_RANGE;
}
是在这边错了啊
[/quote]
堆栈定位到你自己的代码看看哪行 #if _ITERATOR_DEBUG_LEVEL == 2
if (size() <= _Pos)
{ // report error
_DEBUG_ERROR("vector subscript out of range");
_SCL_SECURE_OUT_OF_RANGE;
}
是在这边错了啊