""" Median Blur (OpenCV) + Histogram Equalization (scikit-image) on coloured images Syntax: py *.py Input.png where *.py is the name of this file -------------------------------------------------------------------------------- There are many filters that are designed to work with gray-scale images but not with color images. To simplify the process of creating functions that can adapt to RGB images, scikit-image provides the adapt_rgb decorator. NOTE: A decorator in Python is any callable Python object that is used to modify a function or a class. ---Reference: Sreenivas Bhattiprolu--- -------------------------------------------------------------------------------- """ import cv2, sys, os from skimage.color.adapt_rgb import adapt_rgb, each_channel, hsv_value from skimage import filters from skimage import io import matplotlib.pyplot as plt from skimage.color import rgb2gray file_name = str(sys.argv[1]) im = os.getcwd() + "\\" + file_name img = io.imread(im) #Two ways to apply the filter on color images #------------------------------------------------------------------------------- #1)Separate R, G, B channels - apply filters - put channel back together #2)Convert RGB to HSV - apply filter to V channel - convert RSV to RGB #Too many lines of code needed for this, adapt_rgb decorator makes it easy @adapt_rgb(each_channel) def sobel_each(img): return filters.sobel(img) @adapt_rgb(hsv_value) def sobel_hsv(img): return filters.sobel(img) each_channel_image = sobel_each(img) hsv_value_image = sobel_hsv(img) #Convert to grey if needed #sobel_grey = rgb2gray(hsv_value_image) #plt.imshow(sobel_grey) #Decorators work on any function, including the ones using OpenCV filters @adapt_rgb(each_channel) def median_each(img, k): output_image = cv2.medianBlur(img, k) return (output_image) median_using_cv2 = median_each(img, 3) #plt.imshow(median_using_cv2) #Histogram equalization on RGB channels may not yield desired results. Applying #it on V channel in HSV provides the best results #------------------------------------------------------------------------------- from skimage import exposure @adapt_rgb(each_channel) def eq_each(img): output_image = exposure.equalize_hist(img) return (output_image) equ_RGB = eq_each(img) #plt.imshow(equ_RGB) @adapt_rgb(hsv_value) def eq_hsv(img): output_image = exposure.equalize_hist(img) return (output_image) equ_hsv = eq_hsv(img) #plt.imshow(equ_hsv) fig = plt.figure(figsize=(10, 10)) ax1 = fig.add_subplot(2,2,1); ax1.imshow(img) ax1.title.set_text('Input Image') ax2 = fig.add_subplot(2,2,2); ax2.imshow(equ_RGB) ax2.title.set_text('Equalized using RGB channels') ax3 = fig.add_subplot(2,2,3); ax3.imshow(equ_hsv) ax3.title.set_text('Equalized using V channel in HSV') plt.show()