how to store histograms

조회 수: 6 (최근 30일)
sakshi Garg
sakshi Garg 2015년 4월 16일
답변: Anudeep Kumar 2025년 6월 26일
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

답변 (1개)

Anudeep Kumar
Anudeep Kumar 2025년 6월 26일
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:

카테고리

Help CenterFile Exchange에서 Histograms에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by