Ok, you pasted that same question on April 20. It would have been better to ask your follow-up question by using the "Improve Question" button to the original one. See my answer there, which is Solution 2, which does the entire job in just one line.
If you want to do the operation by, just for exercise, here are a couple of hints regarding your code above:
- You first convert your original image (named "image") to a matrix (named "mat"), I guess just for accessing the pixel values. You can access the pixels of an image directly in OpenCV, so that step is not necessary.
- Your image seems to have 3 channels, in other words, it's a color image. Was that intended? That is probably where you are getting stuck.
- I would reverse the rolls of x an y, because now x is row number and y is column number, which is just the opposite way that you normally would define x and y.
- The cvmGet call must fail, because your matrix has three channels per cell. cvmGet can only operate on single channel matrices as it returns a single value of type double.
- If you have a 3-channel image, you need to put another loop inside the innermost one that loops over all three channels and the calculate the mean value for each of these channels.
I would access the pixel of a 3-channel image like this:
int step = image->widthStep;
int channels = image->nChannels;
uchar* data = (uchar*) image->imageData;
blueValue = data[y*step+x*channels+0];
greenValue = data[y*step+x*channels+1];
redValue = data[y*step+x*channels+2];
Hope that gets you a step further.