灰度图线性变换(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)
matlab直方图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);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论