欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

matlab中imadjust()函数做灰度图像的灰度变换

程序员文章站 2024-03-15 08:51:59
...

图像矩阵g有些为double类型,有些为uint8类型,揪心。
公式:g(x,y)=low_g + (high_g - low_g) / (high_f - low_f)*(f(x,y) - low_f)

clear
I=imread('C:\Users\lenovo\Desktop\1.jpg');
I=rgb2gray(I);%将彩色图像转换为灰度图像
imshow(I);
J=imadjust(I,[0.3;0.6],[0.1;0.9]);
figure;imshow(J);
low_f=0.3*255;high_f=0.6*255;low_g=0.1*255;high_g=0.9*255;
k=(high_g-low_g)/(high_f-low_f);
[M,N]=size(I);
for i=1:M
    for j=1:N
        if (I(i, j) <= low_f)                                        %灰度值小于low_f的像素点
            g(i,j) = low_g;                                         %结果为low_g
        elseif (low_f < I(i, j) && I(i, j) < high_f)        %灰度值在[low_f, high_f]
            g(i,j)=(low_g+k*(I(i,j)-low_f));
        else
            g(i,j) = high_g;                                       %灰度值大于high_f的像素点,结果为high_g
        end
    end
end
figure;imshow(g);