Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everyone, after compiling the source code I got zero variance. this result true or no?
thank you in advance

What I have tried:

C++
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/core/mat.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include<iostream>
#include<math.h>
#include<time.h>
using namespace std;
using namespace cv;

int main(int argc, char *argv[])
{
system("clear");
Mat src=imread("/home/Adam/Bureau/2.jpg",0);
float mean,s,v,e;
int a=src.rows, b=src.cols;
time_t t;
srand((unsigned) time(&t));
for(int i=0;i<src.rows;i++){
for(int j=0;i<src.cols;j++){ s+=src.at<float>(i,j);
mean=s/(a*b);
e+=(src.at<float>(i,j)-mean)*(src.at<float>(i,j)-mean);
v=e/(a*b);
}
}
cout<<"the mean ="<<mean<<endl;
cout<<"la variance est ="<<v<<endl;
return 0;
}
Posted
Updated 7-Nov-17 15:31pm
v3

Anyway your variance computation is incorrect.
You have to first compute the mean value and then use the computed mean value in order to compute the variance. That is you need two loops.
 
Share this answer
 
Comments
saidinsan000000000 7-Nov-17 15:55pm    
thank you
CPallini 7-Nov-17 16:39pm    
You are welcome.
At first you should initialize every variable as mean to e with 0. The value from the access to src.at<float>(i,j) should be stored in a var.

Your program logic looks strange. On every i and j interation you change the mean. I think you should loop over all pixels and sum up to build the mean. Than loop for the variance.

Read the basics about variance to proof that your calculation is what you want.
 
Share this answer
 
Comments
saidinsan000000000 7-Nov-17 15:55pm    
thank you
The mean value used by your code within the loops for calculating the variance is not the final one but the one for the processed items so far. You have also not initialised the sum variables s and e with zero.

You have to caluclate the mean first and then use another iteration to calculate the variance:
// Use double to reduce inaccuray and initialise start value
double mean = 0;
for (int i = 0; i < src.rows; i++)
{
    for (int j = 0; j < src.cols; j++)
        mean += src.at<uchar>(i, j);
}
mean /= a * b;

// Calculate variance now using the final mean
double v = 0;
for (int i = 0; i < src.rows; i++)
{
    for (int j = 0; j < src.cols; j++)
        v += (src.at<uchar>(i, j) - mean) * (src.at<uchar>(i, j) - mean);
}
// Variance
v /= a * b;
// Sample variance
//v /= (a * b) - 1;

[EDIT]
There is another problem:
You use src.at<float>(i, j) to get the pixel values from the image. But you have passed 0 as second parameter to imread which is IMREAD_GRAYSCALE. Then the matrix contains uchar values for each pixel defining the intensity. That type must be used when accessing pixels.
[/EDIT]
 
Share this answer
 
v3
Comments
saidinsan000000000 7-Nov-17 15:54pm    
thank you for your answer but this code gives no result
Jochen Arndt 7-Nov-17 16:02pm    
What do you mean by "no result"?
mean and v will contain a value which is the result. I have not tested it. So it might give not the correct result (but it should).
saidinsan000000000 7-Nov-17 17:24pm    
I mean it gives a no-true result
saidinsan000000000 7-Nov-17 17:45pm    
thanks for your reaction, after the compilation I get "the mean equal to -nan!!!!
Jochen Arndt 7-Nov-17 18:06pm    
That is probably because there is another error in your code which I have copy and pasted. The line(s)
Hide   Copy Code
for (int j = 0; i < src.cols; j++)

must have 'j' for the end condition:
Hide   Copy Code
for (int j = 0; j < src.cols; j++)

I will correct it in my answer.

However, that error should result in an infinite loop which can only be stopped by an access violation.

I also did not know OpenCV good enough to know if using
Mat::at<double>(row, col);<pre>
is correct. You might check that and use <float> if that would be correct (assigning / adding a float to a double needs no cast).
Learn to indent properly your code, it show its structure and it helps reading and understanding.
C++
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/core/mat.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include<iostream>
#include<math.h>
#include<time.h>
using namespace std;
using namespace cv;

int main(int argc, char *argv[])
{
  system("clear");
  Mat src=imread("/home/Adam/Bureau/2.jpg",0);
  float mean,s,v,e;
  int a=src.rows, b=src.cols;
  time_t t;
  srand((unsigned) time(&t));
  for(int i=0;i<src.rows;i++){
    for(int j=0;i<src.cols;j++){ // do not pack many things on same line of code
       s+=src.at<float>(i,j);    // it just make it more difficult to read.
      mean=s/(a*b);
      e+=(src.at<float>(i,j)-mean)*(src.at<float>(i,j)-mean);
      v=e/(a*b);
    }
  }
  cout<<"the mean ="<<mean<<endl;
  cout<<"la variance est ="<<v<<endl;
  return 0;
}

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]

you need to read about C++ division of integers

When you don't understand what you code is doing or its results, the answer is debuggern it allow you to see exactly what your code is doing and to inspect the variables is it execute.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900