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 ...
随机推荐
- Java 性能分析工具 , 第 2 部分:Java 内置监控工具
引言 本文为 Java 性能分析工具系列文章第二篇,第一篇:操作系统工具.在本文中将介绍如何使用 Java 内置监控工具更加深入的了解 Java 应用程序和 JVM 本身.在 JDK 中有许多内置的工 ...
- Curator框架的使用
Curator框架的目的是减少用户的复杂度,毕竟原生的Zookeeper难以使用. 这里举一个使用例子. 第一步:建立连接 // 以下代码与192.168.1.101:2181建立了连接Curator ...
- win7的6个网络命令
1 名称: Ipconfig 参数: /all : 显示详细信息 /renew: 更新所有适配器 /renew EL*:更新所有名称以EL为开头的连接 /release *Con*: 释放所有匹配的连 ...
- Json对象与Json字符串互转(4种转换方式)
Json字符与Json对象的相互转换方式有很多,接下来将为大家一一介绍下,感兴趣的朋友可以参考下哈,希望可以帮助到你 1>jQuery插件支持的转换方式: 复制代码代码如下: $.parseJS ...
- yii2 modal弹窗之ActiveForm ajax表单异步验证
作者:白狼 出处:http://www.manks.top/yii2_modal_activeform_ajax.html 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位 ...
- ASP.NET MVC Controllers and Actions
MVC应用程序里的URL请求是通过控制器Controller处理的,不管是请求视图页面的GET请求,还是传递数据到服务端处理的Post请求都是通过Controller来处理的,先看一个简单的Contr ...
- Oracle学习笔记十三 触发器
简介 触发器是当特定事件出现时自动执行的存储过程,特定事件可以是执行更新的DML语句和DDL语句,触发器不能被显式调用. 触发器的功能: 1.自动生成数据 2.自定义复杂的安全权限 3.提供审计和 ...
- Java监听器
监听器 1.概念 监听器:主要是用来监听特定对象的创建,属性的变化的!,本质上却是一个实现特定接口的普通java类! 对象分为自己创建自己使用的,和别人创建自己用的,自己创建的不需要监听,值需要取监听 ...
- PHP的GD库
GD库 PHP通过GD库,可以对JPG.PNG.GIF.SWF等图片进行处理.GD库常用在图片加水印,验证码生成等方面. 绘制线条 要对图形进行操作,首先要新建一个画布,通过imagecreatetr ...
- 机器学习实战笔记--k近邻算法
#encoding:utf-8 from numpy import * import operator import matplotlib import matplotlib.pyplot as pl ...