计算屏蔽多通道 Matlab 图像中的像素协方差
我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将您的蒙版复制到三维并像平常一样应用它。然后,只需将矢量输出重新整形为一个矩阵,其中掩模中的每个像素对应一行,每个颜色通道对应一列。下面是一个使用内置图像的示例:
现在,只是为了检查一下,如果我们检查原始图像
X
中掩码内的第一个像素,它应该等于
X_data< 中的第一行/代码>:
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
X
it should equal the first row in
X_data
: