数字图像处理 第3章
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
%% 例3.3 P51 intrans ·灰度变换的十项全能函数 clc clear f = imread('Fig0306(a)(bone-scan-GE).tif'); figure imshow(f) g = intrans(f,'stretch',mean2(im2double(f)),0.9); figure imshow(g)
Digital Image Processing
Digital Image Processing
3.3 直方图处理与函数绘 图
3.3.1:生成并绘制函数的直方图 直方图定义:图像中各灰度级的统计值, h(rk ) = nk 归一化直方图:
p (rk ) =
h(rk ) nk = n n
Matlab核心函数:h=inhist(f,b) 例3.4 计算并绘制图像直方图
Digital Image Processing
figure stem(horz,h1,'fill') %用stem函数绘制直方图 axis([0 255 0 15000]) set(gca,'xtick',[0:50:255]) set(gca,'ytick',[0:2000:15000])
figure plot(h) %用plot函数绘制直方图 axis([0 255 0 15000]) set(gca,'xtick',0:50:255) set(gca,'ytick',0:2000:15000)
Digital Image Processing
从键盘输入交互式信息,绘制高斯函数:
function p = manualhist % A good set of starting values is: (0.15, 0.05, 0.75, 0.05, 1, 0.07, 0.002). % Initialize. repeats = true; quitnow = 'x'; % Compute a default histogram in case the user quits before estimating at least one histogram. p = twomodegauss(0.15, 0.05, 0.75, 0.05, 1, 0.07, 0.002); % Cycle until an x is input. while repeats s = input('Enter m1, sig1, m2, sig2, A1, A2, k OR x to quit:','s'); if s == quitnow break end
Digital Image Processing
例3.2:使用对数变换减少动态范围
%% 例3.2 使用对数变换 减小动态范围 经典的阈值函数 clc clear f = imread('Fig0305(a)(spectrum).tif'); figure subplot(121),imshow(f),subplot(122),imhist(f),axis tight g = im2uint8(mat2gray(log(1 + double(f)))); figure subplot(121),imshow(g),title('使用对数变换减小动态范围') subplot(122),imhist(g),axis tight % 对比度拉伸变换 m = 5; E = 10; h = im2uint8(mat2gray(1./(1 + (m./(double(f) + eps)).^E))); figure subplot(121),imshow(h),title('对比度拉伸') subplot(122),imhist(h),axis tight
Digital Image Processing
Digital Image Processing
Digital Image Processing
3.2.3 亮度变换的一些使用M函数 处理可变数量的输入和输出: 输入:n=nargin 输出:n=nargout 亮度变换的第二个M函数: g=changeclass(newclass,f) 编写intrans格式化函数:intrans.m,程序略。 intrans intrans.m 例3.3:函数intrans.m的应用
Digital Image Processing
figure %用bar函数绘制直方图 bar(horz,h1)
figure %用bar函数绘制直方图并调整 bar(horz,h1) axis([0 255 0 15000]) set(gca,'xtick',0:50:255) set(gca,'ytick',0:2000:15000)
得到均衡化后的概率密度函数p(s),定义一个变量z,满足:
综合后得到: 由输入图像得到T(r),只要找到 H −1,就可以使用上式得到变 换后的灰度级z。 Matlab工具箱中可用如下函数实现直方图归定化: g=histeq(f,hspec)
Digital Image Processing
wenku.baidu.com
例3.6:直方图归定化(匹配) 对一幅火星卫星图像,直方图均衡化得不到好的处理效果:
Digital Image Processing
采用交互式直方图匹配,首先计算一个已归一化到单位区域 的双峰值高斯函数:
function p = twomodegauss(m1, sig1, m2, sig2, A1, A2, k) % TWOMODEGAUSS Generates a two-mode Gaussian function. % P = TWOMODEGAUSS(M1, SIG1, M2, SIG2, A1, A2, K) generates a two-mode, % Gaussian-like function in the interval [0,1]. P is a 256-element vector normalized % so that SUM(P) equals 1. The mean and standard deviation of the modes are % (M1, SIG1) and (M2, SIG2), respectively. A1 and A2 are the amplitude values of % the two modes. Since the output is normalized, only the relative % values of A1 and A2 are important. K is an offset value that raises the "floor" of % the function. A good set of values to try is M1=0.15, S1=0.05, M2=0.75, S2=0.05, % A1=1, A2=0.07, and K=0.002. c1 = A1 * (1 / ((2 * pi) ^ 0.5) * sig1); k1 = 2 * (sig1 ^ 2); c2 = A2 * (1 / ((2 * pi) ^ 0.5) * sig2); k2 = 2 * (sig2 ^ 2); z = linspace(0, 1, 256); p = k + c1 * exp(-((z - m1) .^ 2) ./ k1) +c2 * exp(-((z - m2) .^ 2) ./ k2); p = p ./ sum(p(:));
Digital Image Processing
3.3.2:直方图均衡化 对输入图像执行如下变换,得到输出后的灰度级s:
所以输出灰度级的概率密度是均匀的:
使用直方图并调用直方图均衡化技术来处理离散灰度级时, 因为变量的离散属性,处理后的图像直方图不再均匀,对应 离散的灰度级,均衡变换采用求和方式变为:
%% 例3.6 直方图均衡化的缺陷 默认为64 clc clear f = imread('Fig0310(a)(Moon Phobos).tif'); subplot(121),imshow(f),subplot(122),imhist(f) ylim('auto') [g T] = histeq(f,256); figure,subplot(121),imshow(g),subplot(122),imhist(g) ylim('auto') g = histeq(f); % 默认为64 figure,subplot(121),imshow(g),subplot(122),imhist(g) ylim('auto') g = histeq(f,8); figure,subplot(121),imshow(g),subplot(122),imhist(g) ylim('auto') % 对应变换 x = linspace(0,1,256); figure,plot(x,T) axis([0 1 0 1])
第三章 亮度变换与空间滤波
3.1 背景知识
空间域技术直接对图像像素进行操作:g(x,y)=T[f(x,y)]
Digital Image Processing
3.2 亮度变换函数
s=T(r) 3.2.1 imadjust函数 g=imadjust(f,[low_in high_in], [low_out high_out], gamma)
Digital Image Processing
Digital Image Processing
Digital Image Processing
Digital Image Processing
3.3.3:直方图归定化(匹配) 已知输入图像灰度级的概率密度函数P(r),输出灰度级的概率 密度函数p(z)(给定),则:
Digital Image Processing
例3.1:使用函数imadjust
%% 使用函数 imadjust 目的:突出我们感兴趣的亮度带·压缩灰度级的低端并扩展 灰度级的高端 clc clear f = imread('Fig0303(a)(breast).tif'); figure imshow(f) g1 = imadjust(f,[0,1],[1,0]); % === imcomplement(f) 灰度反转 @ 灰度负片 figure imshow(g1) g2 = imadjust(f,[0.5,0.75],[0,1]); % 突出我们感兴趣的亮度带 figure imshow(g2) g3 = imadjust(f,[],[],2); % 压缩灰度级的低端并扩展灰度级的高端 figure imshow(g3)
Digital Image Processing
Digital Image Processing
3.2.2 对数和对比度拉伸变换 对数变换:g=c*log(1+double(f)) 对比度拉伸:
对比度拉伸的matlab实现: g 1. g=1. /(1+(m./(double(f)+eps)).^E)
Digital Image Processing
直方图均衡化的matlab函数为histeq:g=histeq(f,nlev) 例3.5 直方图均衡化
%% 例3.5 直方图均衡化 默认为64 clc clear f = imread('Fig0308(a)(pollen).tif'); figure,subplot(121),imshow(f),subplot(122),imhist(f) ylim('auto') g = histeq(f,256); figure,subplot(121),imshow(g),subplot(122),imhist(g) ylim('auto') g = histeq(f,128); figure,subplot(121),imshow(g),subplot(122),imhist(g) ylim('auto') g = histeq(f); % 默认为64 figure,subplot(121),imshow(g),subplot(122),imhist(g) ylim('auto') g = histeq(f,8); figure,subplot(121),imshow(g),subplot(122),imhist(g) ylim('auto')
f = imread('Fig0303(a)(breast).tif');%读取原图像 h=imhist(f);%直方图函数 h1=h(1:10:256); horz=1:10:256;
Digital Image Processing
figure subplot(121),imshow(f),subplot(122),imhist(f)%绘制原图和默认直方图
Digital Image Processing
Digital Image Processing
3.3 直方图处理与函数绘 图
3.3.1:生成并绘制函数的直方图 直方图定义:图像中各灰度级的统计值, h(rk ) = nk 归一化直方图:
p (rk ) =
h(rk ) nk = n n
Matlab核心函数:h=inhist(f,b) 例3.4 计算并绘制图像直方图
Digital Image Processing
figure stem(horz,h1,'fill') %用stem函数绘制直方图 axis([0 255 0 15000]) set(gca,'xtick',[0:50:255]) set(gca,'ytick',[0:2000:15000])
figure plot(h) %用plot函数绘制直方图 axis([0 255 0 15000]) set(gca,'xtick',0:50:255) set(gca,'ytick',0:2000:15000)
Digital Image Processing
从键盘输入交互式信息,绘制高斯函数:
function p = manualhist % A good set of starting values is: (0.15, 0.05, 0.75, 0.05, 1, 0.07, 0.002). % Initialize. repeats = true; quitnow = 'x'; % Compute a default histogram in case the user quits before estimating at least one histogram. p = twomodegauss(0.15, 0.05, 0.75, 0.05, 1, 0.07, 0.002); % Cycle until an x is input. while repeats s = input('Enter m1, sig1, m2, sig2, A1, A2, k OR x to quit:','s'); if s == quitnow break end
Digital Image Processing
例3.2:使用对数变换减少动态范围
%% 例3.2 使用对数变换 减小动态范围 经典的阈值函数 clc clear f = imread('Fig0305(a)(spectrum).tif'); figure subplot(121),imshow(f),subplot(122),imhist(f),axis tight g = im2uint8(mat2gray(log(1 + double(f)))); figure subplot(121),imshow(g),title('使用对数变换减小动态范围') subplot(122),imhist(g),axis tight % 对比度拉伸变换 m = 5; E = 10; h = im2uint8(mat2gray(1./(1 + (m./(double(f) + eps)).^E))); figure subplot(121),imshow(h),title('对比度拉伸') subplot(122),imhist(h),axis tight
Digital Image Processing
Digital Image Processing
Digital Image Processing
3.2.3 亮度变换的一些使用M函数 处理可变数量的输入和输出: 输入:n=nargin 输出:n=nargout 亮度变换的第二个M函数: g=changeclass(newclass,f) 编写intrans格式化函数:intrans.m,程序略。 intrans intrans.m 例3.3:函数intrans.m的应用
Digital Image Processing
figure %用bar函数绘制直方图 bar(horz,h1)
figure %用bar函数绘制直方图并调整 bar(horz,h1) axis([0 255 0 15000]) set(gca,'xtick',0:50:255) set(gca,'ytick',0:2000:15000)
得到均衡化后的概率密度函数p(s),定义一个变量z,满足:
综合后得到: 由输入图像得到T(r),只要找到 H −1,就可以使用上式得到变 换后的灰度级z。 Matlab工具箱中可用如下函数实现直方图归定化: g=histeq(f,hspec)
Digital Image Processing
wenku.baidu.com
例3.6:直方图归定化(匹配) 对一幅火星卫星图像,直方图均衡化得不到好的处理效果:
Digital Image Processing
采用交互式直方图匹配,首先计算一个已归一化到单位区域 的双峰值高斯函数:
function p = twomodegauss(m1, sig1, m2, sig2, A1, A2, k) % TWOMODEGAUSS Generates a two-mode Gaussian function. % P = TWOMODEGAUSS(M1, SIG1, M2, SIG2, A1, A2, K) generates a two-mode, % Gaussian-like function in the interval [0,1]. P is a 256-element vector normalized % so that SUM(P) equals 1. The mean and standard deviation of the modes are % (M1, SIG1) and (M2, SIG2), respectively. A1 and A2 are the amplitude values of % the two modes. Since the output is normalized, only the relative % values of A1 and A2 are important. K is an offset value that raises the "floor" of % the function. A good set of values to try is M1=0.15, S1=0.05, M2=0.75, S2=0.05, % A1=1, A2=0.07, and K=0.002. c1 = A1 * (1 / ((2 * pi) ^ 0.5) * sig1); k1 = 2 * (sig1 ^ 2); c2 = A2 * (1 / ((2 * pi) ^ 0.5) * sig2); k2 = 2 * (sig2 ^ 2); z = linspace(0, 1, 256); p = k + c1 * exp(-((z - m1) .^ 2) ./ k1) +c2 * exp(-((z - m2) .^ 2) ./ k2); p = p ./ sum(p(:));
Digital Image Processing
3.3.2:直方图均衡化 对输入图像执行如下变换,得到输出后的灰度级s:
所以输出灰度级的概率密度是均匀的:
使用直方图并调用直方图均衡化技术来处理离散灰度级时, 因为变量的离散属性,处理后的图像直方图不再均匀,对应 离散的灰度级,均衡变换采用求和方式变为:
%% 例3.6 直方图均衡化的缺陷 默认为64 clc clear f = imread('Fig0310(a)(Moon Phobos).tif'); subplot(121),imshow(f),subplot(122),imhist(f) ylim('auto') [g T] = histeq(f,256); figure,subplot(121),imshow(g),subplot(122),imhist(g) ylim('auto') g = histeq(f); % 默认为64 figure,subplot(121),imshow(g),subplot(122),imhist(g) ylim('auto') g = histeq(f,8); figure,subplot(121),imshow(g),subplot(122),imhist(g) ylim('auto') % 对应变换 x = linspace(0,1,256); figure,plot(x,T) axis([0 1 0 1])
第三章 亮度变换与空间滤波
3.1 背景知识
空间域技术直接对图像像素进行操作:g(x,y)=T[f(x,y)]
Digital Image Processing
3.2 亮度变换函数
s=T(r) 3.2.1 imadjust函数 g=imadjust(f,[low_in high_in], [low_out high_out], gamma)
Digital Image Processing
Digital Image Processing
Digital Image Processing
Digital Image Processing
3.3.3:直方图归定化(匹配) 已知输入图像灰度级的概率密度函数P(r),输出灰度级的概率 密度函数p(z)(给定),则:
Digital Image Processing
例3.1:使用函数imadjust
%% 使用函数 imadjust 目的:突出我们感兴趣的亮度带·压缩灰度级的低端并扩展 灰度级的高端 clc clear f = imread('Fig0303(a)(breast).tif'); figure imshow(f) g1 = imadjust(f,[0,1],[1,0]); % === imcomplement(f) 灰度反转 @ 灰度负片 figure imshow(g1) g2 = imadjust(f,[0.5,0.75],[0,1]); % 突出我们感兴趣的亮度带 figure imshow(g2) g3 = imadjust(f,[],[],2); % 压缩灰度级的低端并扩展灰度级的高端 figure imshow(g3)
Digital Image Processing
Digital Image Processing
3.2.2 对数和对比度拉伸变换 对数变换:g=c*log(1+double(f)) 对比度拉伸:
对比度拉伸的matlab实现: g 1. g=1. /(1+(m./(double(f)+eps)).^E)
Digital Image Processing
直方图均衡化的matlab函数为histeq:g=histeq(f,nlev) 例3.5 直方图均衡化
%% 例3.5 直方图均衡化 默认为64 clc clear f = imread('Fig0308(a)(pollen).tif'); figure,subplot(121),imshow(f),subplot(122),imhist(f) ylim('auto') g = histeq(f,256); figure,subplot(121),imshow(g),subplot(122),imhist(g) ylim('auto') g = histeq(f,128); figure,subplot(121),imshow(g),subplot(122),imhist(g) ylim('auto') g = histeq(f); % 默认为64 figure,subplot(121),imshow(g),subplot(122),imhist(g) ylim('auto') g = histeq(f,8); figure,subplot(121),imshow(g),subplot(122),imhist(g) ylim('auto')
f = imread('Fig0303(a)(breast).tif');%读取原图像 h=imhist(f);%直方图函数 h1=h(1:10:256); horz=1:10:256;
Digital Image Processing
figure subplot(121),imshow(f),subplot(122),imhist(f)%绘制原图和默认直方图