在Matlab中计算中位数图像 [英] Calculate Median Image in Matlab

查看:314
本文介绍了在Matlab中计算中位数图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是matlab的新手,所以请原谅我,如果我在这里要求明显的:我所拥有的是一系列彩色摄影图像(所有相同的尺寸)。我想要做的是计算每个像素的中位数颜色值。

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.

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

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.

所以例如,如果我有三个图像,我想要matlab来计算(对于每个像素)这三个图像中的哪个颜色值是中值。我怎么会这样做,有人知道吗?

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?

编辑:从我能想到的,我将不得不将所有图像加载到一个矩阵中。矩阵必须具有4个维度(高度,宽度,rgb,图像),并且对于每个像素,每个颜色在第4维度(图像之间)中找到中值。
这是正确的(也可能)?我该怎么做?

推荐答案

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

Expanding my comments into a full answer;

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

@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()将以保留每个像素的亮度的方式执行此操作丢弃色调和饱和度。

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.

编辑:

如果你想定义RGB中位数为笛卡尔坐标中的中间值这对于三幅图像来说很容易做到。

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

考虑单个像素,中位数有三种可能的选择颜色, C1 =(r1,g1,b1) C2 =(r2,g2,b2) C3 =(R3,G3,B3)。通常这些在3D空间中形成一个三角形。

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.

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

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

选择中位数作为与其他两个距离最小的颜色。定义 D1 = D1_2 + D1_3 等,并且 min(D1,D2,D3)应该有效,礼貌 Triangle Inequality 。注意退化情况:等边三角形(C1,C2,C3等距),线(C1,C2,C3彼此线性)或点(C1 = C2 = C3)。

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

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

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.

编辑2

用于定义N点的中位数作为最小点的中心在3D空间中包围它们的球体,您可以尝试:

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


  1. 找到{N}中相距最远的两个点N1和N2。 N1和N2之间的距离是包围所有点的最小球体的直径。 (证明:任何较小的球体都不能同时包围N1和N2。)

  2. 中位数在N1和N2之间: M =(N1 + N2)/ 2

  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.

编辑3 :以上只有在没有三个等距的情况下才有效。也许你需要问math.stackexchange.com?

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

编辑4 :维基百科再次发布! 最小的圈子问题 Bounding sphere

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

这篇关于在Matlab中计算中位数图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