文章目錄
- 數字影像處理-影像增強(Matlab)
- 1、對選定的灰度影像進行反色、線性變換、對數變換等基本處理,(線性變換函式自設)
- 1.1 灰度反轉
- 1.2 線性變換
- 1.3 對數變換
- 2、對選定的灰度影像進行直方圖均衡化處理,并顯示處理前后的直方圖,
- 2.1 直方圖均衡化
- 3、分別在兩幅灰度影像中加入一定量的高斯噪聲和椒鹽噪聲,噪聲強度自定,然后分別采用3x3的均值濾波和3x3中值濾波對噪聲進行處理,顯示結果影像,并計算出兩種處理方法的峰值信噪比(PSNR),
- 3.1 添加高斯噪聲和椒鹽噪聲
- 3.2 峰值信噪比PSNR
- 3.3 對高斯噪聲進行濾波并計算PSNR
- 3.4 對椒鹽噪聲進行濾波并計算PSNR
- 4、對選定的灰度影像進行銳化處理:先對原影像進行3*3均值濾波使其模糊,再分別通過Roberts算子、Sobel算子、拉普拉斯算子對其進行邊緣增強處理,顯示結果影像并對比各方法的結果
- 4.1 均值濾波
- 4.2 Roberts算子邊緣增強
- 4.3 Sobel算子邊緣增強
- 4.4 拉普拉斯算子邊緣增強
- 5、對選定的灰度影像進行巴特沃斯高通濾波處理:要求設定多種不同(高、中、低)的截止頻率進行濾波,顯示其經濾波后的空域影像
- 6、對選定的灰度影像進行理想低通濾波處理:要求設定多種不同(高、中、低)的截止頻率進行濾波,顯示其經濾波后的空域影像,
- 7、對選定的一幅RGB彩色影像(BMP格式),分別顯示該圖的R/G/B單色影像,繪制R/G/B單色影像的直方圖;將RGB彩色模式轉換為HIS模式,再顯示該圖的H/I/S三個分量的影像,
- 8、對一幅彩色RGB影像,采用對每一彩色分量進行拉普拉斯算子濾波,完成影像銳化處理,
數字影像處理-影像增強(Matlab)
1、對選定的灰度影像進行反色、線性變換、對數變換等基本處理,(線性變換函式自設)
1.1 灰度反轉
- 原理公式
g ( x , y ) = L ? 1 ? f ( x , y ) g(x,y)=L-1-f(x,y) g(x,y)=L?1?f(x,y)
其 中 L 為 圖 像 的 灰 度 級 數 其中L為影像的灰度級數 其中L為圖像的灰度級數 - Matlab代碼塊:
%-------------------------灰度反轉(Matlab代碼)-----------------------
clc; %清空控制臺
clear; %清空作業區
close all; %關閉已打開的figure影像視窗
color_pic=imread('lena512color.bmp'); %讀取彩色影像
gray_pic=rgb2gray(color_pic); %將彩色圖轉換成灰度圖
gray_reversal=256-1-gray_pic; % g(x,y)=L-1-f(x,y) 此圖灰度級數L為256級
figure('name','灰度反轉');
subplot(1,2,1);imshow(gray_pic,[]);title('原灰度圖');
subplot(1,2,2);imshow(gray_reversal,[]);title('灰度反轉圖');
- 運行效果:

