Hello,
I would like to confirm if I have the correct way of calculating the mean and standard deviation of an image. I am not sure I have the right answers. Please can someone please confirm this.
Also, I would like to ask if anyone knows how I can obtain pixel from my image. I am using OpenCV's cvLoadImage function to load the image. I am not then exactly sure how to obtain each pixel of the image using first principles. I don't want to use the Opencv function or any other library again for that.
Can someone assist me?
#include <cstdlib>
#include <stdio.h>
#include <iostream>
#include <cmath>
#include "C:\OpenCV2.1\include\opencv\cv.h"
#include "C:\OpenCV2.1\include\opencv\cvaux.h"
#include "C:\OpenCV2.1\include\opencv\cxcore.h"
#include "C:\OpenCV2.1\include\opencv\ml.h"
#include "C:\OpenCV2.1\include\opencv\cxtypes.h"
#include "C:\OpenCV2.1\include\opencv\highgui.h"
using namespace std;
typedef unsigned char uchar;
int main (int argc, char *argv[])
{
IplImage *image;
int height, width;
uchar** ImageData;
uchar* data;
double mean, std_dev;
cvNamedWindow("A1", 1);
image = cvLoadImage(argv[1],1);
//Calculate the height and width of the image
height = image->height;
width = image->width;
cout << "This image has " << width << " by " << height << " pixels." << endl;
cvGetRawData(image, (uchar**)&data);
ImageData = new uchar* [height];
for(int i = 0; i < height; i++)
{
ImageData[i] = new uchar [width];
}
//Calculate the mean of the image
double total = 0;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
//total += ImageData[i][j];
total += data[width*image->width + height];
}
}
mean = (total/ (height * width));
cout << "Mean: " << mean << endl;
//Calculate the standard deviation of the image
double var = 0;
for (int a = 0; a < height; a++)
{
for (int b = 0; b < width; b++)
{
var += ((ImageData[a][b] - mean) * (ImageData[a][b] - mean));
}
}
var /= (height * width);
std_dev = sqrt(var);
cout << "Standard Deviation: " << std_dev << endl;
cvShowImage("A1", image);
cvWaitKey(0);
cvReleaseImage(&image);
cvDestroyWindow("A1");
return 0;
}