text_html_output_1.png text_plain_output_1.png
import cv2

import matplotlib.pyplot as plt

import numpy as np

covid_path = '/kaggle/input/covid19/COVID-19_Radiography_Dataset/COVID/images/'
normal_path = '/kaggle/input/covid19/COVID-19_Radiography_Dataset/Normal/images/'

def img_preprocessing(image_path):
    img = cv2.imread(image_path, 0)
    org_img = img.copy()
    brightest = np.max(img)
    darkest = np.min(img)
    T = darkest + 0.9 * (brightest - darkest)
    thre_img = cv2.threshold(img, T, 255, cv2.THRESH_BINARY)
    thre_img = thre_img[1]
    kernel = np.ones((5, 5), np.uint8)
    cleaned = cv2.erode(thre_img, kernel, iterations=5)
    cleaned = cv2.dilate(cleaned, kernel, iterations=5)
    cleaned = cleaned // 255
    img = img * cleaned
    img = org_img - img
    dim = (224, 224)
    img = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
    B = cv2.bilateralFilter(img, 9, 75, 75)
    R = cv2.equalizeHist(img)
    new_img = cv2.merge((B, img, R))
    return new_img

img = img_preprocessing(covid_path + 'COVID-1.png')
plt.imshow(img)