image histogram without imhist in matlab

To create an image histogram without using the imhist function in MATLAB, you can use the following steps:

  1. Read the image using the imread function and convert it to grayscale if it is a color image using the rgb2gray function.
  2. Create a histogram by counting the number of occurrences of each intensity level in the image. To do this, you can initialize an array of zeros with a size of 256 (since there are 256 possible intensity levels in an 8-bit image). Then, iterate over each pixel in the image and increment the corresponding element in the array by 1.
  3. Optionally, you can normalize the histogram values by dividing each element in the histogram array by the total number of pixels in the image. This will give you the probability distribution of intensity levels in the image.
  4. Finally, plot the histogram using the bar function to visualize the distribution of intensity levels.

Here is an example implementation:

main.m
% Read the image
image = imread('your_image.jpg');

% Convert to grayscale if necessary
if size(image, 3) == 3
    image = rgb2gray(image);
end

% Create histogram
histogram = zeros(1, 256);
totalPixels = numel(image);

for intensity = 0:255
    histogram(intensity + 1) = sum(sum(image == intensity));
end

% Normalize histogram
normalizedHistogram = histogram / totalPixels;

% Plot histogram
figure;
bar(0:255, normalizedHistogram);
title('Image Histogram');
xlabel('Intensity Level');
ylabel('Probability');
512 chars
26 lines

This code will compute and plot the histogram of the input image without using the imhist function.

related categories

gistlibby LogSnag