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); %将三维图像压缩至一维即可以看作是灰度图像?
无情的摸鱼机器