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.
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:
Now, just to check, if we inspect the first pixel inside our mask in the original image
Xit should equal the first row in
X_data: