Question about finding mean in a matrix

I am currently trying to figure out how to take a mean of an NxMxKxW matrix. I would like the resulting matrix to be a matrix of means of size NxMxK. The idea is, I have a collection of images and I would like to take the average of each pixel in each color channel and come up with a new color channel that contains those averages.
I tried doing something like this...
average = mean(image_collection,4);
Although I don't think this is quite right.
I included an image to show the idea I am trying to do.

2 commentaires

Matt J
Matt J le 14 Nov 2022
Although I don't think this is quite right.
It is.
Edward Baker
Edward Baker le 14 Nov 2022
Alrighty then, I guess that solves it. I just wasn't sure if I was interpreting the documentation correctly. Thank you!

Connectez-vous pour commenter.

Réponses (1)

Image Analyst
Image Analyst le 14 Nov 2022

0 votes

Is this a video? If so do you want to average the different color channels together, or have one average image for each separate color channel?
See attached demo to see how to compute the average movie frame. The result is a color image where each color channel is the mean of all corresponding color channels in the video.

8 commentaires

Edward Baker
Edward Baker le 14 Nov 2022
It is not a video, and I would like to average the color channels together. Thank you!
DGM
DGM le 14 Nov 2022
In your question, what you describe is taking the dim4 mean of a multiframe image.
What convention are you using to represent your imagestack? I think most people here would assume that a multiframe image is rows x columns x channels x frames. In other contexts (e.g. GMIC), the convention is rows x columns x frames x channels. Given the convention that I'd be using, the dim4 mean is the mean of frames, not the mean of channels.
Edward Baker
Edward Baker le 14 Nov 2022
The convention I am using is rows x columns x channels x frames
Can you make a small example that proves
average = mean(image_collection, 4);
does not give you the correct average? I believe it should give you the average frame, a rows by columns by 3 RGB image.
DGM
DGM le 14 Nov 2022
Modifié(e) : DGM le 14 Nov 2022
If your image is integer-class and you use mean() on it, the result will be improperly-scaled floating point, causing display/saving issues. That's my guess.
A = imread('peppers.png');
Astack = repmat(A,[1 1 1 4]); % a 4-frame RGB image
framemean = mean(Astack,4);
imshow(framemean) % result is all white
class(framemean) % result is class 'double'
ans = 'double'
[min(framemean(:)) max(framemean(:))] % but it retains the scale of the original class
ans = 1×2
0 255
framemean = cast(framemean,class(Astack)); % so cast it back to the original class
imshow(framemean) % now it will work
Edward Baker
Edward Baker le 14 Nov 2022
I believe it is correct, I made a mistake when checking my work. Thank you for the help.
Another way, that I always use to show floating point images is to use [] in imshow():
imshow(framemean, []) % now it will work
If you do that you don't need to call cast and the variable remains of type double.
DGM
DGM le 14 Nov 2022
The displayrange parameter (i.e. []) controls the map scaling, so it only applies if the image is 2D.
Either way, one needs to maintain awareness of the class, range, and depth of the images they're working with and handle them accordingly.

Connectez-vous pour commenter.

Commenté :

DGM
le 14 Nov 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by