Implement a function with Matlab to output the histogram values of a given grey level image. Display in the boxes the resulting histograms.
时间: 2024-03-03 21:50:31 浏览: 116
Sure, here's an implementation of a function in Matlab that takes a grey level image as input and outputs its histogram:
```matlab
function [hist, edges] = grey_histogram(img)
% img: grey level image
% hist: histogram values
% edges: edges of histogram bins
% Compute histogram
[hist, edges] = histcounts(img, 256);
% Display histogram
figure;
bar(edges(1:end-1), hist);
xlabel('Grey Level');
ylabel('Frequency');
title('Grey Level Histogram');
end
```
To use this function, simply call it with your grey level image as input. For example:
```matlab
img = imread('my_grey_image.jpg');
[hist, edges] = grey_histogram(img);
```
This will display a histogram of the grey levels in your image.
相关问题
Implement a function with Matlab that equalizes grey-scale images based on their histogram. The input is a given grey level image; the output is the derived image with uniform intensity distribution.
Sure, here's an implementation of the function in MATLAB:
```matlab
function equalized_img = hist_equalization(img)
% Calculate histogram
[hist, bins] = imhist(img);
% Calculate cumulative distribution function (CDF)
cdf = cumsum(hist);
% Normalize CDF
cdf_normalized = cdf * max(hist) / max(cdf);
% Create lookup table
lut = interp1(bins, cdf_normalized, 0:255);
% Apply lookup table to image
equalized_img = uint8(lut(double(img)+1));
end
```
To use this function, simply pass in a grayscale image as the argument and it will return the equalized image. Here's an example of how to use it:
```matlab
% Load grayscale image
img = imread('image.jpg');
img_gray = rgb2gray(img);
% Apply histogram equalization
equalized_img = hist_equalization(img_gray);
% Display original and equalized images
subplot(1,2,1), imshow(img_gray), title('Original Image')
subplot(1,2,2), imshow(equalized_img), title('Equalized Image')
```
I hope this helps! Let me know if you have any questions.
阅读全文
相关推荐












