Matlab实现真彩RGB图直方图计算
程序员文章站
2022-05-20 21:09:45
...
1 计算原理
真彩图有RGB三个通道,所以需要分开计算直方图。
计算原理:统计各通道各亮度等级出现的频度。
2 代码实现
实现代码:
%% Read in the image
lenaIMG = imread('lena512color.tiff');
%% My histogram code
% w = width, h = hight
[w, h, ~] = size(lenaIMG);
% Create a 1*256 D matrix, record the intensity value
hcountR = zeros([1 256]); % Red Channel
hcountG = zeros([1 256]); % Green Channel
hcountB = zeros([1 256]); % Blue Channel
for i = 1:w
for j = 1:h
% Accumulation
hcountR(lenaIMG(i, j, 1)) = hcount(lenaIMG(i, j, 1)) + 1;
hcountG(lenaIMG(i, j, 2)) = hcount(lenaIMG(i, j, 2)) + 1;
hcountB(lenaIMG(i, j, 3)) = hcount(lenaIMG(i, j, 3)) + 1;
end
end
% Draw the histogram
subplot(1,3,1);
stem(0:255, hcountR,'.');
title("Channel R");
xlabel('Intensity');
ylabel('Frequentness');
axis([0 255 0 3000]);
subplot(1,3,2);
stem(0:255, hcountG,'.');
title("Channel G");
xlabel('Intensity');
ylabel('Frequentness');
axis([0 255 0 3000]);
subplot(1,3,3);
stem(0:255, hcountB,'.');
title("Channel B");
xlabel('Intensity');
ylabel('Frequentness');
axis([0 255 0 3000]);
3 效果展示
读入图像:
直方图: