13.1 Image Enhancement and Compensation for Nonlinear Changes——Filters enhance images to improve visual quality (matlab program)

1. Brief description

      

The linear transformation and nonlinear transformation of the image, the pixel-by-pixel operation is to convert the brightness value of each pixel of the image to a new brightness value through a certain functional relationship. This conversion can be represented by a function:

s = f ( r ) s = f( r )s=f(r)

Among them, r is the original pixel value, s is the new pixel value, and the function usually adopted is a monotone function for transformation.

Linear transformation:

s ( x , y ) = c + kr ( x , y ) s(x,y) =c+kr(x,y)
s(x,y)=c+kr(x,y)

where c and k are constants

Nonlinear transformation:

s = a + l n ( r + 1 ) b l n c s=a+\frac {ln(r+1)} {blnc}
s=a+ 
blnc
ln(r+1)

 

Where a, b, c are constants

Gamma transformation:

s = c r γ s = cr^γ
s=cr 
γ
 

Where c is a constant, usually 1, γ is also a constant, the range of r is [0,255], usually scaled to [0,1]

The picture shows the situation when γ takes different values. For example, when the pixel value of the original image is 0.2, when γ=1.5, the pixel value of the current image is less than 0.2, and when γ=1, the pixel value of the current image is equal to 0.2. When γ =0.5, the pixel value of the current image is greater than 0.4.
 

2. Code


clear all;
%% 
C=double(imread('trees.tif'));       %读入图片
h=[1,1,1;1,1,1;1,1,1]./9;
B=C(:,:,1);
%B=imfilter(B,h,'replicate');
S=size(B);H=S(1,1);W=S(1,2);
t=0.75;%0.730174;
h0 = (8*t*t*t-6*t*t+3*t)/(1+2*t)*(1/32);
h1 = (-16*t*t*t+20*t*t-12*t+3)/(1+2*t)*(1/32);
h2 = (2*t-3)/(1+2*t)*(1/8.);
h3 = (16*t*t*t-20*t*t+28*t+5)/(1+2*t)*(1/32.);
h4 = (-8*t*t*t+6*t*t+5*t+20)/(1+2*t)*(1/16.);
hL=[h0,h1,h2,h3,h4,h3,h2,h1,h0];

g0=-t/16;
g1=(1-2*t)/16;
g2=(t+4)/16;
g3=(3+2*t)/8;
hH=[-g0,g1,- g2,g3,-g2,g1,-g0];
%% For SAR images submerged in noise, first use Wiener filter method to adaptively denoise
%Idenoise= medfilt2(B(:,:,1),[3 3] );% median filter
% Idenoise = wiener2(B(:,:,1),[5 5]);% adaptive filter
% Idenoise=double(Idenoise);
Idenoise=B;

%% Extract high-frequency edge BI with multi-resolution decomposition method

hL=[1/16,1/4,3/8,1/4,1/16];
for i=1:length(hL)
    for j=1:length(hL)
        hL2D(i,j)=hL(i)*hL(j);
    end
end
L0=imfilter(Idenoise,hL2D,'replicate');%低频近似图像L0
L0=double(L0);
L0max=max(max(L0));
c=0.3;s=2;
T1 = 0;
T2 = (1-c)*L0max;
k1=0.6; k2=s;

BI = Idenoise-L0;% First layer high frequency edge BI

 %% NewBI is obtained after nonlinear interpolation of BI
newBI=BI;

for i=1:H
    for j=1:W
        if abs(BI(i,j))<=T1
            newBI(i,j)=k1*BI(i,j);
        else if (abs(BI(i,j))>T1)&(abs(BI(i,j))<=T2)
                newBI(i,j)=sign(BI(i,j))*(k2*abs(BI(i,j))+T1*(k1-k2));
            end
        end
    end
end
%

%% Perform high-pass filtering on newBI to obtain enhanced high-frequency edge BI
hH=[-1/16,1/4,-3/8,1/4,-1/16];%[-1,2,- 1]/2;
for i=1:length(hH)
    for j=1:length(hH)
        hH2D(i,j)=hH(i)*hH(j);
    end
end

BI=imfilter(newBI,hH2D,'replicate'); % corrected edge


BI=double(BI);
Irecover=newBI+L0;

for i=1:H
    for j=1:W
        if Irecover(i,j)<0
            Irecover(i,j)=0;
        else if Irecover(i,j)>255
                Irecover(i,j)=255;
            end
        end
    end
end

%%
EP1D=[-0.15,0.25,0.7,0.25,-0.15];
for i=1:length(EP1D)
    for j=1:length(EP1D)
        EP2D(i,j)=EP1D(i)*EP1D(j);
    end
end
Irepro = imfilter(Irecover,EP2D,'replicate');


figure,imshow(uint8(B),[]);
title('Original image');
figure,imshow(uint8(Irecover),[]);
title(['High-frequency nonlinear enhanced image']);

figure,imshow(uint8(Irepro),[]);
title(['Non-linear enhancement and then compensated image']);

3. Running results

 

 

 

Guess you like

Origin blog.csdn.net/m0_57943157/article/details/131524306