2016-11-21 23:24:14 hzh_csdn 阅读数 7105
• ###### MATLAB图像处理

全面系统的学习MATLAB在图像处理中的应用

19905 人正在学习 去看看 魏伟

### 读入一幅彩色图像，进行如下图像处理：

#### (1) 在RGB彩色空间中对图像进行模糊和锐化处理

rgb= imread('flower.jpg');
figure;  imshow(rgb); title('原图');
 %平滑滤波
r=rgb(:,:,1);
g=rgb(:,:,2);
b=rgb(:,:,3);
m=fspecial('average',[8,8]);
r_filtered=imfilter(r,m);
g_filtered=imfilter(g,m);
b_filtered=imfilter(b,m);
rgb_filtered=cat(3,r_filtered,g_filtered,b_filtered);
figure;  imshow(rgb_filtered);  title('模糊后');
imwrite(rgb_filtered, 'RGB彩色空间模糊后.jpg');

%拉普拉斯
lapMatrix=[1 1 1;1 -8 1;1 1 1];
i_tmp=imfilter(rgb,lapMatrix,'replicate');
i_sharped=imsubtract(rgb,i_tmp);
figure;   imshow(i_sharped); title('锐化后');
imwrite(i_sharped, 'RGB彩色空间锐化后.jpg');

RGB彩色空间锐化后

#### (4) 在HSI彩色空间中，对I分量图像进行模糊和锐化处理，转换回RGB格式并观察效果

fc = imread('flower.jpg');
h = rgb2hsi(fc);
H = h (:,:,1);
S = h (:,:,2);
I = h (:,:,3);

subplot(3,3,1);imshow(fc);  title('原图');

%平滑滤波
m=fspecial('average',[8,8]);
h_filtered=imfilter(H,m);
img_h_filtered = cat(3,h_filtered,S,I);
rgb_h_filtered = hsi2rgb(img_h_filtered);
subplot(3,3,2);imshow(rgb_h_filtered);  title('H分量模糊后');
imwrite(rgb_h_filtered, 'H分量模糊后.jpg');

H分量模糊后

%拉普拉斯
lapMatrix=[1 1 1;1 -8 1;1 1 1];
i_tmp=imfilter(H,lapMatrix,'replicate');
H_sharped=imsubtract(H,i_tmp);
img_h_sharped = cat(3,H_sharped,S,I);
rgb_h_sharped = hsi2rgb(img_h_sharped);
subplot(3,3,3); imshow(rgb_h_sharped); title('H分量锐化后');
imwrite(rgb_h_sharped, 'H分量锐化后.jpg');

H分量锐化后

subplot(3,3,4);imshow(fc);  title('原图');
%平滑滤波
m=fspecial('average',[8,8]);
s_filtered=imfilter(S,m);
img_s_filtered = cat(3,H,s_filtered,I);
rgb_s_filtered = hsi2rgb(img_s_filtered);
subplot(3,3,5);imshow(rgb_s_filtered);  title('S分量模糊后');
imwrite(rgb_s_filtered, 'S分量模糊后.jpg');

S分量模糊后

%拉普拉斯
lapMatrix=[1 1 1;1 -8 1;1 1 1];
i_tmp=imfilter(S,lapMatrix,'replicate');
s_sharped=imsubtract(S,i_tmp);
img_s_sharped = cat(3,H,s_sharped,I);
rgb_s_sharped = hsi2rgb(img_s_sharped);
subplot(3,3,6); imshow(rgb_s_sharped); title('S分量锐化后');
imwrite(rgb_s_sharped, 'S分量锐化后.jpg');

S分量锐化后

subplot(3,3,7);imshow(fc);  title('原图');
%平滑滤波
m=fspecial('average',[8,8]);
i_filtered=imfilter(I,m);
img_i_filtered = cat(3,H,S,i_filtered);
rgb_i_filtered = hsi2rgb(img_i_filtered);
subplot(3,3,8);imshow(rgb_i_filtered);  title('I分量模糊后');
imwrite(rgb_i_filtered, 'I分量模糊后.jpg');

