how to store histograms
6 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (1 件)
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:
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Histograms についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!