C++ 利用Opencv获取多幅图像的std偏差
我正在尝试使用OpenCV获取多张图片的标准偏差,以下是我所做的: #包括 #包括 #包括C++ 利用Opencv获取多幅图像的std偏差,c++,opencv,image-processing,C++,Opencv,Image Processing,我正在尝试使用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 resultfra
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;
使用名称空间std;
使用名称空间cv;
int main(){
cv::垫框,框32F;
字符文件名[40];
平均值;
常数int计数=134;
const int width=1920;
const int height=1080;
cv::Mat resultframe=cv::Mat::零(高度、宽度、cv_32FC3);
cv::Mat deviationframe=cv::Mat::zeros(高度、宽度、cv_32FC3);
cv::Mat temp=cv::Mat::Zero(高度、宽度、cv_32FC3);
对于(int i=1;i您没有将差平方图像的结果进行累加来计算标准偏差。您当前代码的结果是,只有最后一个图像被平方并除以图像总数。之前的所有计算均无效
此外,除以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;
。
.
.
结果帧*=(1.0/计数);
cv::Mat偏差结果=cv::Mat::零(高度、宽度、cv_32FC3);
对于(int j=1;j
您没有将差分平方图像的结果累加以计算标准偏差。当前代码的结果是,只有最后一个图像被平方并除以图像总数。之前的所有计算都无效
此外,除以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;
。
.
.
结果帧*=(1.0/计数);
cv::Mat偏差结果=cv::Mat::零(高度、宽度、cv_32FC3);
对于(int j=1;j
在@sgar91的帮助之后,这里是最后的代码,我希望它能帮助任何人
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\opencv.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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
使用名称空间cv;
int main(){
cv::垫框,框32F;
字符文件名[40];
平均值;
常数int计数=134;
const int width=1920;
const int height=1080;
cv::Mat resultframe=cv::Mat::零(高度、宽度、cv_32FC3);
cv::Mat deviationframe=cv::Mat::zeros(高度、宽度、cv_32FC3);
cv::Mat temp=cv::Mat::Zero(高度、宽度、cv_32FC3);
对于@sgar91的帮助之后的(inti=1;i),这里是最后的代码,我希望它能帮助任何人
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\opencv.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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
使用名称空间cv;
int main(){
cv::垫框,框32F;
字符文件名[40];
平均值;
常数int计数=134;
const int width=1920;
const int height=1080;
cv::Mat resultframe=cv::Mat::零(高度、宽度、cv_32FC3);
cv::Mat deviationframe=cv::Mat::zeros(高度、宽度、cv_32FC3);
cv::Mat temp=cv::Mat::Zero(高度、宽度、cv_32FC3);
对于(int i=1;i@sgar91)谢谢你的回答,这是有道理的,但它并没有改变我所说的结果get@Engine哦!我明白了。你没有累加差平方图像。当前代码只是在达到deviationframe=(frame32f-resultframe)时覆盖了以前的值
。查看我的更新答案。再次感谢您的帮助,这是原因之一,但现在当我将偏差框设置为255时,我得到了黑色框我编辑了我的问题我得到了它知道我必须在对其运行sqrt()之前对其进行偏差,或者仅在15,96之前对其进行偏差…这就是sqrt()255@sgar91谢谢你的回答,这是有道理的,但它并没有改变我所说的结果get@Engine哦!我明白了。你没有累加差平方图像。当前代码只是在达到deviationframe=(frame32f-resultframe)时覆盖了以前的值
。请参阅我的更新答案。再次感谢您的帮助,这是原因之一,但现在当我将偏差框设置为255时,我得到了黑色框我编辑了我的问题我得到了它知道我必须在对其运行sqrt()之前将其设置为偏差,或者仅将其设置为15,96….这是255的sqrt()