I分量模糊后

%拉普拉斯
lapMatrix=[1 1 1;1 -8 1;1 1 1];
i_tmp=imfilter(I,lapMatrix,'replicate');
i_sharped=imsubtract(I,i_tmp);
img_i_sharped = cat(3,H,S,i_sharped);
rgb_i_sharped = hsi2rgb(img_i_sharped);
subplot(3,3,9); imshow(rgb_i_sharped); title('I分量锐化后');
imwrite(rgb_i_sharped, 'I分量锐化后.jpg');

I分量锐化后

matlab的模糊图像处理 相关内容

2019-06-21 08:57:23 qq_42192910 阅读数 218
• ###### MATLAB图像处理

全面系统的学习MATLAB在图像处理中的应用

19905 人正在学习 去看看 魏伟

# 1、图像模糊

matlab相关文档链接：https://ww2.mathworks.cn/help/images/ref/fspecial.html

# 2、图像亮度不均

（或直接在matlab命令行输出doc()搜索也可查看）

https://blog.csdn.net/Ibelievesunshine/article/details/79958899
https://blog.csdn.net/hjxu2016/article/details/80406886

J = imadjust(I,[low_in high_in],[low_out high_out]) maps intensity values in I to new values in J such that values between low_in and high_in map to values between low_out and high_out.

matlab的模糊图像处理 相关内容

2013-04-10 14:51:37 mlkiller 阅读数 4206
• ###### MATLAB图像处理

全面系统的学习MATLAB在图像处理中的应用

19905 人正在学习 去看看 魏伟

# 一含义

模糊（平滑）是一种常用的图片处理方式，它的作用可以用来降低噪声，还有其他用途

看一下opencv 里面的公式

g(i,j)是目标坐标的像素值， f(i+k,j+l)是k,l这些地方的像素值， h(k,l)是 kernel,  我不知道怎么去准确翻译它的意义，它是过滤器的系数。

简单的按照我的思路去理解，就是一个权值，模糊的含义是将所有的像素按照一定的权值进行运算，得到一个比较均衡的结果。

# 三 算法

1 均值模糊
均值模糊很简单就是周边所有的影响都是1，求平均值即可
2 高斯模糊

# 四均值模糊的代码和效果

先放上均值模糊的代码
void boxblur(Mat input ,Mat &out, int x, int y)
{
// accept only char type matrices
CV_Assert(input.depth() != sizeof(uchar));

out.create(input.size(),input.type());

int nChannels = input.channels();
int nRows = input.rows;
int nCols = input.cols;

int size = x * y;
float kernel = 1.0/size;

int i,j;
uchar* p;
uchar* q;
uchar R,G,B;

for( i = x; i < nRows - x; ++i)
{
q = out.ptr<uchar>(i);
for ( j = y; j < nCols - y; ++j)
{
float sumR = 0;
float sumG = 0;
float sumB = 0;
for (int k =0; k<x;k++)
{
p = input.ptr<uchar>(i-x+k);
for(int l = 0; l < y;l++)
{
sumB += input.at<uchar>(i - x + k,(j + l - y)*nChannels) * kernel;//p[(l + j -y)*nChannels ] * kernel;
sumG += input.at<uchar>(i - x + k,(j + l - y)*nChannels + 1) * kernel;//p[(l + j -y)*nChannels + 1] * kernel;
sumR += input.at<uchar>(i - x + k,(j + l - y)*nChannels + 2) * kernel;//p[(l + j -y)*nChannels + 2] * kernel;
}
}
q[j*nChannels] = sumB;
q[j*nChannels+1] = sumG;
q[j*nChannels+2] = sumR;
}
}

}


 原始 opencv 本文

# 五高斯模糊的代码和效果

