测试环境:Win10+Matlab2018
1.随机生成灰度图
X = rand(25, 25);
Y = round(255*X);
class(Y) %判断类型
Z = 255 - Y; %将灰度值进行反转
subplot(1,2,1)
imshow(uint8(Y))
xlabel('Y')
subplot(1,2,2)
imshow(uint8(Z))
xlabel('Z')
2.随机生成彩色图
rgb = rand(25,25,3)
Im_color = round(rgb*255);
figure(4)
size(Im_color)
class(Im_color)
imshow(uint8(Im_color))
3.读一幅图像,将颜色翻转再输出
B = imread('lighthouse.png');
C = 255 - B;
subplot(1,2,1)
imshow(B)
xlabel('B')
subplot(1,2,2)
imshow(C)
xlabel('C')
4.读图并绘制其直方图
Img = imread('lighthouse.png');
Img = rgb2gray(Img);
count = tabulate(Img(:)); %统计灰度值的频率[灰度值;频率;百分比]
temp = count(:,2);
[c,m] = max(temp); %找到频率最大的灰度值
subplot(1,2,1)
imshow(Img)
xlabel('灰度图')
subplot(1,2,2)
bar(count(:,2))
text(m,temp(m),num2str(m),'VerticalAlignment','bottom',...
'HorizontalAlignment','center','FontSize',9,'color','r','FontWeight','bold')
xlabel('灰度直方图')