how to store histograms
4 views (last 30 days)
Show older comments
i want to store histograms of various images separately and use them afterwards for histogram matching.
how can i store the histograms and create a proper database ?
please help
0 Comments
Answers (1)
Anudeep Kumar
on 26 Jun 2025
To store histograms of various images, you can use 'imhist' to calculate the histogram of image data and then MATLAB struct and hold and save it in a .mat file.
You can refer to the below sample code with appropriate changes to the 'imageFolder' and 'imageFiles'
% Set the folder containing your images
imageFolder = 'Enter_your_Image_path';
imageFiles = dir(fullfile(imageFolder, '*.jpg')); % Change extension if needed
% Choose histogram type: 'grayscale' or 'rgb'
histType = 'grayscale';
% Create a structure to store histograms
histDB = struct();
for i = 1:length(imageFiles)
% Read image
imgPath = fullfile(imageFolder, imageFiles(i).name);
img = imread(imgPath);
% Compute histogram
if strcmp(histType, 'grayscale')
grayImg = rgb2gray(img);
histVals = imhist(grayImg);
elseif strcmp(histType, 'rgb')
histVals = [];
for c = 1:3
histVals = [histVals; imhist(img(:,:,c))];
end
end
% Normalize histogram
histVals = histVals / sum(histVals);
% Store in structure
histDB(i).filename = imageFiles(i).name;
histDB(i).histogram = histVals;
end
% Save to .mat file
save('image_histograms.mat', 'histDB');
disp('Histograms saved to image_histograms.mat');
Later you can load the .mat file and compare the histogram based on any distance meteric you want.
I have attached the documentation of 'imhist' for your reference:
Also here is the documentation on 'struct' and 'matfile' in MATLAB and how to use them:
0 Comments
See Also
Categories
Find more on Histograms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!