void gaussblur(Mat input ,Mat &out, int x, int y)
{
float sigma = 1.5;
Mat kernel;
float pi = 3.1415926;

kernel.create(x ,y ,CV_32F);

float mx = x/2.0;
float my = y/2.0;


       //这里有问题，后面做修正。
for (int i =0; i< x;i++)
{
for (int j =0; j<y;j++)
{
kernel.at<float>(i,j) = exp(-1 * ((i - mx) * (i - mx) +(j - my) * (j-my) )/( 2 * sigma * sigma))/(2 * pi * sigma *sigma) ;
}
}


int nChannels = input.channels();
int nRows = input.rows;
int nCols = input.cols;

out.create(input.size(),input.type());
uchar* p;
uchar* q;
float* s;

for(int  i = x; i < nRows - x; ++i)
{
q = out.ptr<uchar>(i);
for (int j = y; j < nCols - y; ++j)
{
float sumR = 0;
float sumG = 0;
float sumB = 0;
for (int k =0; k<x;k++)
{
p = input.ptr<uchar>(i-x+k);
s = kernel.ptr<float>(k);
for(int l = 0; l < y;l++)
{
sumB += p[(l + j -y)*nChannels ] * s[l];//input.at<uchar>(i - x + k,(j + l - y)*nChannels) * kernel;//
sumG += p[(l + j -y)*nChannels + 1] *s[l];//input.at<uchar>(i - x + k,(j + l - y)*nChannels + 1) * kernel;//
sumR += p[(l + j -y)*nChannels + 2] * s[l];//input.at<uchar>(i - x + k,(j + l - y)*nChannels + 2) * kernel;
}
}
q[j*nChannels] = sumB;
q[j*nChannels+1] = sumG;
q[j*nChannels+2] = sumR;
}
}

}

 原始 opencv 本文

	float sum = 0;
for (int i =0; i< x;i++)
{
for (int j =0; j<y;j++)
{
sum+= kernel.at<float>(i,j) = exp(-1 * ((i - mx) * (i - mx) +(j - my) * (j-my) )/( 2 * sigma * sigma))/(2 * pi * sigma *sigma) ;
}
}
for (int i =0; i< x;i++)
{
for (int j =0; j<y;j++)
{
kernel.at<float>(i,j) = kernel.at<float>(i,j)/ sum ;
}
}

sigma = 0.3*((ksize-1)*0.5 - 1) + 0.8 .

matlab的模糊图像处理 相关内容

2019-09-29 20:22:04 weixin_44225182 阅读数 556
• ###### MATLAB图像处理

全面系统的学习MATLAB在图像处理中的应用

19905 人正在学习 去看看 魏伟

## 图像锐化(原理篇)

图像锐化处理的目的是为了使图像的边缘、轮廓线以及图像的细节变得清晰，经过平滑的图像变得模糊的根本原因是因为图像受到了平均或积分运算，因此可以对其进行逆运算(如微分运算,其实这里用的是差分)就可以使图像变得清晰。
图像中边缘的定义是什么？在图像处理中认为，灰度值变化剧烈的地方就是边缘。变化剧烈程度，数学上就是函数的一阶导数。假设下面第一张图是图像的灰度函数，可以看出，中间变化较快的地方应该是图像的边缘。第二张图是图一的一阶导数，由数学知识可知，一阶导数的极值就是那个变化最快的点–边缘。第三张图是图一的二阶导数，二阶导数为0时，可以看出是图像的边缘处。所以，对于确定图像的边缘，我们只需要找到一阶导数为极值点或者二阶导数为0的位置就行(二阶导数为0不一定为边缘点 比如一副颜色相同的图像 其一阶导数 二阶导数都是0 没有边缘)

那为什么用差分来寻找边缘呢对于连续的函数来说，一阶导数就是直接求导，二阶同理。但是，图像本质是一个二维矩阵，离散型的。是无法求导的。这时候，就需要用到差分这个概念了。实在理解不到的话，可以认为：连续型函数是求导(求微分)，离散型函数则是求差分。

