├── README.md ├── adaptive-threshold.py ├── denoising.py └── histogram-equalization.py /README.md: -------------------------------------------------------------------------------- 1 | # ImgPreprocessing 2 | 3 | It is a project dedicated to overcome the limitations of digital cameras like geometrical distortions, focus loss or uneven document lightening, which can help us to extract text more accurately, classify the language and extract other meaningful information from the image. 4 | -------------------------------------------------------------------------------- /adaptive-threshold.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import matplotlib.image as mpimg 3 | from skimage import io 4 | from skimage import filter 5 | from skimage import img_as_uint 6 | from skimage import data 7 | 8 | 9 | image = io.imread('/home/mallikarjuna/Desktop/white.png') 10 | #image2 = data.page() 11 | #print (type(image2)) 12 | import numpy as np 13 | #print(np.shape(image2)) 14 | global_thresh = filter.threshold_otsu(image) 15 | binary_global = image > global_thresh 16 | io.imsave('/home/mallikarjuna/Desktop/global2.png',img_as_uint(binary_global)) 17 | 18 | block_size = 35 19 | binary_adaptive = filter.threshold_adaptive(image, block_size, offset=10) 20 | io.imsave('/home/mallikarjuna/Desktop/adaptive2.png',img_as_uint(binary_adaptive)) 21 | fig, axes = plt.subplots(nrows=3, figsize=(7, 8)) 22 | ax0, ax1, ax2 = axes 23 | plt.gray() 24 | 25 | ax0.imshow(image) 26 | ax0.set_title('Image') 27 | 28 | ax1.imshow(binary_global) 29 | ax1.set_title('Global thresholding') 30 | 31 | ax2.imshow(binary_adaptive) 32 | ax2.set_title('Adaptive thresholding') 33 | 34 | for ax in axes: 35 | ax.axis('off') 36 | 37 | plt.show() 38 | 39 | -------------------------------------------------------------------------------- /denoising.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | from skimage import io 4 | from skimage import img_as_uint 5 | 6 | from skimage import data, img_as_float 7 | from skimage.restoration import denoise_tv_chambolle, denoise_bilateral 8 | 9 | from skimage import io 10 | from skimage import color 11 | from skimage import img_as_uint,img_as_float 12 | 13 | 14 | astro = img_as_float(io.imread('/home/mallikarjuna/Desktop/new1/s.png')) 15 | 16 | noisy = astro + 0.6 * astro.std() * np.random.random(astro.shape) 17 | noisy = np.clip(noisy, 0, 1) 18 | 19 | fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(8, 5), sharex=True, 20 | sharey=True, subplot_kw={'adjustable': 'box-forced'}) 21 | 22 | plt.gray() 23 | 24 | ax.imshow(denoise_bilateral(noisy, sigma_range=0.05, sigma_spatial=15)) 25 | io.imsave('/home/mallikarjuna/Desktop/pic.png',img_as_uint(denoise_bilateral(noisy, sigma_range=0.05, sigma_spatial=15))) 26 | ax.axis('off') 27 | ax.set_title('Bilateral') 28 | 29 | image = color.rgb2gray(io.imread('/home/mallikarjuna/Desktop/pic.png')) 30 | io.imsave('/home/mallikarjuna/Desktop/black.png',img_as_uint(image)) 31 | 32 | fig.tight_layout() 33 | 34 | plt.show() 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /histogram-equalization.py: -------------------------------------------------------------------------------- 1 | import matplotlib 2 | import matplotlib.pyplot as plt 3 | import numpy as np 4 | 5 | from skimage import data, img_as_float 6 | from skimage import exposure 7 | from skimage import io 8 | from skimage import filter 9 | from skimage import img_as_uint 10 | 11 | 12 | matplotlib.rcParams['font.size'] = 8 13 | 14 | 15 | def plot_img_and_hist(img, axes, bins=256): 16 | """Plot an image along with its histogram and cumulative histogram. 17 | 18 | """ 19 | img = img_as_float(img) 20 | ax_img, ax_hist = axes 21 | ax_cdf = ax_hist.twinx() 22 | 23 | # Display image 24 | ax_img.imshow(img, cmap=plt.cm.gray) 25 | ax_img.set_axis_off() 26 | ax_img.set_adjustable('box-forced') 27 | 28 | # Display histogram 29 | ax_hist.hist(img.ravel(), bins=bins, histtype='step', color='black') 30 | ax_hist.ticklabel_format(axis='y', style='scientific', scilimits=(0, 0)) 31 | ax_hist.set_xlabel('Pixel intensity') 32 | ax_hist.set_xlim(0, 1) 33 | ax_hist.set_yticks([]) 34 | 35 | # Display cumulative distribution 36 | img_cdf, bins = exposure.cumulative_distribution(img, bins) 37 | ax_cdf.plot(bins, img_cdf, 'r') 38 | ax_cdf.set_yticks([]) 39 | 40 | return ax_img, ax_hist, ax_cdf 41 | 42 | 43 | # Load an example image 44 | img = io.imread('/home/mallikarjuna/Desktop/black.png') 45 | 46 | # Contrast stretching 47 | p2, p98 = np.percentile(img, (2, 98)) 48 | img_rescale = exposure.rescale_intensity(img, in_range=(p2, p98)) 49 | 50 | # Equalization 51 | img_eq = exposure.equalize_hist(img) 52 | 53 | # Adaptive Equalization 54 | img_adapteq = exposure.equalize_adapthist(img, clip_limit=0.03) 55 | 56 | # Display results 57 | fig = plt.figure(figsize=(8, 5)) 58 | axes = np.zeros((2, 4), dtype=np.object) 59 | axes[0, 0] = fig.add_subplot(2, 4, 1) 60 | for i in range(1, 4): 61 | axes[0, i] = fig.add_subplot(2, 4, 1+i, sharex=axes[0,0], sharey=axes[0,0]) 62 | for i in range(0, 4): 63 | axes[1, i] = fig.add_subplot(2, 4, 5+i) 64 | 65 | ax_img, ax_hist, ax_cdf = plot_img_and_hist(img, axes[:, 0]) 66 | ax_img.set_title('Low contrast image') 67 | 68 | y_min, y_max = ax_hist.get_ylim() 69 | ax_hist.set_ylabel('Number of pixels') 70 | ax_hist.set_yticks(np.linspace(0, y_max, 5)) 71 | 72 | ax_img, ax_hist, ax_cdf = plot_img_and_hist(img_rescale, axes[:, 1]) 73 | ax_img.set_title('Contrast stretching') 74 | io.imsave('/home/mallikarjuna/Desktop/white.png',img_as_uint(img_rescale)) 75 | 76 | ax_img, ax_hist, ax_cdf = plot_img_and_hist(img_eq, axes[:, 2]) 77 | ax_img.set_title('Histogram equalization') 78 | 79 | ax_img, ax_hist, ax_cdf = plot_img_and_hist(img_adapteq, axes[:, 3]) 80 | ax_img.set_title('Adaptive equalization') 81 | 82 | ax_cdf.set_ylabel('Fraction of total intensity') 83 | ax_cdf.set_yticks(np.linspace(0, 1, 5)) 84 | 85 | # prevent overlap of y-axis labels 86 | fig.tight_layout() 87 | plt.show() 88 | --------------------------------------------------------------------------------