Applying vector median filter on RGB image based on matlab
前言:
最近想看看矢量中值滤波(Vector median filter, VMF)在GRB图像上的滤波效果,意外的是找了一大圈却发现网上没有现成的code,所以通过matab亲自实现了一个,需要学习的朋友可以拿过去用。本文的核心是VMF的matlab实现,最后通过在RGB图像上应用举例说明。
VMF的数学表达:
含有N个矢量的集合{C1,C2,...CN},它的VMF结果如下所示:

其中,CVM1表示距离所有其他向量的距离和最小的那个向量。而距离可以自己定义,常用的欧氏距离,曼哈顿距离等等。
matlab code:
% 向量中值滤波,根据欧式距离进行滤波,将RGB图像中每个像素位置的三个颜色作为一个向量整体处理。 % pdist函数解释
% Pairwise distance between pairs of objects
% Syntax
% D = pdist(X)
% D = pdist(X,distance)
% Description
%
% D = pdist(X)
% 计算 X 中各对行向量的相互距离(X是一个m-by-n的矩阵). 这里 D 要特别注意,
% D 是一个长为m(m–)/2的行向量.可以这样理解 D 的生成:首先生成一个 X 的距离方阵,
% 由于该方阵是对称的,且对角线上的元素为0,所以取此方阵的下三角元素,按照Matlab中矩阵的按列存储原则,
% 此下三角各元素的索引排列即为(,), (,), ..., (m,), (,), ..., (m,), ..., (m,m–).
% 可以用命令 squareform(D) 将此行向量转换为原距离方阵.(squareform函数是专门干这事的,其逆变换是也是squareform。)
%
% D = pdist(X,distance) 使用指定的距离. vectors_set = rand(,);%向量集合中的每一行是一个向量,一共含有5个向量 dist = pdist(vectors_set,'euclidean');%使用欧式距离计算向量之间的距离
dist = squareform(dist);%还原回矩阵的形式
dist = sum(dist,);%求出每个向量到其他所有向量的距离和
indx = find(dist==min(dist));%找到距离和最小的向量的index
median_vec = vectors_set(indx,:) %根据index找出中值向量
RGB图像上的应用:
% 向量中值滤波,根据欧式距离进行滤波,可以用于RGB图像的去噪。 clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = ;
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
subplot(, , );
imshow(rgbImage);
title('Original color Image', 'FontSize', fontSize);
subplot(, , );
noisyRGB = imnoise(rgbImage,'salt & pepper', .);
imshow(noisyRGB);
title('Image with Salt and Pepper Noise', 'FontSize', fontSize);
subplot(, , );
resortedImage = noisyRGB; [h,w,bands] = size(rgbImage);%彩色图像的大小
win_radius = ;%滤波窗口半径大小
win_size = (*win_radius+).^;%滤波窗口的大小
for i = + win_radius : h -
for j = + win_radius : w-
vectors_set = reshape(rgbImage(i-win_radius:i+win_radius,j-win_radius:j+win_radius,:),win_size,,bands);%将窗口内的向量进行变形
vectors_set = reshape(permute(vectors_set,[,,]),bands,[])';%将向量矩阵变为每一行为一个向量的模式
[n,d] = size(vectors_set);%n是向量个数,d是向量维度
% pdist
% Pairwise distance between pairs of objects
% Syntax
% D = pdist(X)
% D = pdist(X,distance)
% Description
%
% D = pdist(X)
% 计算 X 中各对行向量的相互距离(X是一个m-by-n的矩阵). 这里 D 要特别注意,
% D 是一个长为m(m–1)/2的行向量.可以这样理解 D 的生成:首先生成一个 X 的距离方阵,
% 由于该方阵是对称的,且对角线上的元素为0,所以取此方阵的下三角元素,按照Matlab中矩阵的按列存储原则,
% 此下三角各元素的索引排列即为(2,1), (3,1), ..., (m,1), (3,2), ..., (m,2), ..., (m,m–1).
% 可以用命令 squareform(D) 将此行向量转换为原距离方阵.(squareform函数是专门干这事的,其逆变换是也是squareform。)
%
% D = pdist(X,distance) 使用指定的距离.distance可以取下面圆括号中的值,用红色标出!
dist = pdist(vectors_set,'euclidean');
dist = squareform(dist);
dist = sum(dist,2);
indx = find(dist==min(dist));
median_vec = vectors_set(indx,:);
resortedImage(i,j,:)= median_vec(1,:);
end
end imshow(resortedImage);
title('Restored Image', 'FontSize', fontSize);
实验结果:

