在 Matlab 中计算图像中值

发布于 2025-01-06 21:06:16 字数 433 浏览 0 评论 0原文

我是 matlab 新手,所以如果我在这里要求显而易见的东西,请原谅我:我拥有的是彩色摄影图像的集合(所有尺寸都相同)。我想要做的是计算每个像素的 中值 颜色值。

我知道matlab中有一个中值滤波器,但据我所知它并不能完全满足我的要求。因为我想计算整个图像集合之间每个单独像素的中值。

例如,如果我有三个图像,我希望 matlab 计算(对于每个像素)这三个图像中的哪个颜色值是中值。我该怎么做,有人知道吗?


编辑:根据我所能想到的,我必须将所有图像加载到单个矩阵中。该矩阵必须有 4 个维度(高度、宽度、RGB、图像),并且对于每个像素和每种颜色,找到第 4 个维度(图像之间)的中值。 这是正确的(并且可能的)吗?我该如何做到这一点?

I am new to matlab, so forgive me if i am asking for the obvious here: what i have is a collection of color photographic images (all the same dimensions). What i want to do is calculate the median color value for each pixel.

I know there is a median filter in matlab, but as far as i know it does not do exactly what i want. Because i want to calculate the median value between the entire collection of images, for each separate pixel.

So for example, if i have three images, i want matlab to calculate (for each pixel) which colorvalue out of those three images is the median value. How would i go about doing this, does anyone know?


Edit: From what i can come up with, i would have to load all the images into a single matrix. The matrix would have to have 4 dimensions (height, width, rgb, images), and for each pixel and each color find the median in the 4th dimension (between the images).
Is that correct (and possible)? And how can i do this?

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

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

发布评论

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

评论(2

沫尐诺 2025-01-13 21:06:16

你的直觉是正确的。例如,如果您有图像 image_1、image_2、image_3,您可以将它们分配给 4 维矩阵:

X(:,:,:,1) = image_1;
X(:,:,:,2) = image_2;
X(:,:,:,3) = image_3;

然后使用:

Y=median(X,4);

获取中值。

Your intuition is correct. If you have images image_1, image_2, image_3, for example, you can assign them to a 4 dimensional matrix:

X(:,:,:,1) = image_1;
X(:,:,:,2) = image_2;
X(:,:,:,3) = image_3;

Then use:

Y=median(X,4);

To get the median.

别想她 2025-01-13 21:06:16

将我的评论扩展为完整的答案;

@prototoast 的答案很优雅,但由于每个像素的 R、G 和 B 值的中位数是单独计算的,因此输出图像看起来会很奇怪。

为了获得具有视觉意义的明确定义的中位数,最简单的方法是在尝试获取中位数之前将图像转换为黑白图像。

图像处理工具箱中的 rgb2gray() 将以保留每个像素的亮度同时丢弃色调和饱和度的方式执行此操作。

编辑:

如果您想将“RGB中值”定义为“笛卡尔坐标中的中间值”,这对于三个图像来说很容易做到。

考虑一个具有三种可能的中值颜色选择的像素:C1=(r1,g1,b1)C2=(r2,g2,b2)C3=(r3,g3,b3)。一般来说,它们在 3D 空间中形成一个三角形。

获取三种颜色之间的毕达哥拉斯距离:D1_2=abs(C2-C1)D2_3=abs(C3-C2)D1_3=abs(C3- C1)

选择“中值”作为与其他两种颜色距离最近的颜色。定义 D1=D1_2+D1_3 等并采用 min(D1,D2,D3) 应该可行,由 三角形不等式。注意退化情况:等边三角形(C1、C2、C3 等距)、线(C1、C2、C3 彼此成线性)或点(C1=C2=C3)。

请注意,这种思考 3D 中值的简单方法很难扩展到三个以上的图像,因为一组四个或更多 3D 点的“中值”有点难以定义。

编辑 2

要将 N 点的“中值”定义为将它们包围在 3D 空间中的最小球体的中心,您可以尝试:

  1. 在 {N} 中找到最远的两个点 N1 和 N2分开。 N1 和 N2 之间的距离是包围所有点的最小球体的直径。 (证明:任何较小的球体都无法同时包围 N1 和 N2。)
  2. 则中位数位于 N1 和 N2 之间的中间:M = (N1+N2)/2

编辑 3:仅当没有三个点等距时,上述方法才有效。也许您需要询问 math.stackexchange.com?

编辑4:维基百科再次发布! 最小圆问题边界球

Expanding my comments into a full answer;

@prototoast's answer is elegant, but since medians for the R, G and B values of each pixel are calculated separately, the output image will look very strange.

To get a well-defined median that makes visual sense, the easiest thing to do is cast the images to black-and-white before you try to take the median.

rgb2gray() from the Image Processing toolbox will do this in a way that preserves the luminance of each pixel while discarding the hue and saturation.

EDIT:

If you want to define the "RGB median" as "the middle value in cartesian coordinates" this is easy enough to do for three images.

Consider a single pixel with three possible choices for the median colour, C1=(r1,g1,b1), C2=(r2,g2,b2), C3=(r3,g3,b3). Generally these form a triangle in 3D space.

Take the Pythagorean distance between the three colours: D1_2=abs(C2-C1), D2_3=abs(C3-C2), D1_3=abs(C3-C1).

Pick the "median" to be the colour that has lowest distance to the other two. Defining D1=D1_2+D1_3, etc. and taking min(D1,D2,D3) should work, courtesy of the Triangle Inequality. Note the degenerate cases: equilateral triangle (C1, C2, C3 equidistant), line (C1, C2, C3 linear with each other), or point (C1=C2=C3).

Note that this simple way of thinking about a 3D median is hard to extend to more than three images, because "the median" of a set of four or more 3D points is a bit harder to define.

Edit 2

For defining the "median" of N points as the centre of the smallest sphere that encloses them in 3D space, you could try:

  1. Find the two points N1 and N2 in {N} that are furthest apart. The distance between N1 and N2 is the diameter of the smallest sphere that encloses all the points. (Proof: Any smaller and the sphere would not be able to enclose both N1 and N2 at the same time.)
  2. The median is then halfway between N1 and N2: M = (N1+N2)/2.

Edit 3: The above only works if no three points are equidistant. Maybe you need to ask math.stackexchange.com?

Edit 4: Wikipedia delivers again! Smallest circle problem, Bounding sphere.

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