Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6777673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:10:33+00:00 2026-05-26T16:10:33+00:00

I have an RGB image, call it img , represented as a double array

  • 0

I have an RGB image, call it img, represented as a double array with size (100,200,3)

I have a binary mask (call it mask), that’s a logical array with size (100,200).

I want to know the mean pixel value for the masked region.
I also want to know the complete (3×3) covariance matrix for pixel values in the region.

Now, if this were a single channel (as opposed to 3 channel) image, I could simply do:

mean(img(mask(:)))
std(img(mask(:)))

It’s straight forward to do a similar operation in a loop for each channel, pulling out the values, then building up a large 3xN matrix (where N is the number of “trues” in mask and finally, operating on that matrix with mean and cov. Curious if there’s a way to do it without a loop. I’m not seeing it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:10:34+00:00Added an answer on May 26, 2026 at 4:10 pm

    Replicate your mask into the third dimension and apply it like normal. Then simply reshape the vector output into a matrix with a row for each pixel in the mask, and a column for each color channel. Here is an example using a built-in image:

    % Load image
    X = imread(('board.tif'));
    X_size = size(X);
    
    % Make mask
    mask = zeros(X_size(1:2));
    mask(250:251, 100:102) = 1;
    mask = repmat(mask, [1 1 X_size(3)]);
    mask = logical(mask);
    
    % Apply mask to image
    X_data = X(mask);
    X_data = reshape(X_data, [length(X_data)/X_size(3) X_size(3)]);
    
    % Compute stats
    mean(X_data)
    cov(double(X_data))
    

    Now, just to check, if we inspect the first pixel inside our mask in the original image X

    >> X(250, 100, :)
    
    ans(:,:,1) =
    
      144
    
    
    ans(:,:,2) =
    
      125
    
    
    ans(:,:,3) =
    
      123
    

    it should equal the first row in X_data:

    >> X_data
    
    X_data =
    
      144  125  123
      128  138  135
      180  186  184
       60   68   70
       38   42   26
       55   49   44
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have an RGB image and a binary mask (1 channel), and I
I have an array of integers which represent a RGB image and would like
I have an array of RGB values, whose size I can guarantee to match
I have an rgb image matrix (height*width*3) represented in doubles. After some manipulations on
I have an RGB image. I want to convert it to numpy array. I
I have an image in Matlab: img = imopen('image.jpg') which returns an uint8 array
Or say does 1 denotes white for an RGB image? I have this question
I have a raw image buffer in the RGB format. I need to draw
I have a table with an image inside: <table style=border: 3px solid rgb(0, 0,
I have an RGB image in MATLAB, and I want to loop through each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.