# 1.基本线性灰度变换

Image =im2double(rgb2gray(imread('hehua.bmp')));
[h, w] = size(Image);
Image1 = zeros(h,w);
Image2 = zeros(h,w);
Image3 = zeros(h,w);
for i = 1:h
for j = 1:w
Image1(i,j)= Image(i, j)* tan(0.1 * pi);
Image2(i,j)= Image(i, j)* tan(0.25 * pi);
Image3(i,j)= Image(i, j)* tan(0.4 * pi);
end
end
subplot(221),imshow(Image),title('原图');
subplot(222),imshow(Image1),title('变暗');
subplot(223),imshow(Image2),title('不变');
subplot(224),imshow(Image3),title('变亮');

# 2.分段线性灰度变换

Image = imread('hehua.bmp');
Image = im2double(rgb2gray(Image));
[h, w] =size(Image);
% 分段线性灰度变换
Image1 = zeros(h, w); %(行，列)
a = 30/256;
b = 100/256;
c = 75/256;
d = 200/256;

for i=1:h
for j= 1:w
if Image(i,j)<=a
Image1(i,j) = c/a * Image(i, j);
elseif Image(i,j)<b
Image1(i,j) = (d-c)/(b-a)*(Image(i,j) - a) + c;
else
Image1(i,j) = (1-d)/(1-b)*(Image(i,j) - b) +d;
end
end
end
% 高低端灰度不变
Image2 = Image;
for x =1:h
for y=1:w
if a<Image(x, y)<b
Image2(x,y) = (d-c)/(b-a)*(Image(x,y) - a) + c;
end
end
end

% 截取式灰度变换
%[a;b]为要变换的灰度空间   [c,d]为要变换到的灰度空间
%小于a的被直接赋值为c   大于b的被直接赋值为b

subplot(141),imshow(Image),title('原图');
subplot(142),imshow(Image1),title('分段灰度变换');
subplot(143),imshow(Image2),title('高低端灰度不变');
subplot(144),imshow(Image3),title('截取式灰度变换');



Image = im2double(rgb2gray(imread('X.jpg')));
[h, w] = size(Image);
a= 170/256 ; b = 200/256; c= 35/256; d =250/256;
Image1 = zeros(h,w);
Image2 = Image;
for i=1:h
for j=1:w
if Image(i,j)<c
Image1(i,j)=c;
else
Image1(i,j)=d;
end
end
end

for x=1:h
for y=1:w
if c<Image(x,y)<b
Image2(x,y)=0;
end
end
end
subplot(131),imshow(Image),title('原图');
subplot(132),imshow(Image1),title('第一种切法');
subplot(133),imshow(Image2),title('第二种切法');

12-02

04-26
05-05
06-16 5602
07-01
11-19 1万+
10-24 1万+
03-11 882
07-05
10-12
10-22
04-01 6071
09-23 5144
04-19 5469