这篇文章主要讲解了“怎么用Matlab实现图像亮度分布统计图”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么用Matlab实现图像亮度分布统计图”吧!
写了一个输入图片,便会生成美观的图像各通道亮度分布图的函数,大概效果如下:
老样子,工具函数放在最前面,之后会介绍咋用这个函数:
工具函数
function HistogramPic(pic)FreqNum=zeros(size(pic,3),256);for i=1:size(pic,3) for j=0:255 FreqNum(i,j+1)=sum(sum(pic(:,:,i)==j)); endendax=gca;hold(ax,'on');box on;grid onif size(FreqNum,1)==3 bar(0:255,FreqNum(1,:),'FaceColor',[0.6350 0.0780 0.1840],'FaceAlpha',0.5); bar(0:255,FreqNum(2,:),'FaceColor',[0.2400 0.5300 0.0900],'FaceAlpha',0.5); bar(0:255,FreqNum(3,:),'FaceColor',[0 0.4470 0.7410],'FaceAlpha',0.5); ax.XLabel.String='RGB brightness'; rrange=[num2str(min(pic(:,:,1),[],[1,2])),' , ',num2str(max(pic(:,:,1),[],[1,2]))]; grange=[num2str(min(pic(:,:,2),[],[1,2])),' , ',num2str(max(pic(:,:,2),[],[1,2]))]; brange=[num2str(min(pic(:,:,3),[],[1,2])),' , ',num2str(max(pic(:,:,3),[],[1,2]))]; legend({['R: range[',rrange,']'],['G: range[',grange,']'],['B: range[',brange,']']},... 'Location','northwest','Color',[0.9412 0.9412 0.9412],... 'FontName','Cambria','LineWidth',0.8,'FontSize',11);else bar(0:255,FreqNum(1,:),'FaceColor',[0.50 0.50 0.50],'FaceAlpha',0.5); ax.XLabel.String='Gray scale'; krange=[num2str(min(pic(:,:,1),[],[1,2])),' , ',num2str(max(pic(:,:,1),[],[1,2]))]; legend(['Gray: range[',krange,']'],... 'Location','northwest','Color',[0.9412 0.9412 0.9412],... 'FontName','Cambria','LineWidth',0.8,'FontSize',11);endax.LineWidth=1;ax.GridLineStyle='--';ax.XLim=[-5 255];ax.XTick=[0:45:255,255];ax.YLabel.String='Frequency number';ax.FontName='Cambria';ax.FontSize=13;end
函数调用
非常简单的使用方法,就是读取图片后调用函数即可:
pic=imread('test.png');HistogramPic(pic)
若图像是彩图则效果如下:
若图像为灰度图则效果如下:
感谢各位的阅读,以上就是“怎么用Matlab实现图像亮度分布统计图”的内容了,经过本文的学习后,相信大家对怎么用Matlab实现图像亮度分布统计图这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!