Brain tumour detection using OpenCV and Machine learning in Python Programming. This model can be deployed on the cloud and detect tumour precisely.
For this project, we'll need some libraries listed below:
This project is divided into two parts:
1) Tumour segmentation:
we will create a class which is TumorExtractor(), which will help to extract tumour part from the image
class TumorExtractor():
we will initialize the class with a file name and read the image in greyscale format,
def __init__(self,img_file,width,height):
self.img = cv2.resize(cv2.imread(img_file,0),(width,height))
below function used to display image,
def verify_image(self, tum):
cv2.imshow('Windows1', tum)
try:
cv2.waitKey()
except KeyboardInterrupt as e:
print(e)
cv2.destroyWindow('Windows1')
before
extracting tumour from the image, we need to extract brain part from
the image. This function will extract the brain from the image and
remove the unwanted part of the image.
def extract_brain(self,ero_itr=5,dil_itr=5):
# remove all the noise from the image
#eg:- salt and pepper
kernel1 = np.ones((5,5),np.float32)/25
dst1 = cv2.filter2D(self.img,-1,kernel1)
self.verify_image(dst1)
#threshold the image to genrate filter
ret2,th2 = cv2.threshold(dst1,45,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
self.verify_image(th2)
#erosion of image to remove irregular shape and noise
kernel3 = np.ones((5,5), np.uint8)
img_erosion = cv2.erode(th2, kernel3, iterations=ero_itr)
self.verify_image(img_erosion)
#dilation of image to maximize the region of interest
img_dilation = cv2.dilate(img_erosion, kernel3, iterations=dil_itr)
self.verify_image(img_dilation)
# we will draw the contour around the largest irregular shape to extract brain part from image
contours,hierarchy = cv2.findContours(img_dilation, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
if len(contours) != 0:
c = max(contours, key = cv2.contourArea)
x,y,w,h = cv2.boundingRect(c)
new_img = self.img[y:y+h,x:x+w]
return new_img
# we will return extracted brian part from the image
Now we will extract tumour from brain image:
def extract_tumor(self):
#extract brain from the image by calling the function extract_brain()
new_img = self.extract_brain()
self.verify_image(new_img)
#apply the medium filter to smooth image and remove the noise
median_filter = cv2.medianBlur(new_img, 5)
#apply erosion and dilation
kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(9,9))
erosion = cv2.morphologyEx(median_filter, cv2.MORPH_ERODE, kernel)
dilation = cv2.morphologyEx(erosion, cv2.MORPH_DILATE, kernel)
#applying thresholding on the image to extract the tumor part from the image
ret3,th3 = cv2.threshold(dilation,160,255,cv2.THRESH_BINARY)
self.verify_image(th3)
#apply contour filter and extract the largest contour part
contours,hierarchy = cv2.findContours(th3, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
if len(contours) != 0:
c = max(contours, key = cv2.contourArea)
x,y,w,h = cv2.boundingRect(c)
tumor = new_img[y:y+h,x:x+w]
self.verify_image(tumor)
#finally applying above technique, we were able to segment and detect the tumor part inthe image
new_img = cv2.rectangle(new_img, (x, y), (x + w, y + h), (255,255,255), 1)
cv2.putText(new_img, 'Tumor Detected', (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255,255,255), 2)
self.verify_image(new_img)
2) Classification of Image:
In this part, we will create a machine learning model to classify the image into two categories:
Comments