前言:

最近想看看矢量中值滤波(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的更多相关文章

  1. CVPR论文《100+ Times Faster Weighted Median Filter (WMF)》的实现和解析(附源代码)。

    四年前第一次看到<100+ Times FasterWeighted Median Filter (WMF)>一文时,因为他附带了源代码,而且还是CVPR论文,因此,当时也对代码进行了一定 ...

  2. 数字图像处理实验(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 ...

  3. hdu 3648 Median Filter (树状数组)

    Median Filter Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  4. 三维网格去噪算法(two-step framework)

    基于两步法的网格去噪算法顾名思义包含两个步骤:首先对网格表面的法向进行滤波,得到调整后的网格法向信息,然后根据调整后的法向更新顶点坐标位置,下面介绍三篇该类型的文章. [Sun et al. 2007 ...

  5. 一种基于RGB空间的对照度增强的filter

    今天看前辈博客的时候看到一种新的基于RGB颜色空间的image contrast enhance filter 流浪的鱼link: http://blog.csdn.net/jia20003/arti ...

  6. codeforces 590A A. Median Smoothing(思维)

    题目链接: A. Median Smoothing time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  7. 【22.70%】【codeforces 591C】 Median Smoothing

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. Codeforces 590 A:Median Smoothing

    A. Median Smoothing time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  9. 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 ...

随机推荐

  1. 了解npm的文件结构(npm-folders)和配置文件(npm-mrc)

    一.npm的文件结构 npm的安装: 本地安装 1. 将安装包放在 ./node_modules 下(运行 npm 命令时所在的目录),如果没有 node_modules 目录,会在当前执行 npm ...

  2. 用application实现一个网页的浏览计数器

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  3. C语言运算符优先级

    优先级 运算符 名称或含义 使用形式 结合方向 说明 1 [] 数组下标 数组名[常量表达式] 左到右 -- () 圆括号 (表达式)/函数名(形参表) -- . 成员选择(对象) 对象.成员名 -- ...

  4. Spring MVC之@RequestMapping 详解

    (转自:http://blog.csdn.net/walkerjong/article/details/7994326) 引言: 前段时间项目中用到了RESTful模式来开发程序,但是当用POST.P ...

  5. Oracle创建用户设置权限

    (转:http://www.cnblogs.com/yangy608/archive/2011/08/22/2148893.html) create user TEST identified by & ...

  6. IIS将错误信息发送到浏览器

    本文版权归博客园和dige1993所有,访问作者博客:http://www.cnblogs.com/dige1993 最近又开始玩ASP了,调试的时候出现错误不清楚详细错误信息特别不方便,记得以前可以 ...

  7. Windows10上安装EDEM2.7

    这次我们来安装EDEM2.7. 安装软件来自于互联网,本文仅作学习交流之用,工程应用请购买正版. 1 软件准备 从网上找到EDEM2.7安装包,解压后里面包含两个文件,如下图所示. 2 软件安装 鼠标 ...

  8. JAVA开发中遇到的小白点

    这里主要是自己个人开发中遇到的一些小问题,自己攒起来,来弥补自己薄弱的JAVA基础,大神不要见笑 1. DateFormat格式化的HH和hh区别: public static boolean com ...

  9. Shell教程

    http://www.reddragonfly.org/abscn/index.html

  10. HTTP状态码(HTTP Status Code)

    一些常见的状态码为: 200 - 服务器成功返回网页 404 - 请求的网页不存在 503 - 服务不可用 所有状态解释: 1xx(临时响应)表示临时响应并需要请求者继续执行操作的状态代码. 代码 说 ...