1.2 線性變換
- 線性函式
g ( x , y ) = f ( x , y ) ? t a n α g(x,y)=f(x,y)*tan\alpha g(x,y)=f(x,y)?tanα
{ α > π 4 g ( x , y ) > f ( x , y ) 灰 度 拉 伸 α = π 4 g ( x , y ) = f ( x , y ) 灰 度 不 變 α < π 4 g ( x , y ) < f ( x , y ) 灰 度 壓 縮 \begin{cases} \alpha>\dfrac{\pi}{4} & g(x,y)>f(x,y)&灰度拉伸 \\ \\ \alpha=\dfrac{\pi}{4} &g(x,y) =f(x,y)&灰度不變 \\ \\ \alpha<\dfrac{\pi}{4} & g(x,y)<f(x,y)&灰度壓縮 \\ \end{cases} ??????????????????α>4π?α=4π?α<4π??g(x,y)>f(x,y)g(x,y)=f(x,y)g(x,y)<f(x,y)?灰度拉伸灰度不變灰度壓縮? - Matlab代碼塊:
%-------------------------線性變換(Matlab代碼)------------------------
clc; %清空控制臺
clear; %清空作業區
close all; %關閉已打開的figure影像視窗
color_pic=imread('lena512color.bmp'); %讀取彩色影像
gray_pic=rgb2gray(color_pic); %將彩色圖轉換成灰度圖
double_gray_pic=im2double(gray_pic);
linear_transform_reduce=double_gray_pic*tan(pi/6); %g(x,y)=f(x,y)*tan(a) 線性變換函式
linear_transform_increase=double_gray_pic*tan(pi/3); %當a<pi/4時,灰度壓縮,a>pi/4時,灰度拉伸
figure('name','線性變換');
subplot(1,3,1);imshow(gray_pic);title('原灰度圖');
subplot(1,3,2);imshow(im2uint8(linear_transform_reduce),[]);title('灰度壓縮'); %im2double后的影像用im2uint8轉換成uint8
subplot(1,3,3);imshow(im2uint8(linear_transform_increase),[]);title('灰度拉伸');
- 運行效果:

1.3 對數變換
- 對數函式
g ( x , y ) = C l o g [ 1 + f ( x , y ) ] g(x,y)=Clog[1+f(x,y)] g(x,y)=Clog[1+f(x,y)] - Matlab代碼塊:
%-------------------------對數變換(Matlab代碼)---------------------------
clc;
clear;
close all;
gray_pic=imread('log_picture.bmp'); %讀取灰度影像
double_gray_pic=im2double(gray_pic); %進行log運算得把uint8轉換成double型
log_transform=3*log(1+double_gray_pic); %對數變換g(x,y)=clog[1+f(x,y)],c=3
figure('name','對數變換');
subplot(1,2,1);imshow(gray_pic,[]);title('原灰度圖');
subplot(1,2,2);imshow(im2uint8(log_transform),[]);title('對數變換'); %im2double后的影像用im2uint8轉換成uint8
- 運行效果:

2、對選定的灰度影像進行直方圖均衡化處理,并顯示處理前后的直方圖,
2.1 直方圖均衡化
- Matlab代碼塊:
%-------------------------直方圖均衡化(Matlab代碼)-----------------------
clc; %清空控制臺
clear; %清空作業區
close all; %關閉已打開的figure影像視窗
color_pic=imread('lena512color.bmp'); %讀取彩色影像
gray_pic=rgb2gray(color_pic); %將彩色圖轉換成灰度圖
histogram_equalization=histeq(gray_pic); %呼叫histeq函式進行直方圖均衡化
figure('name','直方圖均衡化');
subplot(2,2,1);imshow(gray_pic,[]);title('原灰度圖');
subplot(2,2,2);imshow(histogram_equalization,[]);title('直方圖均衡化后灰度圖');
subplot(2,2,3);imhist(gray_pic,64);title('原直方圖'); %直方圖劃分成64個長度為4的灰度空間,方便查看效果
subplot(2,2,4);imhist(histogram_equalization,64);title('直方圖均衡化'); %直方圖劃分成64個長度為4的灰度空間,方便查看效果
- 運行效果:

3、分別在兩幅灰度影像中加入一定量的高斯噪聲和椒鹽噪聲,噪聲強度自定,然后分別采用3x3的均值濾波和3x3中值濾波對噪聲進行處理,顯示結果影像,并計算出兩種處理方法的峰值信噪比(PSNR),
3.1 添加高斯噪聲和椒鹽噪聲
- Matlab代碼塊:
%------------------------------添加噪聲(Matlab代碼)-----------------------------
clc; %清空控制臺
clear; %清空作業區
close all; %關閉已打開的figure影像視窗
color_pic=imread('lena512color.bmp'); %讀取彩色影像
gray_pic=rgb2gray(color_pic); %將彩色圖轉換成灰度圖
double_gray_pic=im2double(gray_pic); %將uint8轉成im2double型便于后期計算
Gaussian_noise=imnoise(double_gray_pic,'gaussian',0,0.01); %給灰度圖添加均值為0,方差為0.01的高斯噪聲
Salt_pepper_noise=imnoise(double_gray_pic,'salt & pepper',0.05); %給灰度圖添加噪聲密度為0.05的椒鹽噪聲
figure('name','加噪');
subplot(1,3,1);imshow(double_gray_pic,[]);title('原灰度圖');
subplot(1,3,2);imshow(Gaussian_noise,[]);title('加高斯噪聲');
subplot(1,3,3);imshow(Salt_pepper_noise,[]);title('加椒鹽噪聲');
- 運行效果:

3.2 峰值信噪比PSNR
-
原理公式:
均 方 差 ( M S E ) = 1 m n ∑ i = 0 m ? 1 ∑ j = 0 n ? 1 ∣ ∣ I ( i , j ) ? K ( i , j ) ∣ ∣ 2 均方差(MSE)=\dfrac{1}{mn}\sum_{i=0}^{m-1}\sum_{j=0}^{n-1}|| I(i,j)-K(i,j)||^{2} \\ 均方差(MSE)=mn1?i=0∑m?1?j=0∑n?1?∣∣I(i,j)?K(i,j)∣∣2
峰 值 信 噪 比 ( P S N R ) = 10 l o g 10 ( M A X I 2 M S E ) 峰值信噪比(PSNR)=10log_{10}\bigg(\dfrac{MAX_I^2}{MSE}\bigg) 峰值信噪比(PSNR)=10log10?(MSEMAXI2??)
其 中 M A X I = 圖 像 灰 度 級 數 ? 1 , 此 處 圖 片 灰 度 級 為 256 , 所 以 M A X I = 255 其中MAX_I=影像灰度級數-1,此處圖片灰度級為256,所以MAX_I=255 其中MAXI?=圖像灰度級數?1,此處圖片灰度級為256,所以MAXI?=255 -
Matlab代碼塊:
%------------------------matlab新建函式PSNR------------------------
function cal_PSNR = PSNR(img1,img2)
[width,height]=size(img1);
double_img1=im2double(img1); %計算影像峰值信噪比,需將2張影像從uint8型轉成im2double型
double_img2=im2double(img2);
matrix_subtraction=double_img1-double_img2;
MSE=sum(sum(matrix_subtraction.^2))/(width*height);
cal_PSNR=10*log10(255*2/MSE);
end
3.3 對高斯噪聲進行濾波并計算PSNR
- Matlab代碼塊:
%-----------------------------對高斯噪聲進行濾波并計算PSNR (Matlab代碼)-------------------------------
gaussian_mean_filter=filter2(fspecial('average',3),Gaussian_noise); %均值濾波
gau_mean_filter_snr=PSNR(double_gray_pic,gaussian_mean_filter); %均值濾波后的峰值信噪比
gaussian_median_filter=medfilt2(Gaussian_noise,[3,3]); %中值濾波
gau_median_filter_snr=PSNR(double_gray_pic,gaussian_median_filter); %中值濾波后的峰值信噪比
figure('name','對高斯噪聲進行濾波');
subplot(1,2,1);imshow(gaussian_mean_filter,[]);title(['均值濾波,PSNR:',num2str(gau_mean_filter_snr),'dB']);
subplot(1,2,2);imshow(gaussian_median_filter,[]);title(['中值濾波,PSNR:',num2str(gau_median_filter_snr),'dB']);
- 運行效果:

3.4 對椒鹽噪聲進行濾波并計算PSNR
- Matlab代碼塊:
%-----------------------------對椒鹽噪聲進行濾波并計算PSNR (Matlab代碼)-------------------------------
salt_mean_filter=filter2(fspecial('average',3),Salt_pepper_noise); %均值濾波
salt_mean_filter_snr=PSNR(double_gray_pic,salt_mean_filter); %均值濾波后的峰值信噪比
salt_median_filter=medfilt2(Salt_pepper_noise,[3,3]); %中值濾波
salt_median_filter_snr=PSNR(double_gray_pic,salt_median_filter); %中值濾波后的峰值信噪比
figure('name','對椒鹽噪聲進行濾波');
subplot(1,2,1);imshow(salt_mean_filter,[]);title(['均值濾波,PSNR:',num2str(salt_mean_filter_snr),'dB']);
subplot(1,2,2);imshow(salt_median_filter,[]);title(['中值濾波,PSNR:',num2str(salt_median_filter_snr),'dB']);
- 運行效果:

4、對選定的灰度影像進行銳化處理:先對原影像進行3*3均值濾波使其模糊,再分別通過Roberts算子、Sobel算子、拉普拉斯算子對其進行邊緣增強處理,顯示結果影像并對比各方法的結果
4.1 均值濾波
- Matlab代碼塊:
%-----------------------------------3*3均值濾波模糊影像(Matlab代碼)----------------------------
clc; %清空控制臺
clear; %清空作業區
close all; %關閉已打開的figure影像視窗
color_pic=imread('lena512color.bmp'); %讀取彩色影像
gray_pic=rgb2gray(color_pic); %將彩色圖轉換成灰度圖
double_gray_pic=im2double(gray_pic); %將uint8轉成im2double型便于后期計算
mean_filter=filter2(fspecial('average',3),double_gray_pic); %均值濾波
[width,height]=size(mean_filter);
figure('name','3*3均值濾波');
subplot(1,2,1);imshow(double_gray_pic,[]);title('原灰度圖');
subplot(1,2,2);imshow(mean_filter,[]);title('3*3均值濾波');
- 運行效果:

4.2 Roberts算子邊緣增強
- 原理公式
? f = ∣ f ( x , y ) ? f ( x + 1 , y + 1 ) ∣ + ∣ f ( x + 1 , y ) ? f ( x , y + 1 ) ∣ \nabla f=|f(x,y)-f(x+1,y+1)|+|f(x+1,y)-f(x,y+1)| ?f=∣f(x,y)?f(x+1,y+1)∣+∣f(x+1,y)?f(x,y+1)∣
H 1 = [ 1 0 0 ? 1 ] H 2 = [ 0 1 ? 1 0 ] H_1=\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix} \ \ \ \ \ \ \ H_2=\begin{bmatrix} 0 & 1 \\ -1 & 0 \end{bmatrix} H1?=[10?0?1?] H2?=[0?1?10?] - Matlab代碼塊:
% ------------------------------Roberts算子邊緣增強(Matlab)--------------------------------
roberts_img = zeros(width,height); %預先分配記憶體空間,提高運行速率
for i=1:width-1
for j=1:height-1
roberts_img(i,j)=abs(mean_filter(i+1,j)-mean_filter(i,j+1))+abs(mean_filter(i,j)-mean_filter(i+1,j+1));
end
end
figure('name','roberts算子');
subplot(2,2,1);imshow(double_gray_pic,[]);title('原灰度圖');
subplot(2,2,2);imshow(mean_filter,[]);title('3x3均值濾波');
subplot(2,2,3);imshow(im2uint8(roberts_img),[]);title('roberts算子的影像');
subplot(2,2,4);imshow(im2uint8(mean_filter+roberts_img),[]);title('roberts算子銳化影像');
- 運行效果:

