1. 程式人生 > >圖像灰度變換

圖像灰度變換

plot mco all 三種 mat 圖像 end msh tran

一、圖像求反

clear all;
clc;
a=imread(‘pout.tif‘);

% 圖像取反第一種方法
f=255-a;

%圖像取反第二種方法
img=imadjust(a,[0,1],[1,0]);

%圖像取反第三種方法
g=imcomplement(a);

二、通過imadjust函數對圖像亮度進行處理

a0=imadjust(a); %把圖片a的範圍拉伸到[0 1]

a1=imadjust(a,[0,1],[1,0]); %圖像取反---即負變換

a2=imadjust(a,[0.3 0.7],[0.1 1]); %從小到大--將圖片f 較小的灰度值變化區間擴展為較大的灰度值變化區間

a3=imadjust(a,[0 1],[0.2 0.6]);

%從大到小--將圖片f較大的灰度值變化區間壓縮為較小的灰度值變化區間達到降低圖像對比度的作用

a4=imadjust(a,[],[],2); %只改變gama參數

figure(1),

subplot(3,3,1);imshow(a);title(‘原圖像‘);

subplot(3,3,2);imshow(a0);title(‘亮度增強‘)

subplot(3,3,3);imshow(a1);title(‘負變換‘);

subplot(3,3,4);imshow(a2);title(‘從小到大變換,提高對比度‘);

subplot(3,3,5);imshow(a3);title(‘從大到小變換,降低對比度‘);

subplot(3,3,6);imshow(a4);title(‘只改變gama參數‘);

三、分段函數實現亮度提升

clc;

clear all;

img = imread(‘pout.tif‘);

%折線點賦值

f0=0;g0=0;

f1=60;g1=20;

f2=180;g2=220;

f3=255;g3=255;

r1=(g1-g0)/(f1-f0); %第一段折線的斜率

b1=g0-r1*f0; %計算截距1

r2=(g2-g1)/(f2-f1);

b2=g1-r2*f1;

r3=(g3-g2)/(f3-f2);

b3=g2-r3*f2;

[m,n]=size(img);

for i=1:m

for j=1:n

f=img(i,j);

if(f<f1)

g(i,j)=r1*f+b1;

elseif(f>=f1)&&(f<=f2)

g(i,j)=r2*f+b2;

elseif(f>=f2)&&(f<=f3)

g(i,j)=r3*f+b3;

end

end

end

figure(1),

subplot(221);imshow(img);title(‘原圖像‘);

subplot(222);plot([f0 f1 f2 f3],[g0 g1 g2 g3]);

axis tight,xlabel(‘f‘),ylabel(‘g‘),title(‘灰度變換曲線‘);

subplot(223);imshow(g);title(‘灰度變換後‘);

四、對數變換

f = imread(‘fruits.jpg‘);

I=rgb2gray(f);

f = mat2gray(I);%

v = 10;

g_1 = log2(1 + v*f)/log2(v+1);

v = 30;

g_2 = log2(1 + v*f)/log2(v+1);

v = 200;

g_3 = log2(1 + v*f)/log2(v+1);

figure(),

subplot(2,2,1);

imshow(f,[0 1]);

xlabel(‘a).Original Image‘);

subplot(2,2,2);

imshow(g_1,[0 1]);

xlabel(‘b).Log Transformations v=10‘);

subplot(2,2,3);

imshow(g_2,[0 1]);

xlabel(‘c).Log Transformations v=100‘);

subplot(2,2,4);

imshow(g_3,[0 1]);

xlabel(‘d).Log Transformations v=200‘);

% 調整gama參數的幾種情況% a0=imadjust(a); %把圖片a的範圍拉伸到[0 1]% a1=imadjust(a,[0,1],[1,0]); %圖像取反---即負變換% a2=imadjust(a,[0.3 0.7],[0.1 1]); %從小到大--將圖片f 較小的灰度值變化區間擴展為較大的灰度值變化區間% a3=imadjust(a,[0 1],[0.2 0.6]);% %從大到小--將圖片f較大的灰度值變化區間壓縮為較小的灰度值變化區間達到降低圖像對比度的作用% a4=imadjust(a,[],[],2); %只改變gama參數

圖像灰度變換