灰度图线性变换(Matlab)

文章首发及后续更新:https://mwhls.top/2132.html
新的更新内容请到mwhls.top查看。
无图/无目录/格式错误/更多相关请到上方的文章首发页面查看。

线性变换公式

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);
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值