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 ...
随机推荐
- git-入门
一.简介 Git是目前世界上最先进的分布式版本控制系统,Git中绝大部分操作都是访问本地资源,不需要网络,其中有三个概念比较重要:1. 工作目录 2. 暂存区域 3.本地仓库. 简单说明一下,工作目录 ...
- Linix登录报"/etc/profile: line 11: syntax error near unexpected token `$'{\r''"
同事反馈他在一测试服务器(CentOS Linux release 7.2.1511)上修改了/etc/profile文件后,使用source命令不能生效,让我帮忙看看,结果使用SecureCRT一登 ...
- Webform(五)——内置对象(Response、Request)和Repeater中的数据增删改
一.内置对象 (一)Response对象 1.简介:response 对象在ASP中负责将信息传递给用户.Response对象用于动态响应客户端请求,并将动态生成的响应结果返回到客户端浏览器中,使用R ...
- SQL*Plus环境下创建PLUSTRACE角色
普通用户在SQL*Plus中开启AUTOTRACE报告时,遇到SP2-0618: Cannot find the Session Identifier. Check PLUSTRACE role is ...
- Spark核心——RDD
Spark中最核心的概念为RDD(Resilient Distributed DataSets)中文为:弹性分布式数据集,RDD为对分布式内存对象的 抽象它表示一个被分区不可变且能并行操作的数据集:R ...
- java中的浮点数
浮点数值不适用于禁止出现舍入误差的金融计算中.例如,命令System.out.println(2.0-1.1)将打印出0.8999999999999999999999999,而不是人们想象的0.9.其 ...
- [jquery]if条件句
//写个网页用了多门语言,脑袋转不过来亚! //代码: if(){} else if(){} else {}
- idea fect
idea facet 昨天从svn检查一个项目后,部署至tomcat服务器,启动成功,但实际代码其实没有进去, 因为该项目不是maven项目, artifacats是自己配的, 应该是这里弄错的. 最 ...
- Selenium-java-web常用操作---2
都是些的方法,一起交流交流 上传文件 private static void action2() { // TODO Auto-generated method stub WebElement ele ...
- jQuery选择器和选取方法 http://www.cnblogs.com/MaxIE/p/4078869.html
我们已经使用了带有简单Css选择器的jQuery选取函数:$().现在是时候深入了解jQuery选择器语法,以及一些提取和扩充选中元素集的方法了. 一.jQuery选择器 在CSS3选择器标淮草案定义 ...