4.3 Sobel算子邊緣增強
-
原理公式

-
Matlab代碼塊:
% ------------------------------Sobel算子邊緣增強(Matlab代碼)--------------------------------
sobel_img=zeros(width,height); %預先分配記憶體空間,提高運行速率
sobel_x=zeros(width,height); %預先分配記憶體空間,提高運行速率
sobel_y=zeros(width,height); %預先分配記憶體空間,提高運行速率
for i=2:width-1
for j=2:height-1
sobel_y(i,j)=abs(mean_filter(i-1,j+1)+2*mean_filter(i,j+1)+mean_filter(i+1,j+1)-mean_filter(i-1,j-1)-2*mean_filter(i,j-1)-mean_filter(i+1,j-1));
sobel_x(i,j)=abs(mean_filter(i+1,j+1)+2*mean_filter(i+1,j)+mean_filter(i+1,j-1)-mean_filter(i-1,j+1)-2*mean_filter(i-1,j)-mean_filter(i-1,j-1));
sobel_img(i,j)=0.3*sobel_x(i,j)+0.3*sobel_y(i,j);
end
end
figure('name','sobel算子');
subplot(2,2,1);imshow(double_gray_pic,[]);title('原灰度圖');
subplot(2,2,2);imshow(mean_filter,[]);title('3x3均值濾波');
subplot(2,2,3);imshow(im2uint8(sobel_img),[]);title('sobel算子的影像');
subplot(2,2,4);imshow(im2uint8(mean_filter+sobel_img),[]);title('sobel算子銳化影像');
- 運行效果:

