average RGB values from list of pixels.

2 views (last 30 days)
Devdolly Saini
Devdolly Saini on 11 Sep 2019
Commented: Rik on 13 Sep 2019
how do you average the RGB values from a list of pixels through the use of a function where the input is a 3d array and the outputs give the average values of the red, green and blue pixels?
  3 Comments
Devdolly Saini
Devdolly Saini on 11 Sep 2019
Edited: Devdolly Saini on 11 Sep 2019
sorry the input is a 1xnx3 3d array not a 1d array
Rik
Rik on 13 Sep 2019
Please stop flagging questions and comments that do not require attention from site admins. See this page for more information about flagging.

Sign in to comment.

Answers (1)

Adam Danz
Adam Danz on 11 Sep 2019
Edited: Adam Danz on 11 Sep 2019
If x is your 1xnx3 array, meanRGB will be a 1x3 vector of the average rgb values.
meanRGB = mean(squeeze(x),1);
Here's the function form, using median
MedianPixel = @(pixels)median(squeeze(pixels),1);
Test it
pixels(:,:,1) = [54 50 45];
pixels(:,:,2) = [48,52,43];
pixels(:,:,3) = [50,41,47];
MedianPixel(pixels)
% Result
% ans =
% 50 48 47
Here is another version that splits the outputs if needed (it's unnecessary but it's what's described in the document).
function [R,G,B] = MedianPixels(pixels)
R = median(pixels(:,:,1));
G = median(pixels(:,:,2));
B = median(pixels(:,:,3));
end

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!