灰度图线性变换(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);

版权声明:
作者:MWHLS
链接:https://mwhls.top/2132.html
来源:无镣之涯
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
< <上一篇
下一篇>>