4.4 拉普拉斯算子邊緣增強
- 原理公式
? 2 f = f ( x + 1 , y ) + f ( x ? 1 , y ) + f ( x , y + 1 ) + f ( x , y ? 1 ) ? 4 f ( x , y ) \nabla^2f=f(x+1,y)+f(x-1,y)+f(x,y+1)+f(x,y-1)-4f(x,y) ?2f=f(x+1,y)+f(x?1,y)+f(x,y+1)+f(x,y?1)?4f(x,y)
H = [ 0 1 0 1 ? 4 1 0 1 0 ] H=\begin{bmatrix} 0 & 1 & 0 \\ 1 & -4 & 1 \\ 0 & 1 & 0 \end{bmatrix} H=???010?1?41?010???? - Matlab代碼塊:
% ------------------------------Laplace算子邊緣增強(Matlab代碼)----------------------------------
laplace_img = zeros(width,height); %預先分配記憶體空間,提高運行速率
for i=2:width-1
for j=2:height-1
laplace_img(i,j)=mean_filter(i+1,j)+mean_filter(i-1,j)+mean_filter(i,j+1)+mean_filter(i,j-1)-4*mean_filter(i,j);
end
end
figure('name','laplace算子');
subplot(2,2,1);imshow(double_gray_pic,[]);title('原灰度圖');
subplot(2,2,2);imshow(mean_filter,[]);title('3x3均值濾波');
subplot(2,2,3);imshow(im2uint8(laplace_img),[]);title('laplace算子的影像');
subplot(2,2,4);imshow(im2uint8(mean_filter-laplace_img),[]);title('laplace算子銳化影像');
%由于采用的拉普拉斯算子中心是-4為負數,所以最后影像銳化是將兩幅圖相減
- 運行效果:

