Академический Документы
Профессиональный Документы
Культура Документы
Tumours are group of abnormal cells that form bumps growth specifically we
discuss about brain tumours. Early diagnosis of brain tumour plays an important
role in improving treatment and increases rate of survival. In this project, we use
MRI images to identify the tumour. Methods have already been developed to
identify a tumour using MRI images to reduce the time in an automated process.
In this project, we are going to use Machine learning techniques to identify the
tumours as they give accurate results than any other existing methods. Using MRI
and Machine learning methods we classify and identify tumour parts.
5
Table of Contents
1. INTRODUCTION 8-12
1.2.3.1 Python 10
2.3.1 X-rays 14
6
2.3.6.2 T2 weighted image 26
2.3.6.4 PD images 27
3.3.1.1 Classification 32
3.3.1.2 Regression 32
4. CODE 34-51
5.1 Testing 52
6. CONCLUSION 59
7. FUTURE SCOPE 60
8. REFERENCES 61
7
1. INTRODUCTION
In recent times, the Information Technology and e-health care system in the
medical field helps clinical experts to provide better health care to the patient.
This study addresses the problems of segmentation of abnormal brain tissues and
normal tissues as Gray Matter(GM), White Matter(WM), and CerebroSpinal
Fluid (CSF) from Magnetic Resonance(MR) images using FCM segmentation
and linear regression method as classifier .
MRI is mostly used because of the non-ionizing radiation that is being used.
Another advantage with MRI is that the investigators can manipulate the amount
of contrast between different biological tissues by changing elements at the time
of image acquisition.
The MRI data consists of T1, T2 and Photon Density (PD) weighted features of
images. Photon MRI records signals arising predominantly from free water, i.e.,
water not bio-chemically bound in complex molecules, and also contains small
contribution of fat from protons. Photon Density (PD) is based on the number of
8
nuclei stimulated and the relaxation times, T1 and T2, which reflect the chemical
environment of the tissue stimulated, each determine in the different ways the
intensity at which different tissues will appear in an acquired image. T 1 is an
exponential time constant which represents the time taken for excited nuclei to
return to equilibrium after the RF (Radio Frequency) pulse has been turned off.
T2 is an exponential time constant describing the time it takes for the excited
nuclei to loose signal (mainly due to dephasing in the transverse plane). The time
between radio frequency pulses (TR) and the amount of time after the pulse at
which the signal is acquired (echo time or TE), determine the influence each of
these parameters (PD, T1 and T2) on the acquired image.
1.1Project overview
Further the output of the FCM algorithm is then fed to the linear regression
method.
Hard-disk : 1TB
9
RAM : 4 GB
1.2.3.1 Python :
Python is a powerful high-level, object-oriented programming language created
by Guido van Rossum. It has simple easy-to-use syntax, making it the perfect
language for someone trying to learn computer programming for the first time.
Python is a general-purpose language. It has wide range of applications from Web
development (like: Django and Flask), scientific and mathematical computing
(Orange, SymPy, NumPy) to desktop graphical user Interfaces (Pygame,
Panda3D). The syntax of the language is clean and length of the code is relatively
short. Python is considered to be best language used for implementation of
machine learning. The reasons for this would be Ease of learning, Adequate
libraries and tools, Community support. Python has comparatively very less
syntax and the ease of writing a very large program in python is very high.
Libraries like Numpy, Pandas, etc. that are very much essential for doing machine
learning are available for python. Machine learning with python has a very high
community support in the internet so it makes easy to work with python and clear
the hurdles.
NumPy:
NumPy is the fundamental package for scientific computing with Python. It
10
contains among other things like a powerful N-dimensional array object,
sophisticated (broadcasting) functions, tools for integrating C/C++ and Fortran
code, useful linear algebra, Fourier transform, and random number capabilities.
It has functions written in built for higher mathematical calculations required for
the execution of machine learning algorithms.
Scikit-image:
Scikit-image is an open source image processing library for
the Python programming language. It includes algorithms for segmentation,
geometric transformations, color space manipulation, analysis, filtering,
morphology, feature detection, and more. It is designed to interoperate with the
Python numerical and scientific libraries NumPy and Scipy.
Scikit-learn:
Scikit-learn is a free software machine learning library for python programming
language, it features various classification, regression and clustering algorithm
OpenCV:
OpenCV (Open Source Computer Vision Library) is released under a BSD
license and hence it’s free for both academic and commercial use. It has C++, C,
Python and Java interfaces and supports Windows, Linux, Mac OS, iOS and
Android. OpenCV was designed for computational efficiency and with a strong
focus on real-time applications. Written in optimized C/C++, the library can take
advantage of multi-core processing. Enabled with OpenCL, it can take advantage
of the hardware acceleration of the underlying heterogeneous compute platform.
11
Installation :Pip install opencv-contrib-python
Matplotlib:
Pandas:
Pandas is an open source, BSD-licensed library providing high-performance,
easy-to-use data structures and data analysis tools for the Python programming
language. This helps in handling large amounts of data with help of data structures
like Series, Data Frames etc. It has inbuilt methods for reading and writing data
in different formats like CSV, xlsx, HTML etc. Different machine learning
algorithms have the compatibility for pandas data structures
12
2.SYSTEM STUDY AND ANALYSIS
A tumor is said to be the growth in the abnormal tissue of the brain which causes
damage to the functioning cells. Brain tumor detection is very difficult as there
are many techniques available for it. Magnetic Resonance Imaging (MRI) is the
active resource for detecting brain tumor. It is necessary to use technique which
can give the accurate location and size of the tumor. It has been extended and
modified in many ways in order to solve the image segmentation problem.
However, almost all the extensions require the adjustment of at least one
parameter that depends on the image itself. Image Preprocessing techniques are
applied to improve the quality of image. MR Image segmentation is based on set
of measurable features which are extracted. In this process of brain tumor
detection, pixel intensity based features are extracted. Image Segmentation group
pixels into regions and hence defines the object regions. Segmentation uses the
features extracted from an image.
In this project, MRI image segmentation is done using Fuzzy C-Means clustering
algorithm as it yields better results than any other technique. Then classification
is done using machine learning technique. In this we used linear regression
technique to classify the stage of the tumour.
13
Advantages:
2.3.1 X-RAYS
X-rays (radiographs) are the most common and widely available diagnostic
imaging technique. Even if you also need more sophisticated tests, you will
probably get an X-ray first.
The part of your body being pictured is positioned between the X-ray machine
and photographic film. You have to hold still while the machine briefly sends
electromagnetic waves (radiation) through your body, exposing the film to reflect
your internal structure. The level of radiation exposure from X-rays is not
harmful, but your doctor will take special precautions if you are pregnant.
Bones, tumors and other dense matter appear white or light because they absorb
the radiation. Less dense soft tissues and breaks in bone let radiation pass through,
making these parts look darker on the X-ray film. Sometimes, to make certain
organs stand out in the picture, you are asked given barium sulfate or a dye.
X-rays may not show as much detail as an image produced using newer, more
powerful techniques.
14
2.3.2 POSITRON EMISSION TOMOGRAPHY(PET)
Operation
To conduct the scan, a short-lived radioactive tracer isotope is injected into the
living subject (usually into blood circulation). The tracer is chemically
incorporated into a biologically active molecule. There is a waiting period while
the active molecule becomes concentrated in tissues of interest; then the subject
is placed in the imaging scanner. The molecule most commonly used for this
purpose is fluorodeoxyglucose (FDG), a sugar, for which the waiting period is
typically an hour. During the scan a record of tissue concentration is made as the
tracer decays.
15
In the Figure 2.3.2, as the radioisotope undergoes positron emission decay (also
known as positive beta decay), it emits a positron, an antiparticle of
the electron with opposite charge. The emitted positron travels in tissue for a
short distance (typically less than 1 mm, but dependent on the isotope), during
which time it loses kinetic energy, until it decelerates to a point where it can
interact with an electron. The encounter annihilates both electron and positron,
producing a pair of annihilation (gamma) photons moving in approximately
opposite directions. These are detected when they reach a scintillator in the
scanning device, creating a burst of light which is detected
by photomultiplier tubes or silicon avalanche photodiodes (Si APD). The
technique depends on simultaneous or coincident detection of the pair of photons
moving in approximately opposite direction (it would be exactly opposite in
their center of mass frame, but the scanner has no way to know this, and so has a
built-in slight direction-error tolerance). Photons that do not arrive in temporal
"pairs" (i.e. within a timing-window of a few nanoseconds) are ignored.
16
Figure 2.3.3(a) Schematic of a CT system.
17
Figure 2.3.3(b) Schematic representation of CT scanner
Why It Is Done
CT scans are used to study areas of the body and the arms or legs.
Chest(thorax). A CT scan of the chest can look for problems with the lungs,
18
heart, esophagus, the major blood vessel (aorta), or the tissues in the center of
the chest. Some common chest problems a CT scan may find include infection,
lungcancer, a pulmonary embolism, and an aneurysm. It also can be used to see
if cancer has spread into the chest from another area of the body.
19
Arm or leg. A CT scan can look for problems of the arms or legs, including the
shoulder, elbow, wrist, hand, hip, knee, ankle, or foot.
MRI machines make use of the fact that body tissue contains lots of water, and
20
hence protons(H nuclei), which get aligned in a large magnetic field. Each water
molecule has two hydrogen nuclei or protons. When a person is inside the
powerful magnetic field of the scanner, the average magnetic moment of many
protons becomes aligned with the direction of the field. A radio frequency current
is briefly turned on, producing a varying electromagnetic field. This
electromagnetic field has just the right frequency, known as
the resonance frequency, to be absorbed and flip the spin of the protons in the
magnetic field. After the electromagnetic field is turned off, the spins of the
protons return to thermodynamic equilibrium and the bulk magnetization
becomes re-aligned with the static magnetic field. During this relaxation, a radio
frequency signal (electromagnetic radiation in the RF range) is generated, which
can be measured with receiver coils. Information about the origin of the signal
in 3D space can be learned by applying additional magnetic fields during the
scan. These additional magnetic fields can be used to only generate detectable
signal from specific locations in the body (spatial excitation) and/or to make
magnetization at different spatial locations process at different frequencies, which
enables k-space encoding of spatial information. The 3D images obtained in MRI
can be rotated along arbitrary orientations and manipulated by the doctor to be
better able to detect tiny changes of structures within the body. These fields,
generated by passing electric currents through gradient coils, make the magnetic
field strength vary depending on the position within the magnet. Because this
makes the frequency of the released radio signal also dependent on its origin in a
predictable manner, the distribution of protons in the body can be mathematically
recovered from the signal, typically by the use of the inverse Fourier transform.
21
the settings on the scanner, this effect is used to create contrast between different
types of body tissue or between other properties, as in fMRI and diffusion
MRI.MRI is used to image every part of the body, and is particularly useful for
tissues with many hydrogen nuclei and little density contrast, such as
the brain, muscle, connective tissue and most tumors.
Magnetic field
MRI scans require a magnetic field with two properties, uniform field
density and strength. The magnetic field cannot vary more than 1/10,000 of 1%
and field strength ranges (depending on the scanner) from 0.2 to 3 tesla in
strength in currently clinically used scanners, with research scanners
investigating higher field strengths such as seven tesla. The lower field strengths
can be achieved with permanent magnets, which are often used in "open" MRI
scanners, for claustrophobic patients. Higher field strengths can be achieved only
with superconducting magnets. An MRI with a 3.0 tesla strength magnet may be
referred to as a "3-T MRI" or "3-tesla MRI". Since the gradient coils are within
the bore of the scanner, there are large forces between them and the main field
coils, producing most of the noise that is heard during operation. Without efforts
to damp this noise, it can approach 130 decibels (dB) with strong fields
22
electronically activated devices that have approval from the US FDA to
permitMRI procedures in patients under highly specific MRI conditions. In the
case of cochlear implants, the US FDA has approved some implants for MRI
compatibility. In the case of cardiac pacemakers, the results can sometimes be
lethal, so patients with such implants are generally not eligible for MRI.
Prepolarized MRI
23
indicating the differing conditions of the tissue under investigation. During an
MRI scan, under the influence of the strong magnetic field, hydrogen atoms in
our body become ordered. The protons in the atoms either point up or down. Most
cancel each other out - the same number point up as down.When the radio
frequency is switched off, the protons release energy, emitting a signal picked up
by wire coils and sent to the MRI machine’s computer system, where the data is
converted to an image. Undergoing an MRI isn’t painful, but it’s also not a
particularly pleasant experience. Before you start, you need to remove all metal
objects such as watches and rings. The nurse will ask you if have any metal inside
your body. You have to lie inside a narrow tube, where you must keep completely
still during the operation of the machine. While the machine is working, there
will be some extremely loud noises; the nurse will give you earplugs. There are
smaller scanners that are less enclosed, but they may not be quite as accurate.PET
scans are increasingly read alongside CT or magnetic resonance imaging(MRI)
scans, with the combination (called "co-registration") giving both anatomic and
metabolic information (i.e., what the structure is, and what it is doing
biochemically). Because PET imaging is most useful in combination with
anatomical imaging, such as CT, modern PET scanners are now available with
integrated high-end multi-detector-row CT scanners (so-called "PET/CT").
Because the two scans can be performed in immediate sequence during the same
session, with the patient not changing position between the two types of scans,
the two sets of images are more-precisely registered, so that areas of abnormality
on the PET imaging can be more perfectly correlated with anatomy on the CT
images. This is very useful in showing detailed views of moving organs or
structures with higher anatomical variation, which is more common outside the
brain.
24
2.3.6 Types of MRI sequences
T1 weighted images are produced by using short TR and TE times. The contrast
and brightness of the image are predominantly determined by T1 properties of
tissue.
25
2.3.6.2. T2 WEIGHTED images
T2 weighted images are produced by using longer TE and TR times. The contrast
and brightness of the image are predominantly determined by T2 properties of
tissue.
26
2.3.6.3. Fluid Attenuated Inversion Recovery(FLAIR)images
2.3.6.4. PD images
Proton density images were extensively used for brain imaging, however they
have largely been replaced by FLAIR. PD however continues to offer excellent
signal distinction between fluid, hyaline cartilage and fibrocartilage, which
makes this sequence ideal in the assessment of joints.
27
3.SYSTEM DESIGN
28
where in the generative models are used for pre-processing and thus a stable
input is generated for discriminative model which can then train to predict more
complex labels.
Pre-processing:
The purpose of preprocessing is to discard irrelevant information in the input data,
that can negatively affect the recognition. This concerns speed and accuracy.
Preprocessing usually consists of binarization, normalization, sampling,
smoothing and denoising. This project concentrates majorly on this part of the
digit recognition procedure.
29
Skull stripping:
Segmentation:
Feature extraction:
Classification:
There are mainly two types of Machine Learning algorithms which are mentioned
below:
• Supervised Machine Learning
31
3.3.1.1 Classification:
3.3.1.2 Regression:
32
For example, if a cell phone company wants optimize the locations where they
build cell phone towers, they can use machine learning to estimate the number of
clusters of people relying on their towers. A phone can only talk to one tower at
a time, so the team uses clustering algorithms to design the best placement of cell
towers to optimize signal reception for groups, or clusters, of their customers.
33
4. CODE
4.1 ALGORITHM:
Fuzzy c-means (FCM) is a method of clustering which allows one piece of data
to belong to two or more clusters.
where m is any real number greater than 1, uij is the degree of membership of xi in
the cluster j, xi is the ith of d-dimensional measured data, cj is the d-dimension
center of the cluster, and ||*|| is any norm expressing the similarity between any
measured data and the center.
34
The algorithm is composed of the following steps:
A linear regression classifier with thresholding due to its low variance, high
accuracy and lower computation time compared with other classifiers. A linear
regression classifier with thresholding due to its low variance, high accuracy and
lower computation time compared with other classifiers. The value of the
threshold can determine the sensitivity and specificity of the segmentations; i.e.
choosing lower threshold values can increase the sensitivity of the segmentations
with the price of decreasing the specificity, and vice versa. A linear regression
35
line has an equation of the form Y = a + bX, where X is the explanatory variable
and Y is the dependent variable. The slope of the line is b, and a is the intercept
(the value of y when x = 0).
However, the proposed LRC approach, for the first time, simply uses the down
sampled images in combination with the linear regression classification to
achieve superior results compared to the benchmark techniques.
36
4.2 Pseudo code:
1) import wx
2) import argparse
3) import cv2
4) import matplotlib.pyplot as plt
5) import cv2
6) import numpy as np
7) import time
8) import os
9) import itertools
10) from PIL import Image
11) from numpy import average, linalg, dot
12) from skimage.segmentation import slic
13) from skimage.segmentation import mark_boundaries
14) from skimage.util import img_as_float
15) import matplotlib.pyplot as plt
16) import numpy as np
17) import argparse
18) from PIL import Image, ImageStat
19) from scipy.stats import kurtosis, skew
20) import math
21) import argparse
22) import imutils
23) import pywt
24) import pywt.data
25) import matplotlib.pyplot as plt
26) import math
37
27) from matplotlib.figure import Figure
28) from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg
as FigureCanvas
29) class Example(wx.Frame):
30) def __init__(self, *args, **kw):
31) super(Example, self).__init__(*args, **kw)
32) self.InitUI()
33) def InitUI(self):
34) pnl = wx.Panel(self)
35) font = wx.Font(12, wx.DEFAULT, wx.NORMAL, wx.BOLD)
36) heading = wx.StaticText(self, label='Brain Tumour Detection on MRI
IMages ',
1. pos=(100,50), size=(200, -1))
37) heading.SetFont(font)
38) wx.StaticText(self, label='Upload Image', pos=(25, 100))
39) , self.OnClose1)
40) self.SetSize((600, 400))
41) self.SetTitle('CBIR')
42) self.Centre()
43) def OnClose(self, e):
44) frame = wx.Frame(None, -1, 'win.py')
45) frame.SetDimensions(0,0,200,50)
46) openFileDialog = wx.FileDialog(frame, "Open", "", "",
a. "",
b. wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
47) openFileDialog.ShowModal()
48) self.st=wx.TextCtrl(self, -1, openFileDialog.GetPath(), pos=(140, 100))
49) query = cv2.imread(openFileDialog.GetPath())
38
50) cv2.imshow("Input MRi Image", query)
51) def fcm(self,e):
52) import numpy as np
53) import matplotlib.pyplot as plt
54) import skfuzzy as fuzz
55) import os
56) import cv2
57) import numpy as np
58) from time import time
59) def change_color_fuzzycmeans(cluster_membership, clusters):
a. img = []
b. for pix in cluster_membership.T:
i. img.append(clusters[np.argmax(pix)])
c. return img
60) def readimage():
a. folder =self.st.GetLabelText()
b. img = cv2.imread(folder)
c. list_img=[]
d. print img
e. print(img.shape)
f. rgb_img = img.reshape((img.shape[0] * img.shape[1], 3))
g. list_img.append(rgb_img)
h. return list_img
61) def bwarea(img):
a. row = img.shape[0]
b. col = img.shape[1]
c. total = 0.0
d. for r in range(row-1):
39
i. for c in range(col-1):
ii. sub_total = img[r:r+2, c:c+2].mean()
iii. if sub_total == 255:
iv. total += 1
v. elif sub_total == (255.0/3.0):
vi. total += (7.0/8.0)
vii. elif sub_total == (255.0/4.0):
viii. total += 0.25
ix. elif sub_total == 0:
x. total += 0
xi. else:
xii. r1c1 = img[r,c]
xiii. r1c2 = img[r,c+1]
xiv. r2c1 = img[r+1,c]
xv. r2c2 = img[r+1,c+1]
xvi. if (((r1c1 == r2c2) & (r1c2 == r2c1)) & (r1c1 != r2c1)):
xvii. total += 0.75
xviii. else:
xix. total += 0.5
e. return total
62) def imclearborder(imgBW):
a. radius = 2
b. imgBWcopy = imgBW.copy()
c. image, contours,hierarchy = cv2.findContours(imgBWcopy.copy(),
cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
d. imgRows = imgBW.shape[0]
e. imgCols = imgBW.shape[1]
f. contourList = []
40
g. for idx in np.arange(len(contours)):
i. cnt = contours[idx]
ii. for pt in cnt:
iii. rowCnt = pt[0][1]
iv. colCnt = pt[0][0]
v. check1 = (rowCnt >= 0 and rowCnt < radius) or (rowCnt >=
imgRows-1-radius and rowCnt < imgRows)
vi. check2 = (colCnt >= 0 and colCnt < radius) or (colCnt >=
imgCols-1-radius and colCnt < imgCols)
vii. if check1 or check2:
viii. contourList.append(idx)
ix. break
h. for idx in contourList:
i. cv2.drawContours(imgBWcopy, contours, idx, (0,0,0), -1)
i. return imgBWcopy
63) def bwareaopen(imgBW, areaPixels):
a. imgBWcopy = imgBW.copy()
b. image, contours,hierarchy = cv2.findContours(imgBWcopy.copy(),
cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
c. for idx in np.arange(len(contours)):
i. area = cv2.contourArea(contours[idx])
ii. if (area >= 0 and area <= areaPixels):
iii. cv2.drawContours(imgBWcopy, contours, idx, (0,0,0), -1)
iv. return imgBWcopy
64) def imfill(im_th):
a. im_floodfill = im_th.copy()
b. h, w = im_th.shape[:2]
c. mask = np.zeros((h+2, w+2), np.uint8)
41
65) cv2.floodFill(im_floodfill, mask, (0,0), 255);
66) im_floodfill_inv = cv2.bitwise_not(im_floodfill)
67) im_out = im_th | im_floodfill_inv
a. return im_out
68) list_img = readimage()
69) n_data = len(list_img)
70) clusters = [2,3,6]
71) for index,rgb_img in enumerate(list_img):
a. img = np.reshape(rgb_img, (240,240,3)).astype(np.uint8)
b. shape = np.shape(img)
c. plt.figure(figsize=(20,20))
d. plt.subplot(1,4,1)
e. plt.imshow(img)
f. print('Image '+str(index+1))
g. for i,cluster in enumerate(clusters):
h. new_time = time()
i. cntr, u, u0, d, jm, p, fpc = fuzz.cluster.cmeans(rgb_img.T, cluster, 2,
error=0.005, maxiter=1000, init=None,seed=42)
i. new_img = change_color_fuzzycmeans(u,cntr)
ii. fuzzy_img = np.reshape(new_img,shape).astype(np.uint8)
iii. ret, seg_img = cv2.threshold(fuzzy_img,np.max(fuzzy_img)-
1,255,cv2.THRESH_BINARY)
iv. print('Fuzzy time for cluster',cluster)
v. print(time() - new_time,'seconds')
vi. seg_img_1d = seg_img[:,:,1]
vii. bwfim1 = bwareaopen(seg_img_1d, 100)
viii. bwfim2 = imclearborder(bwfim1)
ix. bwfim3 = imfill(bwfim2)
42
x. print('Bwarea : '+str(bwarea(bwfim3)))
xi. print()
xii. if i==0:
xiii. plt.subplot(1,4,i+2)
xiv. if i==2:
xv. plt.subplot(1,4,i+1)
xvi. plt.imshow(bwfim3)
xvii. name = 'Cluster'+str(cluster)
xviii. if i==1:
xix. plt.title('affected area')
xx. if i==2:
xxi. plt.title('actual tumour')
xxii. name = 'segmented'+str(index)+'.png'
xxiii. print()
72) plt.show(name)
73) def OnClose1(self, e):
74) query = cv2.imread(self.st.GetLabelText())
75) original = query
76) titles = ['Approximation', ' Horizontal detail',
i. 'Vertical detail', 'Diagonal detail']
b. coeffs2 = pywt.dwt2(original, 'bior1.3')
c. LL, (LH, HL, HH) = coeffs2
d. fig = plt.figure(figsize=(12, 3))
e. for i, a in enumerate([LL, LH, HL, HH]):
i. ax = fig.add_subplot(1, 4, i + 1)
ii. ax.set_title(titles[i], fontsize=10)
iii. ax.set_xticks([])
iv. ax.set_yticks([])
43
f. fig.tight_layout()
g. file = self.st.GetLabelText()
h. superpixel(file)
i. feature ( file )
j. existingcall(file , 0, type='rect')
k. o1 = ertcall(file , 0, type='rect')
l. ij =0
m. fi= load_images("data/")
n. tr= loadtrain ("Train/")
o. sv= savefile("dataset/")
p. for trf in tr :
i. ii= image_similarity_vectors_via_numpy(file ,trf )
ii. ij += ii
q. avgval = ij /len (tr)
r. print 'Affected Area '
s. lev=1-avgval
t. lev=lev/2
u. lev=lev*100
v. print lev
w. if avgval >.7:
i. print ' Highlevel Tumour '
ii. img=cv2.imread(file)
iii. print file
iv. cv2.imwrite( 'Result/High/Dataset/'+file, img)
x. elif avgval >.5:
i. print ' Intermediate Tumour '
ii. img = cv2.imread(file)
iii. print file
44
y. cv2.imwrite('Result/Intermediate/Dataset/' + file, img)
z. else:
i. print 'begining '
ii. img = cv2.imread(file)
iii. print file
iv. cv2.imwrite('Result/High/'+file, img)
v. cv2.imwrite('Result/Begining/Dataset/' + file, img)
vi. cv2.waitKey(0)
77) def feature ( filename ):
78) img = Image.open(filename)
79) stat = ImageStat.Stat(img)
80) stat = ImageStat.Stat(img)
81) imageFile = filename
82) im1 = Image.open('2.jpg')
83) rgbHistogram = im1.histogram()
84) for rgb in range(3):
85) totalPixels = sum(rgbHistogram[rgb * 256 : (rgb + 1) * 256])
86) ent = 0.0
87) for col in range(rgb * 256, (rgb + 1) * 256):
a. freq = float(rgbHistogram[col]) / totalPixels
b. if freq > 0:
i. ent = ent + freq * math.log(freq, 2)
88) ent = -ent
89) im = Image.open(filename)
90) im_grey = im.convert('LA')
91) width,height = im.size
92) total=0
93) for i in range(0,width):
45
94) for j in range(0,height):
a. total += im_grey.getpixel((i,j))[0]
95) mean = total / (width * height)
96) image = cv2.imread(filename)
97) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
98) blurred = cv2.GaussianBlur(gray, (5 , 5), 0)
99) thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]
100) g_kernel = cv2.getGaborKernel((21, 21), 8.0, np.pi/4, 10.0, 0.5, 0,
ktype=cv2.CV_32F)
101) img = cv2.imread(filename)
102) img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
103) filtered_img = cv2.filter2D(img, cv2.CV_8UC3, g_kernel)
104) img = cv2.imread(filename,0)
105) ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
106) ret,thresh2 =
cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV)
107) ret,thresh3 = cv2.threshold(img,127,255,cv2.THRESH_TRUNC)
108) ret,thresh4 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO)
109) ret,thresh5 =
cv2.threshold(img,127,255,cv2.THRESH_TOZERO_INV)
110) titles = ['Original
Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
111) images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]
112) for i in xrange(6):
113) plt.subplot(2,3,i+1),plt.imshow(images[i],'gray')
114) plt.title(titles[i])
115) plt.xticks([]),plt.yticks([])
116) img = img
46
117) edges = cv2.Canny(img,100,200)
118) plt.subplot(121)
119) segments = slic(img_as_float(image), n_segments = 100, sigma = 5)
120) fig = plt.figure("Superpixels")
121) ax = fig.add_subplot(1, 1, 1)
122) ax.imshow(mark_boundaries(img_as_float(cv2.cvtColor(image,
cv2.COLOR_BGR2RGB)), segments))
123) plt.axis("off")
124) img = image
125) gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
126) gray = np.float32(gray)
127) dst = cv2.cornerHarris(gray,2,3,0.04)
128) dst = cv2.dilate(dst,None)
129) img[dst>0.01*dst.max()]=[0,0,255]
130) def enhance(img):
131) kernel = np.array([[-1, 0, 1], [-2, 0, 2], [1, 0, 1]])
132) return cv2.filter2D(img, -1, kernel)
133) def load_images(folder):
134) images = []
135) for filename in os.listdir(folder):
136) img = os.path.join(folder, filename)
137) if img is not None:
a. images.append(img)
138) return images
139) def loadtrain(folder):
140) images = []
141) for filename in os.listdir(folder):
142) img = os.path.join(folder, filename)
47
143) if img is not None:
a. images.append(img)
144) return images
145) def savefile(folder):
146) images = []
147) for filename in os.listdir(folder):
148) img = filename
149) if img is not None:
a. images.append(img)
150) return images
151) def image_similarity_vectors_via_numpy(filepath1, filepath2):
152) image1 = Image.open(filepath1)
153) image2 = Image.open(filepath2)
154) vectors = []
155) norms = []
156) for image in images:
157) vector = []
158) for pixel_tuple in image.getdata():
159) vector.append(average(pixel_tuple))
160) vectors.append(vector)
161) norms.append(linalg.norm(vector, 2))
162) a, b = vectors
163) a_norm, b_norm = norms
164) res = dot(a / a_norm, b / b_norm)
165) return res
166) def get_thumbnail(image, size=(128,128), greyscale=False):
167) image = image.resize(size, Image.ANTIALIAS)
168) if greyscale:
48
169) image = image.convert('L')
170) return image
171) def existingcall(name, debug, type=None, **options):
172) se_shape = (16, 4)
173) if type == 'rect':
174) se_shape = (17, 4)
175) elif type == 'square':
176) se_shape = (7, 6)
177) raw_image = cv2.imread(name, 1)
178) input_image = np.copy(raw_image)
179) gray = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY)
180) gray = enhance(gray)
181) exist=gray
182) gray_blur = cv2.GaussianBlur(gray, (15, 15), 0)
183) thresh = cv2.adaptiveThreshold(gray_blur, 255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_IN
V, 11, 1)
184) kernel = np.ones((3, 3), np.uint8)
185) closing = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE,kernel,
iterations=4)
186) input_image1=input_image
187) im = input_image
188) im1 = input_image
189) sure_bg = cv2.dilate(closing, kernel, iterations=3)
190) dist_transform = cv2.distanceTransform(closing, cv2.DIST_L2, 5)
191) ret, sure_fg = cv2.threshold(dist_transform, 0.7 *
dist_transform.max(), 255, 0)
192) markers[unknown == 255] = 0
49
193) imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
194) ret, thresh = cv2.threshold(imgray, 110, 255, 0)
195) _, contours, _ = cv2.findContours(thresh, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
196) cv2.drawContours(im1, contours, -1, (255, 0, 0), 1)
197) return input_image
198) def ertcall(name, debug, type=None, **options):
199) se_shape = (16, 4)
200) if type == 'rect':
201) se_shape = (17, 4)
202) elif type == 'square':
203) se_shape = (7, 6)
204) raw_image = cv2.imread(name, 1)
205) input_image = np.copy(raw_image)
206) thresh = cv2.adaptiveThreshold(gray_blur, 255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_IN
V, 11, 1)
207) kernel = np.ones((3, 3), np.uint8)
208) closing = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE,kernel,
iterations=4)
209) input_image1=input_image
210) im = input_image
211) im1 = input_image
212) unknown = cv2.subtract(sure_bg, sure_fg)
213) ret, markers = cv2.connectedComponents(sure_fg)
214) markers = markers + 1
215) markers[unknown == 255] = 0
216) imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
50
217) ret, thresh = cv2.threshold(imgray, 127, 255, 0)
218) _, contours, _ = cv2.findContours(thresh, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
219) cv2.drawContours(im1, contours, -1, (0, 255, 0), 1)
220) return input_image
221) def main():
222) app = wx.App()
223) ex = Example(None)
224) ex.Show()
225) app.MainLoop()
226) if __name__ == '__main__':
227) main()
51
5. TESTING AND DEPLOYMENT
5.1 TESTING
TYPES OF TESTING
• SYSTEM TESTING
System testing ensures that the entire integrated software system meets
requirements. It tests a configuration to ensure known and predictable
results. An example of system testing is the configuration oriented system
integration test. System testing is based on process descriptions and flows,
emphasizing pre-driven process links and integration points.
Environment Testing
Performance Testing
52
TEST CASES:
1.If the user click on process without uploading the image then an error occurs
stating that the image is not read.
53
2. The image shape we are taking is (240,240,3) when user gives an image which
has different dimensions then error occurs.
54
3. When the user uploads the image and when there is an error in import statement
Error message:
55
5.2 OUTPUTS:
User-interface:
Uploading image:
56
Processing:
57
Final output after classification:
58
6.CONCLUSION
59
7.FUTURE SCOPE
Though the project gives highest accuracy in identifying the tumour part in brain
magnetic resonance image and predict the stage in which the tumour is, this
project still had gaps which may be considered as the future scope of the project.
Further the project can be extended, for submission of magnetic resonance image
through a mobile application to the expert system and get the diagnostic
recommendations from the remote location
60
8.REFERENCES
[2] Pal S.K. and Pal N.R, “A Review on Image Segmentation Techniques”,
Pattern Recognition, Vol.26, N0.9, Pp. 1277-1294. 1993.
[3] Palvidis.T, “Structural Pattern Recognition”, Springer, New York, 1977.
[4] Parikh J.A, “A Comparative Study Of Cloud Classification Techniques”,
Remote Sensing Of Environment, Vol.6, Pp. 67-81, 1977.
[5] Davis L.S, “Image Texture Analysis Techniques- A Survey”, Digital
Image Processing, Simon and R.M. Haralick (Eds.) Pp 189-201, 1981.
[6] Fu.K.S and Mui J.K, “A Survey On Image Segmentation “, Pattern
Recognition, Vol.12, Pp. 3-16, 1981.
[7] Sahoo P.K etal, “A Survey Of Thresholding Techniques”, Computer
Vision, Graphics and Image Processing, Vol.41, Pp. 233-260, 1988.
[8] Rahman Farnoosh et al, “Image Segmentation Using Gaussian Mixture
Models”, 2000
[9] Nagesh Vadaparthi, SrinivasYarramalle, Suresh Varma.P,
“Unsupervised medical Image Segmentation on Brain MRI images
using Skew Gaussian Distribution”, IEEE-International Conference on
Recent Trends in Information Technology, 2011, pp.1293-1297.
[10] McCallum, A.; Nigam, K.; and Ungar L.H. (2000) "Efficient Clustering
of High Dimensional Data Sets with Application to Reference
Matching", Proceedings of the sixth ACM SIGKDD international
conference on Knowledge discovery and data mining, 169-178.
61