matlab实现图像增强

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011366281/article/details/51119733

下面是根据我自己的工作整理的空间域和频率域的图像增强,首先将彩色图像转化为灰度图像,matlab 代码如下:

<span style="font-size:18px;">clear all  
I1=imread('1.jpg');                  
imshow(I1)                                    
title('输入的彩色JPG图像')  
I = rgb2gray(I); %灰度化后的数据存入数组  
imwrite(I,'image_gray.bmp'); %保存灰度图像  
figure,imshow(I);  
title('灰度图') </span>

1、灰度变换

     灰度变换可以调整图像的灰度动态范围或图像对比度,是图像增强的重要手段之一。

1.1线性变换

     在图像曝光不足或过度的情况下,图像灰度可能会局限在一个很小的范围内。这时在显示器上看到的是一个模糊不清、似乎没有灰度层次的图像。对这种图像采用线性变换方法对像素灰度做线性拉伸,可有效改善图像视觉效果。

matlab代码如下:线性转化函数就是k*X+b


function [ new ] = LinearTransformFunc( original, k, d )
    new = original * k + b;
end

<span style="font-size:18px;">original = imread('2.bmp');
transformed = LinearTransformFunc(original, 2, 6);
figure
imshow(transformed)
imwrite(transformed,'lineartrans.jpg')</span>

1.2 分段线性变换

  为了突出感兴趣目标所在的灰度区间,相对抑制那些不感兴趣的灰度空间可以采用分段性变换


<span style="font-size:18px;">function [ new ] = StretchFunc(original, x1, y1, x2, y2 )
    new = original;
    w = size(new, 1);
    h = size(new, 2);
    k1 = y1 / x1;
    dk1 = (y2 - y1) / (x2 - x1);
    dk2 = (255 - y2) / (255 - x2);
    for i = 1 : w
        for j = 1 : h
            x = new(i, j);
            if x < x1
                new(i, j) = k1 * x;
            elseif x < x2
                new(i, j) = dk1 * (x - x1) + y1;
            else
                new(i, j) = dk2 * (x - x2) + y2;
            end
        end
    end
end
original = imread('image_gray.bmp');
transformed = StretchFunc(original,93,0,255,255); 
figure
imshow(transformed)
imwrite(transformed,'stretch.jpg')</span>

1.3  非线性灰度变换

    当用某些非线性函数如对数函数、指数函数等,作为映射函数时,可实现凸显灰度的非线性变换。

    对数变换:对图像的低灰度区有较大的拉伸,而对高灰度区进行压缩。

    指数变换:对图像的高灰度区进行较大的拉伸。

2、直方图均衡化

I = imread('3.jpg');
m = 16;
H = histeq(I,m);
imshow(H,[]);
title('均衡后的图像');

3、图像平滑

figure(2)
subplot(3,2,1);
imshow(I);
title('原图像');
% 加入椒盐噪声并显示
II = imnoise(I,'salt & pepper');
subplot(3,2,2);
imshow(II);
title('加入椒盐噪声后的图像');
% 低通滤波平滑
[B,A] = butter(6,0.2,'low');
J = filter(B,A,double(II));
subplot(3,2,3);
imshow(J,[]);
title('低通滤波平滑');
% 中值滤波平滑
J = medfilt2(II);
subplot(3,2,4);
imshow(J,[]);
title('中值滤波平滑');
% 同态滤波
[I0,M] = imread(fineName);
%I0 = II;M=[];
I1 = log(double(I0)+1);
I2 = fft2(I1);
N=2;D0=0.05*pi;rh=0.8;r=0.5;
[row,col]=size(I2);
for m=1:row
    for n=1:col
        D1(m,n)=sqrt(m^2+n^2);
        H(m,n)=r+(rh/(1+(D0/D1(m,n))^(2*N)));
    end
end
I3=I2.*single(H);
I4=ifft2(I3);
I5=exp(I4)-1;
 
subplot(3,2,5);
imshow(I0,M);
title('原图像');
subplot(3,2,6);
imshow(I5,M);
title('同态滤波后的图像');

4、图像锐化

ima=double(I);
h=fspecial('sobel');
bw1 = imfilter(ima,h); %sobel算子锐化
figure;subplot(121);</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">imshow(uint8(ima));title('原始图像');%图像显示
subplot(122);</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">imshow(bw1);title('sobel算子锐化');
bw2 = edge(ima,'prewitt');%prewitt算子锐化
figure;subplot(121);</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">imshow(uint8(ima));title('原始图像');
subplot(122);imshow(bw2);title('prewitt算子锐化');
bw3 = edge(ima,'roberts');%roberts算子锐化
figure;subplot(121);</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">imshow(uint8(ima));title('原始图像');
subplot(122);</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">imshow(bw3);title('roberts算子锐化');
bw4 = edge(ima,'log');%log算子锐化
figure;subplot(121);</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">imshow(uint8(ima));title('原始图像');
subplot(122);</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">imshow(bw4);title('log算子锐化');
bw5 = edge(ima,'canny');%canny算子锐化
figure;subplot(121);</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">imshow(uint8(ima));title('原始图像');
subplot(122);imshow(bw5);title('canny算子锐化');
h1=fspecial('gaussian',[9 9]);%gaussian低通滤波器锐化
bw6 = imfilter(ima,h1);
figure;subplot(121);</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">imshow(uint8(ima));title('原始图像');
subplot(122);
imshow(uint8(bw6));
title('gaussian低通滤波器锐化');

猜你喜欢

转载自blog.csdn.net/u011366281/article/details/51119733