1

我正在尝试使用 OpenCV 获取多张图片的标准偏差,这就是我所做的:

#include #include #include

 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;

}

当我看到我得到的结果帧“平均值”是正确的,但标准偏差是错误的。知道我做错了什么,在此先感谢您的帮助

4

2 回答 2

1

您没有累积差异平方图像的结果来计算标准偏差。您当前代码的结果是只有最后一张图像被平方并除以图像总数。之前的所有计算都没有效果。

此外,除以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;
于 2013-02-14T11:35:44.117 回答
0

在@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;
 }
于 2013-02-14T14:45:30.707 回答