## 更多

matlab的模糊图像处理 相关内容

2017-12-14 17:50:13 lovebaby859450415 阅读数 1539
• ###### MATLAB图像处理

全面系统的学习MATLAB在图像处理中的应用

19905 人正在学习 去看看 魏伟

# 数字图像处理的MATLAB实现（第二版） - 第二章学习总结

• 亮度（或灰度变换）
• 空间滤波
• 模糊图像处理

## 1.背景知识

g(x,y)=T[f(x,y)](1)

## 2.灰度变换函数

s=T(r)(2)

### 2.1灰度图像进行灰度变换的基本图像处理工具箱函数

Low_High=stretchlim(f, tol)

test.m
% default
g1 = imadjust(f, [0 1], [1, 0]);

% 调用工具箱的负片函数
g = imcomplement(f);

% 扩展亮度[0.5 0.75]->[0 1]
g2 = imadjust(f, [0.5 0.75], [0 1]);

% 使用gamma增强
g3 = imadjust(f, [ ], [ ], 2);

% 使用stretchlim函数
g4 = imadjust(f, stretchlim(f), [ ]);

### 2.2. 针对灰度变换的自建M函数

tofloat$tofloat$ 函数
Logical$Logical$uint8$uint8$uint16$uint16$int16$int16$类的图像转换成 single$single$类，配合应用适当的比例因子。下面附上附注释的完整代码。

function [out, revertclass] = tofloat(in)
%TOFLOAT Convert image to floating point
%  [OUT, REVERTCLASS] = TOFLOAT(IN) converts the input image IN to
%  floating-point. If IN is a double or a single image, then OUT
%  equals IN. Otherwise, OUT equals IM2SINGLE(IN). REVERTCLASS is
%  a function handle that can be used to convert back to the class
%  of IN.

identity = @(x) x;       % 函数句柄
tosingle = @im2single;   % 函数句柄

table = {'uint8',   tosingle, @im2uint8
'uint16',  tosingle, @im2uint16
'int16',   tosingle, @im2int16
'logical', tosingle, @logical
'double',  identity, identity
'single',  identity, identity};

% 返回 in 在表中第一列的第几个位置
classIndex = find(strcmp(class(in), table(:, 1)));

% 如果是classIndex矩阵为空，则isempty 返回 1
if isempty(classIndex)
error('Unsupported input image class.');
end

% 这里table是cell数据类型，这里用'{ }', 表示在对应位置的输出值
% out = table{classIndex, 2} = 对应的句柄函数
out = table{classIndex, 2}(in);

revertclass = table{classIndex, 3}; % revertclass= @(x) x
end

gscale$gscale$ 函数

gscale$gscale$ 函数的目的就是实现该功能。

function g =gscale(f, varargin)
% GSCALE Scales the intensity of the input image.
% G = GSCALE(F, 'full8') scales the intensities of F to the full
% 8-bit intensity range [0, 255]. This is the default if there is
% only one input argument.
%
% G = GSCALE(F, 'full16') scales the intensities of F to the full
% 16-bit intensity range [0, 65535].
% G = GSCALE(F, 'minmax', LOW, HIGH) scales the intensities of F
% to the range [LOW, HIGH]. These vaules must be provided, and they
% must be in the range [0, 1], independently of the class of the
% input. GSCALE performs any necessary scaling. If the input is of
% class double, and its vaules are not in the range [0, 1], then
% GSCALE scales it to  this range before processing.
%
% The class of the output is the same as the class of the input.

% length 计算varargin中的最大向量尺度
if length(varargin) == 0   % If only one argument it must be f.
method = 'full8';
else
method = varargin{1};
end

