1

I am trying to average the pixel value of an image across a circular region of interest. I am trying to use cvAvgSdv and it returns a sensible mean, but zero standard deviation. I also try cvCountNonZero which also returns zero. Must be a basic mistake I am making but hope someone can point it out. The roi.png looks correct (a white spot in the correct place on a black background). My full code is below (VS C++ 2010 Express, Opencv245):

#include <opencv/cv.h>
#include <opencv2/highgui/highgui.hpp>
#include <stdio.h>

int main( int argc, char** argv )
{
IplImage* src;
if( argc >= 2 && (src=cvLoadImage(argv[1], 0))!= 0)
{
    IplImage * roi;
    roi = cvCreateImage(cvGetSize(src), 8, 1);

    int n;
    int centre_x = 396, centre_y = 317;
    CvScalar pixel_mean, pixel_stdev;

    cvZero(roi);
    cvCircle(
        roi,
        cvPoint(centre_x, centre_y),
        15,  //radius<<<<<<<<<<<<<<<<<<<<<<<<<<<
        cvScalar(255),
        -1, 8, 0
    );
    cvAvgSdv(src, &pixel_mean, &pixel_stdev, roi);
    n = cvCountNonZero(roi);

    printf
    (
         "center x: %d y: %d A: %d B: %d av: %f stdev: %f n: %d\n",
         centre_x,
         centre_y,
         15,
         15,
         pixel_mean,  //new
         pixel_stdev,  //new
         n   //new
    );

    cvSaveImage("roi.png", roi, 0);
}
}

Thanks

user1228123
  • 424
  • 4
  • 15
  • Not a dicky bird on this. Oh well, I worked it out in the end. The mistake was in the printf. It is necessary to explicitly select which parts of the cvScalars to output. Add ".val[0]" to the end of pixel_mean and pixel_stdev and it works. – user1228123 May 08 '14 at 08:32

1 Answers1

1

Mistake in the printf and use of cvScalars. Use pixel_mean.val[0] and pixel_stdev.val[0] in place of pixel_mean and pixel_stdev. That section of code becomes:

printf
(
     "center x: %d y: %d A: %d B: %d av: %f stdev: %f n: %d\n",
     centre_x,
     centre_y,
     15,
     15,
     pixel_mean.val[0],  //new
     pixel_stdev.val[0],  //new
     n   //new
);

A safer approach is to printf each variable independently, then it doesn't matter if you forget the .val[0] part:

printf
(
     "center x: %d y: %d A: %d B: %d",
     centre_x,
     centre_y,
     15,
     15
);
printf(" av: %f", pixel_mean.val[0]);  //new
printf(" stdev: %f", pixel_stdev.val[0]);  //new
printf(" n: %d\n", n);   //new

Looks a bit cumbersome, but it doesn't matter if the .val[0] are forgotten, so is much safer.

user1228123
  • 424
  • 4
  • 15