
% clear all
% 
I = imread('rgb.jpg'); 
figure,imshow(I);
Ir = I(:, :, 1); 
Ig = I(:, :, 2);
Ib = I(:, :, 3);

figure,imshow(Ir); colormap([[0:1/255:1]', zeros(256,1), zeros(256,1)]); 
% hold on, plot(675, 1076, '.k', 'MarkerSize',12); 
iptsetpref('ImshowBorder','tight'); saveas(gcf,'red.png'); 
figure,imshow(Ig); colormap([zeros(256,1),[0:1/255:1]', zeros(256,1)]); 
% hold on, plot(675, 1076, '.k', 'MarkerSize',12);
iptsetpref('ImshowBorder','tight'); saveas(gcf,'green.png'); 
figure,imshow(Ib); colormap([zeros(256,1), zeros(256,1), [0:1/255:1]']); 
% hold on, plot(675, 1076, '.k', 'MarkerSize',12);
iptsetpref('ImshowBorder','tight'); saveas(gcf,'blue.png'); 

% 
I=rgb2gray(I);                                       

[m,n] = size(I);                                       
GP = zeros(1,256);                                       
for k = 0 : 255
    num(k+1) = length(find(I==k));
    GP(k+1) = length(find(I==k))/(m*n);                
end
% figure, bar(0:255, GP, 'g')
% title('Histogram')
% xlabel('Gray scale')
% ylabel('Frequency')

% figure, bar(0:255, num, 'g')
% title('Histogram')
% xlabel('Gray scale')
% ylabel('Frequency')
% 
% fxy = imhist(I, 256); %stastic number of gray
% figure;
% plot(fxy); %plot histgram
% xlabel('Gray Value');
% ylabel('Frequency');

figure; imhist(I);
xlabel('Intensity value');
ylabel('Number of pixels');

figure;
[y_r, x] = imhist(Ir);
[y_g, x] = imhist(Ig);
[y_b, x] = imhist(Ib);
plot(x, y_r, 'r', x, y_g, '--g', x, y_b, ':b', 'LineWidth', 2.5);
legend('\color{red} Red', '\color{green} Green', '\color{blue} Blue', 'Location', 'best');
legend boxoff 
xlim([0 255]); ylim([0 80000]);
xlabel('Intensity value');
ylabel('Number of pixels');

% II = 0.299*Ir + 0.587*Ig + 0.114*Ib;
% figure; imhist(II);
% xlim([0 255]); ylim([0 80000]);
% figure,imshow(II)













