<jupyter_start><jupyter_text>This notebook implements the paper titled:*Shadow removal of text document images by estimating local and global background colors* We start by importing required packages and loading images. The loaded image is determined by `filename`. Some suggested values are also provided to work with other images from `./dataset/document`. The definitions of the functions we implemented and used below can be found [here](helper_functions.py) under appropriate section.<jupyter_code># Imports packages and prepares for execution (PreRUN)

import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
from skimage import color,filters,transform
from scipy import ndimage as ndi
from helper_functions import *
from tqdm import tqdm


base_img_rpath = '../dataset/document/'  #dataset-read path
base_img_wpath = '../output/document/'   #output-write path
filename = 'textbook.jpg'  #choices = 'document.png', 'color_mosaic.jpg', 'textbook.jpg', 'manuscript.jpg'
ip_img = load_img(base_img_rpath + filename)


is_0_255 = True   #modifies algo if pixel values are 0-255 (False > color_mosaic), arg passed to functions.
f, ax = plot_img(ip_img, "Original Image")<jupyter_output><empty_output><jupyter_text>The global background color i.e $I_c^{global}(\mathsf{x})$ is found using three possible ways. The parameter `choice` determines which method is called.<jupyter_code># Extracting global background colour

choice = 1

if choice==1:
    # Global average over each channel  (Approach #1)  
    I_global = get_global_colour_1(ip_img,is_0_255)
elif choice==2:
    # Max pixel value for each channel  (Approach #2)
    I_global = get_global_colour_2(ip_img,is_0_255)
elif choice==3:
    # Average of top 50 pixels          (Approach #3)
    I_global = get_global_colour_3(ip_img,is_0_255)
f,ax = show_img_compare(ip_img, I_global, 'Original Image', 'Estimated Global Background')<jupyter_output><empty_output><jupyter_text>The local background i.e $I_c^{local}(\mathsf{x})$ is estimated using specific values of percentile parameter(`p`) and `block_size`. The cell below plots the preliminary estimation of local background.<jupyter_code># Estimation of Local Background
p = 0.9
block_size = 7
I_local = get_local_bg(ip_img,p, block_size, is_0_255)
f,ax = plot_img(I_local, "Local Background")<jupyter_output><empty_output><jupyter_text>The local background is refined using a median block technique as outlined in [1]. We call this result `I_local_refined`.<jupyter_code># Refined Estimation of Local Background
threshold = 0.01
median_block_size = 17
I_local_refined = get_local_bg_refined(I_local, ip_img, threshold, median_block_size, is_0_255)
f,ax = plot_img(I_local_refined, "Refined Local Background")<jupyter_output><empty_output><jupyter_text>The resulting shadow-free or deshadowed image is obtained by dividing the input shadow image by the shadow map. The shadow map $S_c(\mathsf{x})$ is defined as [1]:$$S_c(\mathsf{x}) = \frac{I_c^{local}(\mathsf{x})}{I_c^{global}(\mathsf{x})}$$ The deshadowed image is calculated and plotted below.<jupyter_code># Deshadowed Image
generate_deshadow(ip_img, I_local, I_global, is_0_255)
I_deshadow = generate_deshadow(ip_img, I_local, I_global, is_0_255)
I_deshadow_refined = generate_deshadow(ip_img, I_local_refined, I_global, is_0_255)

# Plotting 
title1 = 'De-Shadowed Image'
title2 = 'Refined De-Shadowed Image'
f, a = show_img_compare(I_deshadow, I_deshadow_refined, title1, title2)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09356223175965665, 'fraction_numerical': 0.01888412017167382, 'mean_word_length': 6.253112033195021, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '454472', 'license': 'MIT', 'n_tokens_mistral': 1175, 'n_tokens_neox': 1123, 'n_words': 347, 'path': 'projects/Shadow-Removal/notebooks/RunMe_approach_1.ipynb', 'repo_name': 'bioshape-lab/ece278a', 'size': '3495'}