<jupyter_start><jupyter_text>Image Filtering using Custom KernelsThis programs applies any filter over a given image. Importing Libraries<jupyter_code>import cv2 as cv
import numpy as np
# Dependencies for displaying image in notebook
from PIL import Image
from IPython.display import display<jupyter_output><empty_output><jupyter_text>Method to load the image<jupyter_code>def loadImg(imageName):
    src = cv.imread(imageName, cv.IMREAD_COLOR)
    # Check if image is loaded 
    if src is None:
        print ('Error opening image!')
        return None
    view(src)
    return src<jupyter_output><empty_output><jupyter_text>Method to display the image<jupyter_code>def view(img):
    img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
    display(Image.fromarray(img))
    
# For showing using opencv library    
#     window_name = 'custom filter'
#     cv.imshow(window_name, img)
#     cv.waitKey(0)
#     cv.destroyAllWindows()<jupyter_output><empty_output><jupyter_text>Method to Apply FilterThis function takes a kernel as an input and applies image fitering using cv::filter2D funtion. If not kernel is passes as an agrument then it will apply an identity kernel making no changes.<jupyter_code>def filters(img,kernel = np.array([[0,0,0],[0,1,0],[0,0,0]])):
    
    # Defining the ddepth of the filtered image
    ddepth = -5
    
    # Checking if the image is valid 
    if img is None:
        print ('Image not Found')
        return 'Image not founds'
    
    # Applying the filter 
    dst = cv.filter2D(img, ddepth, kernel)
    
    #Calling the view function
    view(dst)
    return<jupyter_output><empty_output><jupyter_text>Loading the Image<jupyter_code>imageName = 'Om_OpenCv/lena.jpg'
img = loadImg(imageName)<jupyter_output><empty_output><jupyter_text>Defining kernel and Calling filter method Here we choose the sharpen kernel, any other kernel can be applied.<jupyter_code>kernel = np.array([[0,-1,0],[-1,5,-1],[0,-1,0]])
filters(img,kernel)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09487951807228916, 'fraction_numerical': 0.012048192771084338, 'mean_word_length': 4.386486486486486, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '624799', 'license': 'MIT', 'n_tokens_mistral': 665, 'n_tokens_neox': 652, 'n_words': 184, 'path': '.ipynb_checkpoints/Om_OpenCv_ImageFiltering-checkpoint.ipynb', 'repo_name': 'Shreyansh-Gupta/Open-contributions', 'size': '1992'}