计算屏蔽多通道 Matlab 图像中的像素协方差

发布于 2024-12-13 04:21:38 字数 400 浏览 3 评论 0原文

我有一个 RGB 图像,称之为 img,表示为大小为 (100,200,3) 的双精度数组

我有一个二进制掩码(称之为 mask),这是一个逻辑大小为 (100,200) 的数组。

我想知道屏蔽区域的平均像素值。 我还想知道该区域中像素值的完整 (3x3) 协方差矩阵。

现在,如果这是一个单通道(而不是 3 通道)图像,我可以简单地执行以下操作:

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

在循环中为每个通道执行类似的操作,拉出值,然后构建一个大型 3xN 矩阵(其中 N 是 mask 中“true”的数量,最后用mean和cov对该矩阵进行操作,我很好奇是否有一种无需循环的方法。

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 (3x3) 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恋竹姑娘 2024-12-20 04:21:38

将您的蒙版复制到三维并像平常一样应用它。然后,只需将矢量输出重新整形为一个矩阵,其中掩模中的每个像素对应一行,每个颜色通道对应一列。下面是一个使用内置图像的示例:

% 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))

现在,只是为了检查一下,如果我们检查原始图像 X 中掩码内的第一个像素,

>> X(250, 100, :)

ans(:,:,1) =

  144


ans(:,:,2) =

  125


ans(:,:,3) =

  123

它应该等于 X_data< 中的第一行/代码>:

>> X_data

X_data =

  144  125  123
  128  138  135
  180  186  184
   60   68   70
   38   42   26
   55   49   44

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文