灰度图线性变换(Matlab)
线性变换公式
y = ax + b
c , x < a
y = (d-c)/(b-a) * x + c , a <= x < b
d , b <= x
执行结果
- 从左到右,从上到下,图1为原图,图2为公式1处理后图像,图3为公式2处理后图像。
- 每张图下面为其灰度直方图。
代码
clear all;
ima = imread('1.jpg');
grayIma = rgb2gray(ima);
array = zeros(1, 256);
[height, width] = size(grayIma);
for row = 1:1:height
for col = 1:1:width
pos = grayIma(row, col);
array(1, pos) = array(1, pos) + 1;
end
end
subplot(2,3,1);
imshow(grayIma);
subplot(2,3,4);
bar(array);
grayImaLinear = 256 - grayIma;
array = zeros(1, 256);
[height, width] = size(grayIma);
for row = 1:1:height
for col = 1:1:width
pos = grayImaLinear(row, col);
array(1, pos) = array(1, pos) + 1;
end
end
subplot(2,3,2);
imshow(grayImaLinear);
subplot(2,3,5);
bar(array);
grayImaLinearSegment = grayIma;
a = 100;
b = 160;
c = 100;
d = 160;
for row = 1:1:height
for col = 1:1:width
temp = grayImaLinearSegment(row, col);
if(temp < a)
grayImaLinearSegment(row, col) = c;
elseif(temp < b)
grayImaLinearSegment(row, col) = (d - c) / (b - a) * (temp - a) + c;
else
grayImaLinearSegment(row, col) = d;
end
end
end
array = zeros(1, 256);
[height, width] = size(grayIma);
for row = 1:1:height
for col = 1:1:width
pos = grayImaLinearSegment(row, col);
array(1, pos) = array(1, pos) + 1;
end
end
subplot(2,3,3);
imshow(grayImaLinearSegment);
subplot(2,3,6);
bar(array);
共有 0 条评论