5、對選定的灰度影像進行巴特沃斯高通濾波處理:要求設定多種不同(高、中、低)的截止頻率進行濾波,顯示其經濾波后的空域影像
- 基本原理:

- Matlab代碼塊:
%************************5、對選定的灰度影像進行巴特沃斯高通濾波處理:要求設定多種不同(高、中、低)的截止頻率進行濾波,顯示其經濾波后的空域影像**************************
clc; %清空控制臺
clear; %清空作業區
close all; %關閉已打開的figure影像視窗
color_pic=imread('lena512color.bmp'); %讀取彩色影像
gray_pic=rgb2gray(color_pic); %將彩色圖轉換成灰度圖
double_gray_pic=im2double(gray_pic); %將uint8轉成im2double型便于后期計算
[width,height]=size(double_gray_pic);
mid_w=width/2; %影像中心點橫坐標
mid_h=height/2; %影像中心點縱坐標
fourier_pic=fft2(double_gray_pic); %對灰度圖進行傅里葉變換
fourier_shift=fftshift(fourier_pic); %將頻譜圖中零頻率成分移動至頻譜圖中心
level=2; %二階巴特沃茲
end_radius=[5,30,83]; %設定截止頻率
result1=zeros(width,height); %預先分配記憶體空間,提高運行速率
result2=zeros(width,height); %預先分配記憶體空間,提高運行速率
result3=zeros(width,height); %預先分配記憶體空間,提高運行速率
for i=1:width
for j=1:height
distance=sqrt((i-mid_w)^2+(j-mid_h)^2); %計算點(x,y)到中心點的距離
h1=1/(1+(end_radius(1)/distance)^(2*level)); %計算巴特沃斯濾波器
h2=1/(1+(end_radius(2)/distance)^(2*level));
h3=1/(1+(end_radius(3)/distance)^(2*level));
result1(i,j)=fourier_shift(i,j)*h1; %用濾波器乘以主函式
result2(i,j)=fourier_shift(i,j)*h2;
result3(i,j)=fourier_shift(i,j)*h3;
end
end
output1=im2uint8(real(ifft2(ifftshift(result1)))); %最終輸出要記得頻譜搬移回去
output2=im2uint8(real(ifft2(ifftshift(result2))));
output3=im2uint8(real(ifft2(ifftshift(result3))));
figure('name','巴特沃茲高通濾波器');
subplot(2,2,1);imshow(double_gray_pic);title('原灰度圖');
subplot(2,2,2);imshow(output1,[]);title(['巴特沃茲高通濾波 D0=',num2str(end_radius(1))]);
subplot(2,2,3);imshow(output2,[]);title(['巴特沃茲高通濾波 D0=',num2str(end_radius(2))]);
subplot(2,2,4);imshow(output3,[]);title(['巴特沃茲高通濾波 D0=',num2str(end_radius(3))]);
- 運行效果:

6、對選定的灰度影像進行理想低通濾波處理:要求設定多種不同(高、中、低)的截止頻率進行濾波,顯示其經濾波后的空域影像,
- 基本原理:

- Matlab代碼塊:
%************************6、對選定的灰度影像進行理想低通濾波處理:要求設定多種不同(高、中、低)的截止頻率進行濾波,顯示其經濾波后的空域影像**************************
clc; %清空控制臺
clear; %清空作業區
close all; %關閉已打開的figure影像視窗
color_pic=imread('lena512color.bmp'); %讀取彩色影像
gray_pic=rgb2gray(color_pic); %將彩色圖轉換成灰度圖
double_gray_pic=im2double(gray_pic); %將uint8轉成im2double型便于后期計算
[width,height]=size(double_gray_pic);
mid_w=width/2; %影像中心點橫坐標
mid_h=height/2; %影像中心點縱坐標
fourier_pic=fft2(double_gray_pic); %對灰度圖進行傅里葉變換
fourier_shift=fftshift(fourier_pic); %將頻譜圖中零頻率成分移動至頻譜圖中心
end_radius=[5,30,83]; %設定截止頻率
Result=zeros(width,height); %預先分配記憶體空間,提高運行速率
figure('name','理想低通濾波器');
subplot(2,2,1);imshow(double_gray_pic,[]);title('原灰度圖');
for k=1:3
Result=fourier_shift;
for i=1:width
for j=1:height
distance=sqrt((i-mid_w)^2+(j-mid_h)^2); %計算點(x,y)到中心點的距離
if distance>end_radius(k) %如果距離大于截止頻率,則濾除分量,直接置0
Result(i,j)=0;
end
end
end
output=im2uint8(real(ifft2(ifftshift(Result)))); %最終輸出要記得頻譜搬移回去
subplot(2,2,k+1);imshow(output,[]);title(['理想低通濾波器 D0=',num2str(end_radius(k))]);
end
- 運行效果:

7、對選定的一幅RGB彩色影像(BMP格式),分別顯示該圖的R/G/B單色影像,繪制R/G/B單色影像的直方圖;將RGB彩色模式轉換為HIS模式,再顯示該圖的H/I/S三個分量的影像,
- RGB轉HSI公式:

- Matlab代碼塊:
%************************7、對選定的一幅RGB彩色影像(BMP格式),分別顯示該圖的R/G/B單色影像,繪制R/G/B單色影像的直方圖;將RGB彩色模式轉換為HIS模式,再顯示該圖的H/I/S三個分量的影像**************************
clc; %清空控制臺
clear; %清空作業區
close all; %關閉已打開的figure影像視窗
color_pic=imread('lena512color.bmp'); %讀取彩色影像
double_color_pic=im2double(color_pic); %將uint8轉成im2double型便于后期計算
%----------------------------分別提取R/G/B三個通道影像---------------------------
R=double_color_pic(:,:,1);
G=double_color_pic(:,:,2);
B=double_color_pic(:,:,3);
figure('name','提取R/G/B影像');
subplot(2,2,1);imshow(double_color_pic,[]);title('原彩色影像');
subplot(2,2,2);imshow(R,[]);title('R');
subplot(2,2,3);imshow(G,[]);title('G');
subplot(2,2,4);imshow(B,[]);title('B');
figure('name','R/G/B單色影像直方圖');
subplot(3,2,1);imshow(R,[]);title('R');
subplot(3,2,2);imhist(R,128);title('R直方圖');
subplot(3,2,3);imshow(G,[]);title('G');
subplot(3,2,4);imhist(G,128);title('G直方圖');
subplot(3,2,5);imshow(B,[]);title('B');
subplot(3,2,6);imhist(B,128);title('B直方圖');
%----------------------------RGB轉HSI------------------------------
num=0.5*((R-G)+(R-B));
den=sqrt((R-G).^2+(R-B).*(G-B));
theta=acos(num./(den+eps)); %eps=2.2204e-16 防止分母為0
H=theta;
H(B>G)=2*pi-H(B>G);
H=H/(2*pi); %歸一化處理
num=min(min(R,G),B);
den=R+G+B;
den(den==0)=eps; %eps=2.2204e-16 防止分母為0
S=1-3*num./den;
H(S==0)=0; %完全不飽和的顏色根本沒有色調 即飽和度為0時色調無定義置0
I=(R+G+B)/3;
HSI=cat(3,H,S,I); %合并三個圖層
figure('name','HSI影像');
subplot(2,2,1);imshow(HSI,[]);title('HSI影像');
subplot(2,2,2);imshow(H,[]);title('H分量,色調');
subplot(2,2,3);imshow(S,[]);title('S分量,飽和度');
subplot(2,2,4);imshow(I,[]);title('I分量,亮度');
- 運行效果:



8、對一幅彩色RGB影像,采用對每一彩色分量進行拉普拉斯算子濾波,完成影像銳化處理,
- 原理公式
? 2 f = f ( x + 1 , y ) + f ( x ? 1 , y ) + f ( x , y + 1 ) + f ( x , y ? 1 ) ? 4 f ( x , y ) \nabla^2f=f(x+1,y)+f(x-1,y)+f(x,y+1)+f(x,y-1)-4f(x,y) ?2f=f(x+1,y)+f(x?1,y)+f(x,y+1)+f(x,y?1)?4f(x,y)
H = [ 0 1 0 1 ? 4 1 0 1 0 ] H=\begin{bmatrix} 0 & 1 & 0 \\ 1 & -4 & 1 \\ 0 & 1 & 0 \end{bmatrix} H=???010?1?41?010???? - Matlab代碼塊:
%************************8、對一幅彩色RGB影像,采用對每一彩色分量進行拉普拉斯算子濾波,完成影像銳化處理**************************
clc; %清空控制臺
clear; %清空作業區
close all; %關閉已打開的figure影像視窗
color_pic=imread('lena512color.bmp'); %讀取彩色影像
double_color_pic=im2double(color_pic); %將uint8轉成im2double型便于后期計算
%-----先提取三個圖層------
R=double_color_pic(:,:,1); %提取R圖層
G=double_color_pic(:,:,2); %提取G圖層
B=double_color_pic(:,:,3); %提取B圖層
[width,height]=size(R);
laplace_imgR = zeros(width,height); %預先分配記憶體空間,提高運行速率
laplace_imgG = zeros(width,height); %預先分配記憶體空間,提高運行速率
laplace_imgB = zeros(width,height); %預先分配記憶體空間,提高運行速率
%--------分別對R/G/B圖層進行拉普拉斯算子濾波-------
for i=2:width-1
for j=2:height-1
laplace_imgR(i,j)=R(i+1,j)+R(i-1,j)+R(i,j+1)+R(i,j-1)-4*R(i,j);
laplace_imgG(i,j)=G(i+1,j)+G(i-1,j)+G(i,j+1)+G(i,j-1)-4*G(i,j);
laplace_imgB(i,j)=B(i+1,j)+B(i-1,j)+B(i,j+1)+B(i,j-1)-4*B(i,j);
end
end
output=cat(3,laplace_imgR,laplace_imgG,laplace_imgB); %合并三個經拉普拉斯算子濾波后的圖層
figure('name','RGB拉普拉斯算子銳化');
subplot(2,2,1);imshow(laplace_imgR,[]);title('R分量拉普拉斯濾波');
subplot(2,2,2);imshow(laplace_imgG,[]);title('G分量拉普拉斯濾波');
subplot(2,2,3);imshow(laplace_imgB,[]);title('B分量拉普拉斯濾波');
subplot(2,2,4);imshow(output,[]);title('合成影像拉普拉斯濾波');
figure('name','三通道銳化后的影像');
subplot(1,2,1);imshow(color_pic,[]);title('原彩色圖');
subplot(1,2,2);imshow(im2uint8(double_color_pic-output));title('拉普拉斯銳化');
- 運行效果:


轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/201791.html
標籤:其他
下一篇:【資料結構——樹和森林】
