matlab 彩色圖像轉化成灰度圖像,灰度圖像降低灰度級


灰度級數k,k=2^b,稱該圖像為b比特圖像。

降低灰度級數是靠2的冪次方

網上代碼:https://blog.csdn.net/silence2015/article/details/68927360

function changereduce_factor(imgpath,reduce_factor)
% Write a computer program capable of reducing the number of intensity levels in an image from 256 to 2, 
% in integer powers of 2. The desired number of intensity levels needs to be a variable input to your program.
    f = imread(imgpath);
    if reduce_factor<0
        reduce_factor=0
    else if reduce_factor>8
            reduce_factor=8
        end
    end

    dfactor=uint8(2^reduce_factor);
    f_trans=(f/dfactor)*dfactor;
    subplot(1,2,1);
    imshow(f);
    subplot(1,2,2);
    imshow(f_trans);

end

其中reduce_factor=8表示原圖像是一張8bit圖像,有灰度級dfactor是256,

所以(f/dfactor)取整只有兩個結果0或者1,然后再乘灰度級就變成了黑白圖片(只有兩個灰度級)

那么當reduce不等於比特圖像那個值的時候會出現什么結果,如果大於,那么圖像是全黑的0,小於的話取整運算的結果將不只是0,1,還會出現更多數

但是還是小於比特圖像數,也就做到了將灰度值降低。

pic=imread('pic/coltogray/1.jpg');
##gray_pic=rgb2gray(pic);
##figure(1)
##imshow(gray_pic)

[x,y,z]=size(pic);

graypic=zeros(x,y);

level=4

dfactor=uint8(2^level)
max1=0;
min1=0;
for i=1:x
  for j=1:y
    sum=0;
    for k =1:z
      sum=sum+pic(i,j,k)/3;
    end
    graypic(i,j)=sum;
    graypic1(i,j)=(sum/dfactor)*dfactor;
  end
end

graypic=uint8(graypic);
graypic1=uint8(graypic1);

figure(1);
imshow(graypic);
figure(2);
imshow(graypic1);
%將三維圖像壓縮至一維即可以看作是灰度圖像?

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM