text_plain_output_1.png
from matplotlib import animation, rc

import cv2

import matplotlib.image as mpimg

import matplotlib.pyplot as plt

import os

img = cv2.imread('/kaggle/input/brain-tumor-mri-images-44c/Carcinoma T2/112._big_gallery.jpeg')
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
threshold_value = 70
max_value = 255
threshold_type = cv2.THRESH_BINARY
_, binary_img = cv2.threshold(gray_img, threshold_value, max_value, threshold_type)

img1 = img
img2 = gray_img
img3 = binary_img

fig, axs = plt.subplots(1,3,figsize=(12,4))
fig.suptitle('Binarization of Image')

axs[0].imshow(cv2.cvtColor(img1, cv2.COLOR_BGR2RGB))
axs[1].imshow(img2, cmap='gray')
axs[2].imshow(img3)

for ax in axs:
    ax.set_xticks([])
    ax.set_yticks([])

plt.show()

for i in range(0, 255, 5):
    gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    threshold_value = i
    max_value = 255
    threshold_type = cv2.THRESH_BINARY
    _, binary_img = cv2.threshold(gray_img, threshold_value, max_value, threshold_type)
    mpimg.imsave(str(i).zfill(3) + '.png', binary_img)

from matplotlib import animation, rc
rc('animation', html='jshtml')

def create_animation(ims):
    fig=plt.figure(figsize=(4,4))
    #plt.axis('off')
    im=plt.imshow(cv2.cvtColor(ims[5],cv2.COLOR_BGR2RGB))
    plt.close()
    
    def animate_func(i):
        im.set_array(cv2.cvtColor(ims[i],cv2.COLOR_BGR2RGB))
        return [im]

    return animation.FuncAnimation(fig, animate_func, frames=len(ims), interval=1000//3)

paths = []
for dirname, _, filenames in os.walk('/kaggle/working/'):
    for filename in filenames:
        if filename[-4:] == '.png':
            paths += [os.path.join(dirname, filename)]
paths = sorted(paths)

images = []
for i in range(len(paths)):
    images += [cv2.imread(paths[i])]

create_animation(images)