% strcmp 比较class(f) 是否是double类型 如果是则返回1
% max(f(:) > 1) 如果f中有大于1的元素则返回逻辑1
if strcmp(class(f), 'double') & (max(f(:)) > 1 | min(f(:)) < 0)
f = mat2gray(f);
end

% Perform the specified scaling.
switch method
case 'full8'
g = im2uint8(mat2gray(double(f)));   % im2uint8 将f转换为8位无符号整形
case 'full16'
g = im2uint16(mat2gray(double(f)));  % im2uint16 将f转换为16位无符号整形
case 'minmax'
low = varargin{2}; high = varargin{3};
if low > 1 | low < 0 high > 1 | high < 0
error('Parameters low and high must be in the range [0, 1].');
end
if strcmp(class(f), 'double')
low_in = min(f(:));
high_in = max(f(:));
elseif strcmp(class(f), 'uint8')
low_in = double(min(f(:)))./255;
high_in = double(max(f(:)))./255;
elseif strcmp(class(f), 'uint16')
low_in = double(min(f(:)))./65535;
high_in = double(max(f(:)))./65535;
else
% imadjust automatically matches the class of the input.
g = imadjust(f, [low_in high_in], [low high]);
end

intrans$intrans$ 函数

function g = intrans(f, method, varargin)

% Verify the correct number of inputs.
error(nargchk(2, 4, nargin))
if strcmp(method, 'log')
% The log transform handles image classes differently than the
% others transforms, so let the logTranform function handle that
% and then return.
g = logTranform(f, varargin{:});
return;
end

% If f is floating point, check to see if it is in the range [0, 1]
% If it is not, force it to be using function mat2gray.
if isfloat(f) && (max(f(:)) > 1 || min(f(:)) < 0)
f = mat2gray(f);
end
[f, revertclass] = tofloat(f); % Store class of f for use later.

% Perform the intensity transformation specified
switch method
case 'neg'
g = imcomplement(f);

case 'gamma'
g = gammaTransform(f, varargin{:});

case 'stretch'
g = stretchTransform(f, varargin{:});

case 'specified'
g = spcfiedTransform(f, varargin{:});

otherwise
error('Unknown enchancement method.');
end

% Convert to the class of the input image.
g = revertclass(g);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function g = gammaTransform(f, gamma)
g = imadjust(f, [ ], [ ], gamma);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function g = stretchTransform(f, varargin)
if isempty(varargin)
% Use defaults.
m = mean2(f);
E = 4.0;
elseif length(varargin) == 2
m = varargin{1};
E = varargin{2};
else
error('Incorrect number of inputs for the stretch method.');
end
g = 1./(1 + (m./f).^E);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function g = spcfiedTransform(f, txfun)
% f is floating point with vaules in the range [0 1].
txfun = txfun(:);  % Force it to be a column vector.
if any(txfun) > 1 || any(txfun) <= 0
error('All elements of txfun must be in the range [0 1].');
end
T = txfun;
X = linspace(0, 1, numel(T))';
g = interpl(X, T, f);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function g = logTranform(f, varargin)
[f, revertclass] = tofloat(f);
if numel(varagin) >= 2
if strcmp(varagin{2}, 'uint8')
revertclass = @im2uint8;
elseif strcmp(varagin{2}, 'uint16')
revertclass = @im2uint16;
else
error('Unsupported CLASS option for "log" method.');
end
end
if numel(varagin < 1)
% Set default for C
C = 1;
else
C = varargin{1};
end
g = C * (log(1+f));
g = revertclass(g);
end

## 3. 直方图处理和函数绘图

### 3.1. 直方图

p(rk)=h(rk)/n=nk/n(3)

h=imhist(f,b)

### 3.2. 直方图均衡化

s=T(r)=r0pr(w)dw(4)

ps(s)={10s10other(5)

sk=T(rk)=j=0kpr(rj)=j=0knjn(6)

g=histeq(f,nlev)

## 5.模糊集合

matlab的模糊图像处理 相关内容