<jupyter_start><jupyter_text>TumorInsight : A Brain Tumor Detection and Classification Model     <NAME> - 18BCE2113     <NAME> - 18BCE2513     <NAME> - 18BCE2517   Importing the required libraries<jupyter_code># cv2 is the Python Version of OpenCV
import cv2
# matplotlib is used for displaying images
import matplotlib.pyplot as plt
%matplotlib inline 
# numpy is used for matrix manipulations 
import numpy as np<jupyter_output><empty_output><jupyter_text>Read color image<jupyter_code># Read the color image
orig_img = cv2.imread("../CNN/test_images/test10.jpg",1) # 1 indicates color image
# OpenCV uses BGR while Matplotlib uses RGB format
# Display the color image with matplotlib
plt.imshow(cv2.cvtColor(orig_img, cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.show()<jupyter_output><empty_output><jupyter_text>Convert to greyscale<jupyter_code>gray_img = cv2.cvtColor( orig_img, cv2.COLOR_BGR2GRAY ) 
plt.imshow(gray_img,cmap='gray')
plt.axis('off')
plt.show()<jupyter_output><empty_output><jupyter_text>Median Filtering<jupyter_code># To remove salt and pepper noise
# Using 5*5 kernel
median_filtered = cv2.medianBlur(gray_img, 5)
plt.imshow(median_filtered,cmap='gray')
plt.axis('off')
plt.show()<jupyter_output><empty_output><jupyter_text>Edge Detection<jupyter_code># 3*3 Sobel Filters
Gx= np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])
print("Gx \n", Gx)
Gy = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]])
print("Gy \n", Gy)
img_sobelx = cv2.Sobel(median_filtered,cv2.CV_8U,1,0,ksize=3)
img_sobely = cv2.Sobel(median_filtered,cv2.CV_8U,0,1,ksize=3)
#del f = Gx + Gy
# Adding mask to the image
img_sobel = img_sobelx + img_sobely+gray_img
plt.imshow(img_sobel,cmap='gray')
plt.axis('off')
plt.show()<jupyter_output>Gx 
 [[-1  0  1]
 [-2  0  2]
 [-1  0  1]]
Gy 
 [[ 1  2  1]
 [ 0  0  0]
 [-1 -2 -1]]<jupyter_text>Thresholding<jupyter_code># Set threshold and maxValue
threshold = 50
maxValue = 255
 
# Threshold the pixel values
th, thresh = cv2.threshold(img_sobel, threshold, maxValue, cv2.THRESH_BINARY)
plt.imshow(thresh,cmap='gray')
plt.axis('off')
plt.show()<jupyter_output><empty_output><jupyter_text>Determining foreground and background<jupyter_code># To remove any small white noises in the image using morphological opening. 
kernel = np.ones((3,3),np.uint8)
opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2)

plt.imshow(opening,cmap='gray')
plt.axis('off')
plt.show()
# Black region shows sure background area
# Dilation increases object boundary to background.
sure_bg = cv2.dilate(opening,kernel,iterations=3)

plt.imshow(sure_bg,cmap='gray')
plt.axis('off')
plt.show()
#  White region shows sure foreground area
dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5)
ret, sure_fg = cv2.threshold(dist_transform,0.7*dist_transform.max(),255,0)

plt.imshow(sure_fg,cmap='gray')
plt.axis('off')
plt.show()
# Identifying regions where we don't know whether foreground and background
# Watershed algorithm
sure_fg = np.uint8(sure_fg)
unknown = cv2.subtract(sure_bg,sure_fg)

plt.imshow(unknown,cmap='gray')
plt.axis('off')
plt.show()<jupyter_output><empty_output><jupyter_text>Watershed Segmentation<jupyter_code>contours, hierarchy = cv2.findContours(sure_fg,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

# Creating a numpy array for markers and converting the image to 32 bit using dtype paramter
marker = np.zeros((gray_img.shape[0], gray_img.shape[1]),dtype = np.int32)

marker = np.int32(sure_fg) + np.int32(sure_bg)

# Marker Labelling
for id in range(len(contours)):
    cv2.drawContours(marker,contours,id,id+2, -1)

marker = marker + 1

marker[unknown==255] = 0

copy_img = orig_img.copy()

cv2.watershed(copy_img, marker)

imgplt = plt.imshow(marker)
plt.colorbar()
plt.show()

copy_img[marker==-1]=(0,0,255)
cv2.imwrite('img.jpg',copy_img)
plt.imshow(copy_img,cmap='gray')
plt.axis('off')
plt.show()<jupyter_output><empty_output><jupyter_text>Morphological Operations  Opening is achieved by erosion followed by dilation<jupyter_code># The basic purpose of the operation is to show only that part of the image having 
# more intensity which has the tumor that is the part of the image forming our desired extraction.
kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(7,7))
erosion = cv2.morphologyEx(median_filtered, cv2.MORPH_ERODE, kernel)
plt.imshow(erosion,cmap='gray')
plt.axis('off')
plt.show()
dilation = cv2.morphologyEx(erosion, cv2.MORPH_DILATE, kernel)
plt.imshow(dilation,cmap='gray')
plt.axis('off')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13346657783699756, 'fraction_numerical': 0.033311125916055964, 'mean_word_length': 6.4693200663349915, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '265414', 'license': 'MIT', 'n_tokens_mistral': 1871, 'n_tokens_neox': 1729, 'n_words': 375, 'path': 'src/ImageProc/TumorInsight - Brain Tumor Detection Model.ipynb', 'repo_name': 'sakshiigupta/TumorInsight', 'size': '4528'}