How to plot histogram of multiple images in a for loop and save them in different figures?

Hello,
I have 20 images in a directory. I want to plot histogram for each image in a for loop and then store the histogram (each in a seperate figure) in a folder. This is the code that I wrote but it does not generate histogram.Could you guide me how to do that?
Thanks
srcFile = dir('./Registered_to_1_Metastatic8 copy 2/*.jpg');
for k = 1 :length(srcFile)
nextFilename = strcat('./Registered_to_1_Metastatic8 copy 2/',srcFile(k).name);
next_An_Img = imread(nextFilename);
An_gray_Img = rgb2gray(next_An_Img);
figure
imshow(An_gray_Img )
figure
h = imhist(An_gray_Img );
imwrite(h,['histo' num2str(k) '.jpg'],'jpg');
end

5 commentaires

KSSV
KSSV le 1 Avr 2019
Modifié(e) : KSSV le 1 Avr 2019
For logicals you will not get histgogram......... you have only 1's and 0's. Think what you want for histogram.
My mistake. I edited the program, but still doesnt work. I want histograms of all grayscale images in the folder.
Wait for some time....if your images are huge..it will take some time to get plotted.
srcFile = dir('./Registered_to_1_Metastatic8 copy 2/*.jpg');
for k = 1 :length(srcFile)
nextFilename = strcat('./Registered_to_1_Metastatic8 copy 2/',srcFile(k).name);
next_An_Img = imread(nextFilename);
An_gray_Img = rgb2gray(next_An_Img);
figure
imshow(An_gray_Img )
figure
imhist(An_gray_Img );
% imwrite(h,['histo' num2str(k) '.jpg'],'jpg');
saveas(gcf,['histo' num2str(k) '.jpg'])
end
Thank you so much. It worked. Your answered this question in the comments section not in the Answers , so I could not figure it out how to accept the answer.
I have given in answer now.

Connectez-vous pour commenter.

 Réponse acceptée

Wait for some time....if your images are huge..it will take some time to get plotted.
srcFile = dir('./Registered_to_1_Metastatic8 copy 2/*.jpg');
for k = 1 :length(srcFile)
nextFilename = strcat('./Registered_to_1_Metastatic8 copy 2/',srcFile(k).name);
next_An_Img = imread(nextFilename);
An_gray_Img = rgb2gray(next_An_Img);
figure
imshow(An_gray_Img )
figure
imhist(An_gray_Img );
% imwrite(h,['histo' num2str(k) '.jpg'],'jpg');
saveas(gcf,['histo' num2str(k) '.jpg'])
end

Plus de réponses (1)

Hello,
I have 20 images in a folder. I want generate histogram for these 20 image (single histogram for 20 images).Could you guide me how to do that?
Thanks

Community Treasure Hunt

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

Start Hunting!

Translated by