Log transform

The log transformation is very useful when we need to compress or stretch a certain range of gray-levels in an image; for example, in order to display the Fourier spectrum (where the DC component value is much higher than the others, so that without the log transform the other frequency components almost always cannot even be seen). The point transformation function for a log transform is of the general form,, where c is a constant.

Let's implement the histogram for the color channels of the input image:

def plot_image(image, title=''):
pylab.title(title, size=20), pylab.imshow(image)
pylab.axis('off') # comment this line if you want axis ticks

def plot_hist(r, g, b, title=''):
r, g, b = img_as_ubyte(r), img_as_ubyte(g), img_as_ubyte(b)
pylab.hist(np.array(r).ravel(), bins=256, range=(0, 256), color='r', alpha=0.5)
pylab.hist(np.array(g).ravel(), bins=256, range=(0, 256), color='g', alpha=0.5)
pylab.hist(np.array(b).ravel(), bins=256, range=(0, 256), color='b', alpha=0.5)
pylab.xlabel('pixel value', size=20), pylab.ylabel('frequency', size=20)
pylab.title(title, size=20)

im = Image.open("../images/parrot.png")
im_r, im_g, im_b = im.split()
pylab.style.use('ggplot')
pylab.figure(figsize=(15,5))
pylab.subplot(121), plot_image(im, 'original image')
pylab.subplot(122), plot_hist(im_r, im_g, im_b,'histogram for RGB channels')
pylab.show()

The following screenshot shows the output—the histograms for the color channels for the original image before applying the log transformation:

Let's now apply a log transform using the PIL image module's point()function and impact on the transformation on the histograms of different color channels for an RGB image:

im = im.point(lambda i: 255*np.log(1+i/255))
im_r, im_g, im_b = im.split()
pylab.style.use('ggplot')
pylab.figure(figsize=(15,5))
pylab.subplot(121), plot_image(im, 'image after log transform')
pylab.subplot(122), plot_hist(im_r, im_g, im_b, 'histogram of RGB channels log transform')
pylab.show()

The output shows how the histograms are squeezed for different color channels:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.179.59