result of implementing histogram not exactly like imhist function why?

4 vues (au cours des 30 derniers jours)
ira shree
ira shree le 29 Août 2022
Modifié(e) : Vidhi Agarwal le 26 Nov 2024 à 3:23
im = imread('CameraMan.jpg');
A = rgb2gray(im);
S = size(A);
[D1, ia, ic] = unique(A);
[R1,C1]=size(D1);
E1=[];
for i = 1 : R1
F1 = sum(A(:)==D1(i));
E1(i)=F1;
end
[counts,blocklocation]=imhist(A);
counts= counts';
figure;
subplot(1,2,1);
bar(E1);title('bar');
subplot(1,2,2);
imhist(A);title('hist');

Réponses (1)

Vidhi Agarwal
Vidhi Agarwal le 26 Nov 2024 à 3:20
Modifié(e) : Vidhi Agarwal le 26 Nov 2024 à 3:23
I understand you are facing discrepancy between implementation of a histogram and MATLAB's "imhist" function. This might be due to several factors. Below are some potential reasons:
  • If your image is of type "uint8", it should have pixel values in the range [0, 255]. Ensure that your histogram accounts for all possible values, even if they don't appear in the image.
  • "imhist" uses fixed bin widths for each possible intensity value. If current implementation is not using the same bin width, the results will differ.
  • Ensure that the image is correctly converted to grayscale and that no preprocessing steps are missing.
  • Check for edge cases where certain intensity values might not be present in the image, ensuring that the histogram still accounts for all possible values.
Revised version of code is given below:
im = imread('CameraMan.jpeg');
A = rgb2gray(im);
% Initialize the histogram array for 256 bins
E1 = zeros(1, 256);
% Calculate histogram manually
for i = 0:255
E1(i+1) = sum(A(:) == i);
end
% Compute using imhist
[counts, ~] = imhist(A);
counts = counts';
% Plot both histograms
figure;
subplot(1, 2, 1);
bar(0:255, E1); % Ensure the x-axis matches the intensity range
title('Manual Histogram');
subplot(1, 2, 2);
imhist(A);
title('imhist Function');
For better understanding of "imhist" refer to the following documentation:
Hope that helps!

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by