图像灰度变换

一、图像求反

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参数

原文地址:https://www.cnblogs.com/geeker-xjl/p/9581651.html