总结:算法的实现并不复杂,但是运算时间较长,可以进一步优化。
另外:在针对RGB图像的矢量滤波上面,有一种做法是color plane by color plane的做法,虽然已经不算是vector median filter-based的方法了,但是效果还好,可以借鉴
matlab code:
%
% 一个plane一个plane的单独处理 clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = ;
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = .
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(, , );
imshow(rgbImage);
title('Original color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(,'Screensize'));
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, );
greenChannel = rgbImage(:, :, );
blueChannel = rgbImage(:, :, );
% Display the individual red, green, and blue color channels.
subplot(, , );
imshow(redChannel);
title('Red Channel', 'FontSize', fontSize);
subplot(, , );
imshow(greenChannel);
title('Green Channel', 'FontSize', fontSize);
subplot(, , );
imshow(blueChannel);
title('Blue Channel', 'FontSize', fontSize);
% Generate a noisy image. This has salt and pepper noise independently on
% each color channel so the noise may be colored.
noisyRGB = imnoise(rgbImage,'salt & pepper', .);
subplot(, , );
imshow(noisyRGB);
title('Image with Salt and Pepper Noise', 'FontSize', fontSize);
% Extract the individual red, green, and blue color channels.
redChannel = noisyRGB(:, :, );
greenChannel = noisyRGB(:, :, );
blueChannel = noisyRGB(:, :, );
% Display the noisy channel images.
subplot(, , );
imshow(redChannel);
title('Noisy Red Channel', 'FontSize', fontSize);
subplot(, , );
imshow(greenChannel);
title('Noisy Green Channel', 'FontSize', fontSize);
subplot(, , );
imshow(blueChannel);
title('Noisy Blue Channel', 'FontSize', fontSize);
% Median Filter the channels:
redMF = medfilt2(redChannel, [ ]);
greenMF = medfilt2(greenChannel, [ ]);
blueMF = medfilt2(blueChannel, [ ]);
% Find the noise in the red.
noiseImage = (redChannel == | redChannel == );
% Get rid of the noise in the red by replacing with median.
noiseFreeRed = redChannel;
noiseFreeRed(noiseImage) = redMF(noiseImage);
% Find the noise in the green.
noiseImage = (greenChannel == | greenChannel == );
% Get rid of the noise in the green by replacing with median.
noiseFreeGreen = greenChannel;
noiseFreeGreen(noiseImage) = greenMF(noiseImage);
% Find the noise in the blue.
noiseImage = (blueChannel == | blueChannel == );
% Get rid of the noise in the blue by replacing with median.
noiseFreeBlue = blueChannel;
noiseFreeBlue(noiseImage) = blueMF(noiseImage);
% Reconstruct the noise free RGB image
rgbFixed = cat(, noiseFreeRed, noiseFreeGreen, noiseFreeBlue);
subplot(, , );
imshow(rgbFixed);
title('Restored Image', 'FontSize', fontSize);
实验结果:

Applying vector median filter on RGB image based on matlab的更多相关文章
- CVPR论文《100+ Times Faster Weighted Median Filter (WMF)》的实现和解析(附源代码)。
四年前第一次看到<100+ Times FasterWeighted Median Filter (WMF)>一文时,因为他附带了源代码,而且还是CVPR论文,因此,当时也对代码进行了一定 ...
- 数字图像处理实验(11):PROJECT 05-02,Noise Reduction Using a Median Filter 标签: 图像处理MATLAB 2017-05-26 23:
实验要求: Objective: To understand the non-linearity of median filtering and its noise suppressing abili ...
- hdu 3648 Median Filter (树状数组)
Median Filter Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- 三维网格去噪算法(two-step framework)
基于两步法的网格去噪算法顾名思义包含两个步骤:首先对网格表面的法向进行滤波,得到调整后的网格法向信息,然后根据调整后的法向更新顶点坐标位置,下面介绍三篇该类型的文章. [Sun et al. 2007 ...
- 一种基于RGB空间的对照度增强的filter
今天看前辈博客的时候看到一种新的基于RGB颜色空间的image contrast enhance filter 流浪的鱼link: http://blog.csdn.net/jia20003/arti ...
- codeforces 590A A. Median Smoothing(思维)
题目链接: A. Median Smoothing time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- 【22.70%】【codeforces 591C】 Median Smoothing
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces 590 A:Median Smoothing
A. Median Smoothing time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #327 (Div. 2) B. Rebranding C. Median Smoothing
B. Rebranding The name of one small but proud corporation consists of n lowercase English letters. T ...
随机推荐
- UDAD 用户故事驱动的敏捷开发 – 演讲实录
敏捷发展到今天已经在软件行业得到了广泛认可,但大多数敏捷方法都是为了解决某一特定问题而总结出来的特定方法或实践,一直缺乏一个可以将整个开发过程串接起来的成体系的方法.用户故事驱动的敏捷开发(User ...
- 让代码更简单——自定义toBean实现
经过历时三天的鏖战,终于将阶段性项目——新闻发布系统做完了.在编码过程中,发现了很多冗余代码,统一流程,却需要不断重复编码——将用户输入实例化为对象的过程. 例: Person.set("i ...
- 梳理delegate相关概念
一.前言 可能项目规模较小,项目中除了增删改查就只剩下业务流程,以前都没怎么弄明白的东西时间长了就越发的模糊了... 二.使用场景 MSDN:delegate 是一种可用于封装命名或匿名方法的引用类型 ...
- ORACLE lag()与lead() 函数
一.简介 lag与lead函数是跟偏移量相关的两个分析函数,通过这两个函数可以在一次查询中取出同一字段的前N行的数据(lag)和后N行的数据(lead)作为独立的列,从而更方便地进行进行数据过滤.这种 ...
- how2heap分析系列:2_fastbin_dup
源码 #include <stdio.h> #include <stdlib.h> int main() { printf("This file demonstrat ...
- [译]Thinking in React
编者按 使用React的思想来构建应用对我在实际项目中以及帮助他人解决实际问题时起到了很大作用,所以我翻译此文来向那些正在或即将陷入React或React-Native深坑的同胞们表示慰问.网上已经有 ...
- AC日记——滑动窗口 洛谷 P1886
题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最大值和最小值. 例如: The array i ...
- 制作Mac安装盘U盘
1. 下载对应版本的mac安装文件, 复制到mac上, 解压后应该是一个类似于 Install OS X [version name].app 的目录, 复制到/Applications 2. 将U盘 ...
- [LeetCode] Combine Two Tables 联合两表
Table: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | PersonId ...
- 图解javascript