使用Opencv获取多张图片的标准偏差
Getting the std deviation of multiple pictures using Opencv
我正在尝试使用OpenCV获得多张图片的标准偏差,我所做的是:
#包括#包括#包括
using namespace std;
using namespace cv;
int main(){
cv::Mat frame,frame32f;
char filename[40];
cv::Mat mean;
const int count =134;
const int width = 1920;
const int height = 1080;
cv::Mat resultframe = cv::Mat::zeros(height,width,CV_32FC3);
cv::Mat deviationframe = cv::Mat ::zeros(height,width,CV_32FC3);
cv::Mat temp = cv::Mat ::zeros(height,width,CV_32FC3);
for(int i = 1 ; i<= count; i++){
//int i = 3;
sprintf(filename,"d:\BMdvideos\images\image%d.tiff",i);
frame = imread(filename,CV_LOAD_IMAGE_COLOR);
frame.convertTo(frame32f,CV_32FC3 );
resultframe +=frame32f;
frame.release();
}
resultframe *= (1.0/count);
for(int j =1; j<count; j++){
sprintf(filename,"d:\BMdvideos\images\image%d.tiff",j);
frame = imread(filename,CV_LOAD_IMAGE_COLOR);
frame.convertTo(frame32f,CV_32FC3);
temp =(frame32f - resultframe);
deviationframe+= temp.mul(temp);
//temp.release();
}
imshow("devi",deviationframe); // works
deviationframe *= 1.0/(count -1);
imshow("devi2",deviationframe); // works
cv::sqrt(deviationframe,deviationframe);
resultframe *= 1.0/255.0;
imshow("devi3",deviationframe);// works
deviationframe *= 1.0/255.0;
imshow("mean ",resultframe);
imshow("deviation frame ",deviationframe);// BLACK FRAME !!!!!!!!!!!!!!!!!!!
waitKey(0);
return 0;
}
当我看到我得到的结果时,"平均值"是正确的,但标准偏差是错误的。知道我做错了什么吗,提前感谢的帮助
您没有累积差分平方图像的结果来计算标准偏差。当前代码的结果是,只有最后一个图像被平方并除以图像总数。之前的所有计算都无效。
此外,除以255
仅用于图像的可视化,而不用于实际计算。在计算标准偏差之前,你要除以255,这会使你的结果不正确。
修改您的代码如下:
.
.
.
resultframe *= (1.0/count);
cv::Mat deviationResult = cv::Mat::zeros(height,width,CV_32FC3);
for(int j =1; j< count; j++)
{
sprintf(filename,"d:\BMdvideos\images\image%d.tiff",j);
frame = imread(filename,CV_LOAD_IMAGE_COLOR);
frame.convertTo(frame32f,CV_32FC3);
deviationframe =(frame32f - resultframe);
deviationResult += deviationframe.mul(deviationframe);
}
resultframe *= (1.0/255.0);
deviationResult = deviationResult /(count -1 );
cv::sqrt(deviationResult ,deviationResult );
deviationResult *= (1.0/255.0);
imshow("mean ",resultframe);
imshow("deviation frame ",deviationResult);
waitKey(0);
return 0;
在@sgar91的帮助下,这是最后一段代码,我希望它能帮助任何
#include <opencv2corecore.hpp>
#include <opencv2highguihighgui.hpp>
#include <opencv2opencv.hpp>
#include <stdlib.h>
using namespace std;
using namespace cv;
int main(){
cv::Mat frame,frame32f;
char filename[40];
cv::Mat mean;
const int count =134;
const int width = 1920;
const int height = 1080;
cv::Mat resultframe = cv::Mat::zeros(height,width,CV_32FC3);
cv::Mat deviationframe = cv::Mat ::zeros(height,width,CV_32FC3);
cv::Mat temp = cv::Mat ::zeros(height,width,CV_32FC3);
for(int i = 1 ; i<= count; i++){
sprintf(filename,"d:\BMdvideos\images\image%d.tiff",i);
frame = imread(filename,CV_LOAD_IMAGE_COLOR);
frame.convertTo(frame32f,CV_32FC3 );
resultframe +=frame32f;
frame.release();
}
resultframe *= (1.0/count);
for(int j =1; j<count; j++){
sprintf(filename,"d:\BMdvideos\images\image%d.tiff",j);
frame = imread(filename,CV_LOAD_IMAGE_COLOR);
frame.convertTo(frame32f,CV_32FC3);
temp =(frame32f - resultframe);
deviationframe+= temp.mul(temp);
//temp.release();
}
deviationframe *= 1.0/(count -1);
deviationframe= deviationframe/255;
cv::sqrt(deviationframe,deviationframe);
resultframe *= 1.0/255.0;
imshow("mean ",resultframe);
imshow("deviation frame ",deviationframe);
waitKey(0);
return 0;
}
相关文章:
- 使用CMake检测支持的C++标准
- 如何理解C++标准N3337中的expr.const.cast子句8
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- 编译标准库类型
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- 使用Pybind11向Python公开Eigen::张量
- 编译器如何在使用SFINAE的函数和标准函数之间确定两者是否可行
- 铸造标准::有没有回到原来的类型
- 标准 N3337 5.2.10 第 7 条中的C++"类型"是什么意思?
- this_thread::sleep_for和计时时钟之间的关系是否由C++11标准指定
- 标准库类型的赋值运算符的引用限定符
- 标准是否严格定义了该程序应该如何编译?
- 如何从Windows应用程序输出到标准?
- 安全到标准:移动会员?
- 张量流错误 此文件需要编译器和库支持 ISO C++ 2011 标准
- 在 tensorflow c++ api 中,如何从张量中保存图片或数组
- 检查一张图片是否与另一张图片偏移
- 每秒捕获多张图片,并命名使用时间和帧数
- 区分 n 张图片的算法
- 使用Opencv获取多张图片的标准偏差