Created
August 24, 2013 19:39
-
-
Save lizecillie/6330029 to your computer and use it in GitHub Desktop.
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
from __future__ import division | |
import matplotlib.pyplot as plt | |
import numpy as np | |
from skimage import io, color, img_as_float | |
def histogram_equalization(A): | |
rows = A.shape[0] | |
columns = A.shape[1] | |
histogram, bin_edges = np.histogram(A.flatten(), range=(0, 1), bins=256) | |
histogram = histogram/np.max(histogram) | |
c = histogram.cumsum() | |
c = c/np.max(c) | |
B = 255*A.copy() | |
for i in xrange(rows): | |
for j in xrange(columns): | |
B[i, j] = c[B[i, j]] | |
return B | |
def histogram_equalization_colour(A): | |
B = A.copy() | |
B[:, :, 0] = histogram_equalization(B[:, :, 0]) | |
B[:, :, 1] = histogram_equalization(B[:, :, 1]) | |
B[:, :, 2] = histogram_equalization(B[:, :, 2]) | |
return B | |
def histogram_equalization_point_transformation(A): | |
B = A.copy() | |
P1 = B[:, :, 0] | |
P2 = B[:, :, 1] | |
P3 = B[:, :, 2] | |
G = (np.sum(B * (1/3, 1/3, 1/3), axis=2))/255 | |
rows = G.shape[0] | |
columns = G.shape[1] | |
histogram, bin_edges = np.histogram(G.flatten(), range=(0, 1), bins=256) | |
histogram = histogram/histogram.max() | |
c = histogram.cumsum() | |
c = c/c.max() | |
P1 = 255*P1 | |
P2 = 255*P2 | |
P3 = 255*P3 | |
for i in xrange(rows): | |
for j in xrange(columns): | |
P1[i, j] = c[P1[i, j]] | |
P2[i, j] = c[P2[i, j]] | |
P3[i, j] = c[P3[i, j]] | |
return B | |
def rgb_hsi_conversion_equalize(A): | |
B = A.copy() | |
B = color.rgb2hsv(B) | |
B[:, :, 0] = histogram_equalization(B[:, :, 0]) | |
B[:, :, 1] = histogram_equalization(B[:, :, 1]) | |
B[:, :, 2] = histogram_equalization(B[:, :, 2]) | |
B = color.hsv2rgb(B) | |
return B | |
def rgb_hsi_conversion_equalizeI(A): | |
B = A.copy() | |
B = color.rgb2hsv(B) | |
B[:, :, 2] = histogram_equalization(B[:, :, 2]) | |
B = color.hsv2rgb(B) | |
return B | |
A = img_as_float(io.imread('spaghetti.jpg', plugin='pil')) | |
out = histogram_equalization_colour(A) | |
out1 = histogram_equalization_point_transformation(A) | |
out2 = rgb_hsi_conversion_equalize(A) | |
out3 = rgb_hsi_conversion_equalizeI(A) | |
f0, (ax0, ax1) = plt.subplots(1, 2) | |
f0.tight_layout() | |
ax0.imshow(A) | |
ax0.set_title('Input image') | |
ax1.imshow(out) | |
ax1.set_title('Histogram equalization on each colour panel seperately') | |
f1, (ax0, ax1) = plt.subplots(1, 2) | |
f1.tight_layout() | |
ax0.imshow(A) | |
ax0.set_title('Input image') | |
ax1.imshow(out1) | |
ax1.set_title('Histogram equalization point transformation applied to R, G and B panels') | |
f2, (ax0, ax1) = plt.subplots(1, 2) | |
f2.tight_layout() | |
ax0.imshow(A) | |
ax0.set_title('Input image') | |
ax1.imshow(out2) | |
ax1.set_title('Histogram equalization on the H, S and I panels') | |
f3, (ax0, ax1) = plt.subplots(1, 2) | |
f3.tight_layout() | |
ax0.imshow(A) | |
ax0.set_title('Input image') | |
ax1.imshow(out3) | |
ax1.set_title('Histogram equalization on the I-panel') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment