result of implementing histogram not exactly like imhist function why?

4 ビュー (過去 30 日間)
ira shree
ira shree 2022 年 8 月 29 日
編集済み: Vidhi Agarwal 2024 年 11 月 26 日
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');

回答 (1 件)

Vidhi Agarwal
Vidhi Agarwal 2024 年 11 月 26 日
編集済み: Vidhi Agarwal 2024 年 11 月 26 日
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!

カテゴリ

Help Center および File ExchangeHistograms についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by