简体   繁体   English

为什么在传递到中值滤镜之前需要将图像转换为灰度?

[英]Why is an image needed to be converted into greyscale before passed to median filter?

Why when I pass a coloured image through median filter I get an error saying that the expected input should be 2D? 为什么当我将彩色图像通过中值滤波器传递时,出现错误消息,要求输入应为2D?

what does this mean? 这是什么意思?

This makes sense because the way colors are described in RGB, that is a 3D array, as opposed to grayscale being these 2D. 这是有道理的,因为颜色是用RGB(即3D阵列)描述的,而灰度是这些2D。

RGB is not a linear scale, and the three channels change the color spectrum quite a lot when used independently. RGB不是线性比例,并且三个通道在单独使用时会大大改变色谱。 This will cause weird effects in color in RGB median filters. 这将在RGB中值滤镜中引起奇怪的色彩效果。

If you think about how a median filter could be applied to a RGB there are 2 different possibilities: 如果考虑如何将中值滤镜应用于RGB,则有2种不同的可能性:

  1. Treat each channel independently 独立对待每个渠道

  2. Treat each pixel as unique RGB values. 将每个像素视为唯一的RGB值。

In the first case, the median filter would give strange colors. 在第一种情况下,中值滤镜会产生奇怪的颜色。 Choosing repeatedly R,G and B values without taking into account the other values will generate random colors that have nothing to do with the originals 在不考虑其他值的情况下反复选择R,G和B值将生成与原件无关的随机颜色

Instead, if you chose to treat pixel values as triplets, as single RGB data, then you will never get anything out of the median filter, because the likelihood of 2 pixel values to have the exact same color in RGB bite by bite is very very low. 相反,如果您选择将像素值作为三元组作为单个RGB数据对待,那么您将永远不会从中值滤镜中得到任何东西,因为两个像素值在RGB中逐个咬合时具有完全相同的颜色的可能性非常大低。 Thus a median filter in RGB triples would be the same as doing nothing in 99.99% of the cases. 因此,在99.99%的情况下,RGB三元组中的中值滤波器将等同于不执行任何操作。

This is why the median filter is not defined/makes no sense in RGB. 这就是为什么在RGB中未定义中值滤镜/使其不起作用的原因。


However ... 但是 ...

If you convert your data to HSV or L a*b* then you can do a median filter of each of its components, and transform the result back to RGB. 如果将数据转换为HSVL a*b*则可以对每个分量进行中值滤波,然后将结果转换回RGB。

Actually it's not uncommon to apply median filter in RGB color space. 实际上,在RGB颜色空间中应用中值滤镜并不少见。

If the purpose is removing salt & pepper noise , it is working quite well in RGB color space. 如果目的是消除盐和胡椒粉噪声 ,则它在RGB颜色空间中效果很好。
Instead of passing RGB as input to medfilt2 , you need to pass each color channel separately. 无需将RGB作为medfilt2输入,而是需要分别传递每个颜色通道。

Ander Biguri is right: from mathematical perspective, it's incorrect to apply median filter in RGB color space. 安德·比古里(Ander Biguri)是对的:从数学角度来看,在RGB颜色空间中应用中值滤镜是不正确的。 It's probably the reason why it's not supported by Matlab implementation of medfilt2 . 这可能是medfilt2的Matlab实现不支持它的原因。

Example for applying medfilt2 in RGB color space: 在RGB颜色空间中应用medfilt2示例:

RGB = imread('https://i.stack.imgur.com/G2aNm.png');

figure;imshow(RGB);title('Original');

R = RGB(:, :, 1);
G = RGB(:, :, 2);
B = RGB(:, :, 3);

R = medfilt2(R);
G = medfilt2(G);
B = medfilt2(B);

RGB = cat(3, R, G, B);

figure;imshow(RGB);title('After median filter of each color channel');

Original image: 原始图片:
在此处输入图片说明

Image after median filter of each color channel: 每个颜色通道的中值滤波后的图像:
在此处输入图片说明

As you can see, it's working quite well... 如您所见,它运作良好...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM