Created
March 13, 2019 17:21
-
-
Save inhzus/c86da05e91c5abd9c5835b9c35d9109e to your computer and use it in GitHub Desktop.
Histogram equalization, RGB and HSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function [output] = Histogram_equalization(input_image) | |
%first test the image is a RGB or gray image | |
hsv = true; | |
if numel(size(input_image)) == 3 | |
%this is a RGB image | |
%here is just one method, if you have other ways to do the | |
%equalization, you can change the following code | |
if hsv | |
input_image = rgb2hsv(input_image); | |
h=input_image(:,:,1); | |
s=input_image(:,:,2); | |
v=input_image(:,:,3); | |
s = zoom_hist(s); | |
v = zoom_hist(v); | |
output = cat(3, h, s, v); | |
output = hsv2rgb(output); | |
else | |
r=input_image(:,:,1); | |
g=input_image(:,:,2); | |
b=input_image(:,:,3); | |
r = hist_equal(r); | |
g = hist_equal(g); | |
b = hist_equal(b); | |
output = cat(3,r,g,b); | |
end | |
else | |
%this is a gray image | |
[output] = hist_equal(input_image); | |
end | |
function ret = zoom_hist(a) | |
ret = double(hist_equal(uint8(256 * a))) / 256; | |
end | |
function [ret] = hist_equal(input_channel) | |
% hist of input_channel | |
input_channel = input_channel + 1; | |
[height, width] = size(input_channel); | |
src = get_hist(input_channel); | |
% figure, plot(1:256, src); | |
% above: collect and get hist | |
% following steps generate the function to realize histeq | |
% principle: f(x) = 256 / numel * (\sum_0^x{src_x}) | |
conversion = zeros(256, 1); | |
cur = 0; | |
pixels = numel(input_channel); | |
for i = 1:256 | |
cur = cur + src(i); | |
conversion(i) = 256 * cur / pixels; | |
end | |
% above: generate function(0:255) -> pixel value | |
ret = zeros(height, width, 'uint8'); | |
for i = 1:height | |
for j = 1:width | |
ret(i, j) = conversion(input_channel(i, j)); | |
end | |
end | |
% figure, plot(1:256, get_hist(ret + 1)); | |
%you should complete this sub-function | |
end | |
function ret = get_hist(channel) | |
ret = zeros(256, 1); | |
[height, width] = size(channel); | |
for m = 1:height | |
for n = 1:width | |
ret(channel(m, n)) = ret(channel(m, n)) + 1; | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment