简体   繁体   中英

Histogram equalization help in MATLAB

My code is shown below:

G= histeq(imread('F:\Thesis\images\image1.tif'));
figure,imshow(G);

The error message I got was the following and I'm not sure why it is appearing:

Error using histeq
Expected input number 1, I, to be two-dimensional.

Error in histeq (line 68)
validateattributes(a,{'uint8','uint16','double','int16','single'}, ...

Error in testFile1 (line 8)
G= histeq(imread('F:\Thesis\images\image1.tif'));

Your image is most likely colour. histeq only works on grayscale images. There are three options available to you depending on what you want to do. You can either convert the image to grayscale, you can histogram equalize each channel individually or what is perceptually better is to convert the image into the HSV colour space, histogram equalize the V or Value component, then convert back into RGB. I tend to prefer the last option for colour images. Therefore, one method will be an enhanced grayscale image, and the other two will be an enhanced colour image.

Option #1 - Convert to grayscale then equalize

G = imread('F:\Thesis\images\image1.tif');
G = histeq(rgb2gray(G));
figure; imshow(G);

Use rgb2gray to convert the image to grayscale, then equalize the image.

Option #2 - Equalize each channel individually

G = imread('F:\Thesis\images\image1.tif');
for i = 1 : size(G, 3)
    G(:,:,i) = histeq(G(:,:,i));
end
figure; imshow(G);

Loop through each channel and equalize.

Option #3 - Convert to HSV, histogram equalize the V channel then convert back

G = imread('F:\Thesis\images\images1.tif');
Gh = rgb2hsv(G);
Gh(:,:,3) = histeq(Gh(:,:,3));
G = im2uint8(hsv2rgb(Gh));
figure; imshow(G);

Use the rgb2hsv function to convert a colour image into HSV. We then use histogram equalization on the V or Value channel, then convert back from HSV to RGB with hsv2rgb . Note that the output of hsv2rgb will be a double type image and so assuming that the original input image was uint8 , use the im2uint8 function to convert from double back to uint8 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM