我这辈子都记不起这个手术叫什么了。该操作被定义为将所考虑的像素替换为内核窗口中频率最高的像素值。
目的是消除人们可能在其他更突出区域的边缘发现的无关颜色,并将其合并为一个较小的颜色子集。
例如,考虑一下刚果国旗:
如果我们放大到两种颜色之间的边界,我们会观察到颜色过渡效果。
出于我的目的,上图中只有两种颜色,但对角线形状有助于混合边界颜色。
最佳答案
这称为模式过滤器,因为您将用它的邻居的模式(或最常见的值)替换每个像素。
在 MATLAB 中,如果您有图像处理工具箱,您可以使用 colfilt
轻松执行此类过滤。它将在指定大小的邻域内应用提供的函数。
output = colfilt(data, [5 5], 'sliding', @mode)
更新
如果你不想处理边缘的所有零填充,你可以使用 padarray
在每边应用 3 个像素的填充,然后执行过滤,然后删除距离所有边缘额外 3 个像素。
% Pad with replicates of the data
data = padarray(data, [3 3], 'replicate', 'both');
% Perform the filtering
new = colfilt(data, [5 5], 'sliding', @mode);
% Crop out the padding part
new = new(4:end-3,4:end-3);
如果您想将其推广到大小为 n
的内核,您可以使用以下函数:
function out = mode_filter(data, n)
pad_size = ceil(n / 2);
% Pad with replicates of the data
data = padarray(data, [pad_size, pad_size], 'replicate', 'both');
% Perform the filtering
out = colfilt(data, [n n], 'sliding', @mode);
% Crop out the padded part
out = out((pad_size + 1):(end - pad_size), (pad_size + 1):(end - pad_size));
end
关于matlab - 消除图像中两种颜色边缘的颜色过渡效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40010827/