Visual c++ 取图像平均值进行背景减法-结果不正确
当我尝试将存储在列表中的N个前帧相加,然后除以num frames时,生成的背景模型与预期不符。我能说出来,因为我之前在同一个视频中尝试过Matlab中的算法Visual c++ 取图像平均值进行背景减法-结果不正确,visual-c++,opencv,image-processing,computer-vision,background-subtraction,Visual C++,Opencv,Image Processing,Computer Vision,Background Subtraction,当我尝试将存储在列表中的N个前帧相加,然后除以num frames时,生成的背景模型与预期不符。我能说出来,因为我之前在同一个视频中尝试过Matlab中的算法 class TemporalMeanFilter { private: int learningCount; list<Mat> history; int height, width; Mat buildModel(){ if(history.size() == 0)
class TemporalMeanFilter {
private:
int learningCount;
list<Mat> history;
int height, width;
Mat buildModel(){
if(history.size() == 0)
return Mat();
Mat image_avg(height, width, CV_8U, Scalar(0));
double alpha = (1.0/history.size());
list<Mat>::iterator it = history.begin();
cout << "History size: " << history.size() << " Weight per cell: " << alpha << endl;
while(it != history.end()){
image_avg += (*it * alpha);
it++;
}
return image_avg;
}
public:
TemporalMeanFilter(int height, int width, int learningCount){
this->learningCount = learningCount;
this->height = height;
this->width = width;
}
void applyFrameDifference(Mat& frame_diff, Mat& bg_model, Mat& input_frame){
if(history.size() == learningCount)
history.pop_front();
history.push_back(input_frame);
bg_model = buildModel();
frame_diff = bg_model - input_frame;
}
};
//。。。在屏幕上显示。。。程序结束
图片:
左边是bg_模型,中间是curr_帧,右边是输出
也许是因为CV_U8的四舍五入?我尝试更改为CV_32FC1,但由于某种原因,程序无法添加两个CV_32FC1矩阵,因此程序崩溃
如有任何见解,将不胜感激。谢谢
更多信息:
在课堂上,我现在将平均值保存在CV_16UC1垫子中,以防止剪切,以及在连续添加后如何导致错误
add函数/运算符+将结果类型从CV_16UC1更改为CV8UC1。这个错误是由那个引起的。有没有建议如何要求它保留原始数据类型?(附言:我礼貌地问……没用)
你是对的,几乎可以肯定,这是通过累积缩放灰度值得到的舍入误差。但是,没有理由使用浮点像素使其崩溃,因此您应该尝试以下方法:
Mat buildModel()
{
if (history.size() == 0)
return Mat();
Mat image_avg = Mat::zeros(height, width, CV_32FC1);
double alpha = (1.0 / history.size());
list<Mat>::iterator it = history.begin();
while (it != history.end())
{
Mat image_temp;
(*it).convertTo(image_temp, CV_32FC1);
image_avg += image_temp;
it++;
}
image_avg *= alpha;
return image_avg;
}
matbuildmodel()
{
if(history.size()==0)
返回垫();
Mat image_avg=Mat::零(高度、宽度、CV_32FC1);
double alpha=(1.0/history.size());
list::iterator it=history.begin();
while(it!=history.end())
{
Mat图像温度;
(*it).转换到(图像温度,CV_32FC1);
图像平均值+=图像温度;
it++;
}
图像_平均值*=α;
返回图像_平均值;
}
根据您想要对结果执行的操作,您可能需要对其进行规格化或重新缩放,或在显示前将其转换回灰度等。谢谢您的回复。事实上,我现在知道这个问题是由于剪辑造成的。但是,问题不在于背景模型看起来与输入帧相同。我不明白为什么不能正确计算平均值。
background_model += *it;
OpenCV Error: Bad argument (When the input arrays in add/subtract/multiply/divid
e functions have different types, the output array type must be explicitly speci
fied) in unknown function, file C:\buildslave64\win64_amdocl\2_4_PackSlave-win32
-vc11-shared\opencv\modules\core\src\arithm.cpp, line 1313
Mat buildModel()
{
if (history.size() == 0)
return Mat();
Mat image_avg = Mat::zeros(height, width, CV_32FC1);
double alpha = (1.0 / history.size());
list<Mat>::iterator it = history.begin();
while (it != history.end())
{
Mat image_temp;
(*it).convertTo(image_temp, CV_32FC1);
image_avg += image_temp;
it++;
}
image_avg *= alpha;
return image_avg;
}