How to plot RGB histogram of an image into a single 3D slice plot?

14 vues (au cours des 30 derniers jours)
Mohammad
Mohammad le 16 Juil 2024
Commenté : Voss le 17 Juil 2024
Hello. Im working on color image encryption and need to plot the histogram for each color channels. I can plot the individual color channel histogram in three 2D plot with this code.
close all;
image = imread('peppers.png');
R = image(:,:,1);
G = image(:,:,2);
B = image(:,:,3);
[Width,Length] = size(image);
subplot(2,2,1); imshow(image);
title('Plain Image')
subplot(2,2,2);
imhist(R);
myHist1 = findobj(gca, 'Type', 'Stem');
myHist1.Color = [1 0 0];
title('Red Channel Plain Histogram')
subplot(2,2,3);
imhist(G);
myHist2 = findobj(gca, 'Type', 'Stem');
myHist2.Color = [0 1 0];
title('Green Channel Plain Histogram')
subplot(2,2,4);
imhist(B);
myHist3 = findobj(gca, 'Type', 'Stem');
myHist3.Color = [0 0 1];
xlim([0 256]);
title('Blue Channel Plain Histogram')
But, i tried to plot them together in a 3D plot like this below (picture from an article i found), but i cant. I tried to find any forum that talks about this, but i can only found slice plots of meshgrid, not from histogram.
If anyone could help, I would appreciate it so much.
Thank you!

Réponse acceptée

Voss
Voss le 16 Juil 2024
image = imread('peppers.png');
R = image(:,:,1);
G = image(:,:,2);
B = image(:,:,3);
subplot(2,2,1);
imshow(image);
title('Plain Image')
subplot(2,2,2);
imhist(R);
myHist1 = findobj(gca, 'Type', 'Stem');
myHist1.Color = [1 0 0];
xlim([0 255]);
title('Red Channel Plain Histogram')
subplot(2,2,3);
imhist(G);
myHist2 = findobj(gca, 'Type', 'Stem');
myHist2.Color = [0 1 0];
xlim([0 255]);
title('Green Channel Plain Histogram')
subplot(2,2,4);
imhist(B);
myHist3 = findobj(gca, 'Type', 'Stem');
myHist3.Color = [0 0 1];
xlim([0 255]);
title('Blue Channel Plain Histogram')
figure('Position',[10 10 500 500])
ax = gca();
myHist1_new = copyobj(myHist1,ax);
myHist1_new.ZData = myHist1_new.YData;
myHist1_new.YData = 1+zeros(size(myHist1_new.XData));
myHist2_new = copyobj(myHist2,ax);
myHist2_new.ZData = myHist2_new.YData;
myHist2_new.YData = 2+zeros(size(myHist2_new.XData));
myHist3_new = copyobj(myHist3,ax);
myHist3_new.ZData = myHist3_new.YData;
myHist3_new.YData = 3+zeros(size(myHist3_new.XData));
box on
grid on
xlim([0 255])
% ax.XDir = 'reverse'; % to match the xdir in the reference image
ylim([0 4])
yticks([1 2 3])
yticklabels(["R","G","B"]+" Channel")
view([30 35])
  2 commentaires
Mohammad
Mohammad le 17 Juil 2024
It works like a charm! I only have MATLAB 2014b though which yticks is yet undefined, but i successfully modify and run it.
Thank you!
Voss
Voss le 17 Juil 2024
You're welcome!

Connectez-vous pour commenter.

Plus de réponses (1)

Ashutosh Thakur
Ashutosh Thakur le 16 Juil 2024
Hello Mohammad,
You can explore the hist3 function available in MATLAB to create 3-D histogram plots. However, note that this function is specifically designed for bivariate data. You can refer to following link for the usage and the examples of the hist3 function: https://www.mathworks.com/help/stats/hist3.html.
You can also refer to the sample code for the usage of hist3 function:
x = randn(1000, 1); % 1000 random values from a normal distribution
y = randn(1000, 1); % 1000 random values from a normal distribution
% Combine the data into a single matrix
data = [x, y];
% Create a 3-D histogram plot
figure;
hist3(data, 'CdataMode', 'auto', 'FaceColor', 'interp');
% Set plot properties
title('3-D Histogram Plot');
xlabel('X-axis values');
ylabel('Y-axis values');
zlabel('Frequency');
colorbar; % Add a color bar to show the frequency scale
% Set view angle for better visualization
view(3);
grid on;
I hope this helps!

Produits


Version

R2014b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by