# Histogram equalization without histeq (color)

5 views (last 30 days)
Tito on 18 Nov 2021
Edited: DGM on 18 Nov 2021
Hi guys, I need to do an histogram equalization without using the command "histeq" but I need that in color. All the helps that i've found are in gray scale, I need to get an image equalizated that looks like the one that histeq throws or at least very similar.
I hope you can help me because i've been trying to do it and I always end up with an image with a lot of intensity to the point that it looks way more worst than at the beginning.
Thank you!
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Answers (1)

DGM on 18 Nov 2021
Edited: DGM on 18 Nov 2021
It all depends on why you can't use histeq(). If you can't use histeq() because you don't have IPT, then MIMT has a drop-in replacement that would work:
nbins = 64;
A = imread('peppers.png');
B = zeros(size(A),class(A));
for c = 1:size(A,3)
% this uses the default 64-bin flat histogram
% you can explicitly specify otherwise
B(:,:,c) = histeq(A(:,:,c),nbins);
end
subplot(2,2,1)
imshow(A)
subplot(2,2,2)
imshow(B)
colors = {'r','g','b'};
subplot(2,2,3)
hold on
for c = 1:size(A,3)
[n,x] = imhist(A(:,:,c),64); % same as MIMT imhistFB()
stem(x,n,colors{c},'marker','none')
end
subplot(2,2,4)
hold on
for c = 1:size(A,3)
[n,x] = imhist(B(:,:,c),64);
stem(x,n,colors{c},'marker','none')
end
If you're avoiding histeq() because this is homework and you're supposed to write it from scratch, then just open histeq() and look at the code and documentation to understand how it works.
Nothing about histogram equalization should imply that the result "looks better". It usually looks awful; the goal is to enforce some desired intensity distribution on the image to facilitate further processing. Maybe you're expecting something more like CLAHE?
A = imread('peppers.png');
B = zeros(size(A),class(A));
for c = 1:size(A,3)
% same as MIMT adapthisteqFB()
B(:,:,c) = adapthisteq(A(:,:,c));
end
figure;
imshow(B)
You may also elect to do either of these tasks by converting the image to some other model (e.g. LAB) and then doing equalization on the brightness component of the converted image. That may yield more desirable results. If you don't have IPT and need conversion tools, there are alternatives.
nbins = 64;
mode = 'lab'; % pick something
A = imread('peppers.png');
switch mode
case 'hsv'
B = rgb2hsv(A); % not in IPT
%B(:,:,3) = histeq(B(:,:,3),nbins);
B(:,:,3) = adapthisteq(B(:,:,3));
B = im2uint8(hsv2rgb(B));
case 'lab'
B = rgb2lab(A); % MIMT has rgb2lch and others
%B(:,:,1) = histeq(B(:,:,1)/100,nbins)*100;
B(:,:,1) = adapthisteq(B(:,:,1)/100)*100;
B = im2uint8(lab2rgb(B));
end
figure
imshow(B)
##### 1 CommentShowHide None
Tito on 18 Nov 2021
Yes, I'm avoiding histeq() because this is homework and I suppos to write it from scratch and then compare it with the result of run it through histeq(). I tried with the documentation but it only talks about the grayscale case or that's what I understand.
And what I mean by "it looks worse" is that I'm producing this:
While histeq() is making this:

Sign in to comment.

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by