Project Report Format
Project Report Format
CERTIFICATE
This is to certify that the Real Time Research (CS456PC) entitled “EYE DISEASE
DETECTION USING MACHINE LEARNING” is being submitted by MOHD.
MUNEERUDDIN bearing Roll No: 21265A0515 in the partial fulfillment for the
award of Bachelor of Technology in Computer Science and Engineering to
Jawaharlal Nehru Technological University Hyderabad is a record of bonafide
work carried out under the guidance and supervision.The results embodied in this
project have not been submitted to any other University or Institute for the award of
any degree or diploma.
Ms K Shirisha
Asst.Professor
1
External Examiner
DECLARATION
This is to certify that the work reported in this project titled “EYE DISEASE
DETECTION USING MACHINE LEARNING” is a record of work done by me
in the Department of Computer Science and Engineering, Mahatma Gandhi
Institute and Technology, Hyderabad. No part of the work is copied from
books/journals/internet and wherever the portion is taken, the same has been duly
referred to in the text. The report is based on the work done entirely by me and not
copied from any other source.
MOHD. MUNEERUDDIN
21265A0515
2
ACKNOWLEDGEMENT
The satisfaction that accompanies the successful completion of any task would be
incomplete without the mention of people who made it possible because success is
the abstract of hard work and perseverance, but steadfast of all is encouraging
guidance. So, I acknowledge all those whose guidance and encouragement served
as a beacon light and crowned my efforts with success.
Finally, I would also like to thank all the faculty and staff of the CSE Department
who helped us directly or indirectly for completing this project.
MOHD. MUNEERUDDIN
21265A0515
3
TABLE OF CONTENTS
CERTIFICATE i
DECLARATION ii
ACKNOWLEDGEMENT iii
LIST OF FIGURES vi
LIST OF TABLES vii
ABSTRACT viii
1. INTRODUCTION 1
1.1 Problem Statement 1
1.2 Objectives 2
1.3 Existing System and its Disadvantages 2
1.4 Proposed System and its Advantages 3
1.5 System Requirements 4
1.5.1 Software Requirements 4
1.5.2 Hardware Requirements 4
2. LITERATURE SURVEY 5
2.1 Previously implemented Techniques
2.2 Tabular Representation
3. DESIGN AND METHODOLOGY 8
3.1 System Architecture 8
3.2 Algorithms and Metrics 9
3.2.1 Mean Squared Error 9
3.2.2 Brute-force matching with SIFT descriptors and ratio test (BFSIFT) 10
3.2.3 Brute- Force Matching with ORB Descriptors(BFOD) 11
3.2.4 K-Nearest Neighbors (KNN) 13
3.3 Library Used 14
3.3.1 Fast Library for Approximate Nearest Neighbors (FLANN) 14
3.4 UML Diagrams 15
3.4.1 Class Diagram 15
3.4.2 Use Case Diagram 16
3.4.3 Sequence Diagram 16
4. Testing and Results 17
4.1 Testing 17
4
4.2 Results 17
4.2.1 Login Page 17
4.2.2 Home Page 18
4.2.3 Select Eye Image 18
4.2.4 Select method 19
4.2.5 Using Mean Square Error(MSE) method 19
4.2.6 Using Brute Fore matching with ORB Descriptors(BFOD) 20
4.2.7 Using Brute Force Matching with SIFT Descriptors and Ratio Test
(BFSIFT) method 21
4.2.8 Using Fast Library for Approximate Nearest Neighbors (FLANN)
method 22
5. CONCLUSION AND FUTURE SCOPE 24
5.1 Conclusion 24
5.2 Future Scope 24
BIBLIOGRAPHY 25
APPENDIX 26
5
LIST OF FIGURES
6
LIST OF TABLES
7
ABSTRACT
8
1. INTRODUCTION
This proposed system endeavors to showcase the efficacy and accuracy of the
employed algorithms and techniques, showcasing their proficiency in distinguishing
between healthy and diseased eye images. Through this research, we aim to contribute
significantly to the early detection and subsequent treatment of prevalent eye
ailments, thereby potentially reducing the incidence of blindness caused by these
conditions.
The convergence of machine learning, image analysis, and medical diagnostics forms
the cornerstone of this proposed system, ushering in a promising era where
technology becomes instrumental in safeguarding and enhancing visual health.
Millions worldwide suffer from visual impairment due to diseases like cataracts,
glaucoma, and conjunctivitis. Early diagnosis is crucial for effective treatment and
preventing vision loss. Traditional methods often require specialist expertise and
expensive equipment, limiting access in rural areas and for underprivileged
1
populations. The proposed system addresses the need for a readily accessible and
affordable early detection system for these prevalent eye diseases. The challenge lies
in early-stage eye disease symptoms that can be subtle and challenging to identify.
The proposed system seeks to tackles this challenge by proposing a novel image-
based system utilizing machine learning techniques for automated eye disease
detection.
1.2 Objectives
Proposed system for Eye Disease Detection using Machine Learning is concentrated
on four points. First of all, to develop an automated system for early detection of
common eye diseases such as cataracts, glaucoma, conjunctivitis from eye image,
which achieve high accuracy of >90% in classifying eye disease and which is
compared to ground truth diagnoses by ophthalmologists. After that, it will optimize
the image analysis and classification processes for speed and efficiency, i.e.,
Minimizing wait time for diagnoses (< 1 minute per each image). Later, it will train
the system on a diverse dataset of eye images to ensure accurate performance across
different demographics and disease stages. Finally, to develop a user-friendly
interface to input the eye images and display the results.
Automated Retinal Imaging Systems have gained attraction in recent years for their
capability to capture high-resolution images of the retina, aiding in the detection of
various eye diseases. These systems utilize specialized cameras to capture detailed
images of the retina, and then employ image processing algorithms to analyze these
images for signs of diseases such as diabetic retinopathy, glaucoma, and macular
degeneration.
Disadvantages:
2
systems limits their widespread adoption, hindering access to early diagnosis
for a larger population.
The proposed system integrates machine learning algorithms and image analysis
techniques to develop a system capable of accurately detecting multiple classes of eye
diseases, including cataracts, glaucoma, conjunctivitis, and eye allergies. This
proposed system utilizes a diverse dataset of eye images to train and fine-tune deep
learning models, enabling precise classification and diagnosis of various ocular
conditions.
Advantages
3
2. Enhanced Accuracy: Leveraging deep learning models, the system can
analyze intricate features within eye images, leading to enhanced accuracy in
disease identification. This precision minimizes the likelihood of
misdiagnosis, ensuring better patient outcomes.
3. Scalability and Affordability: The proposed system strives for scalability and
affordability by utilizing widely available hardware resources like standard
digital cameras or smartphone cameras. This approach reduces the dependency
on expensive specialized equipment, making it more accessible in various
healthcare settings.
4
● RAM : 4 GB (min)
● Hard Disk : 20 GB
● Monitor : SVGA
2. LITERATURE SURVEY
Literature survey provides insights into best practices, theoretical frameworks, and
user experiences, guiding the project team in making informed decisions and
addressing potential challenges. The survey informs technology selection,
benchmarks against existing solutions, and ensures legal and ethical considerations
are taken into account in the project development.
Mohammad Monirujjaman Khan et al., [1] the study presented in this research puts
forward a fanatical ocular detection method which is mainly based on deep learning.
In the proposed method authors picked a cutting-edge image classification algorithm
like VGG-19 and trained the model on the ODIR dataset, which contains 5000 photos.
The photos consist of eight different classes of the fundus images. Many varieties of
ocular illnesses are constituted by these classes. Nevertheless, the dataset for these
classes authors picked was very imbalanced and to solve this problem they sought to
turn this multiclass classification problem into a binary classification problem and
take the same number of images for both categories in order to solve this challenge.
The accuracy of the VGG-19 model was 98.13% for the normal (N) versus
pathological (M) myopia class, 94.03% for the normal (N) against cataract (C), and
90.94% for the normal (N) versus glaucoma classes (G).
Nouf Badha, et al., [2] study focuses on identifying eye infections of Glaucoma
disease where authors used a variety of machine learning (ML) classifiers, including
Support Vector Machine (SVM), K-Nearest Neighbors (KNN), Naive Bayes (NB),
5
Multi-layer perceptron (MLP), Decision Tree (DT), and Random Forest (RF), as well
as Deep Learning (DL) models, such as Convolutional Neural Network (CNN) based
on Resnet152 model. On the dataset for Ocular Disease Intelligent Recognition, the
proposed techniques are evaluated. The collected findings 9 demonstrated that, in
comparison to the other ML classifiers, the RF and MLP classifiers had the best
accuracy of 77%. For the identical task and dataset, the deep learning model (CNN
model: Resnet152) offers an even higher accuracy of 84%.
Hao Gu et al., [3] . This research demonstrates a design of a novel approach that is
based on hierarchical deep learning network is proposed. The network aids in
identification of corneal diseases and is composed of a family of multi-task and multi-
label learning classifiers which represents different levels of eye diseases which are
indeed derived from a predefined hierarchical eye disease taxonomy. In order to
understand the fine-grained heterogeneity of eye illness traits, authors presented a
multi-level eye disease-guided loss function. A dataset of 5,325 ocular surface
pictures, the proposed algorithm was trained directly on an end-to-end basis. In a
prospective, a dataset which is majorly clinic-based consists of 510 outpatients who
are freshly enrolled with diseases of infectious like keratitis, non-infectious keratitis,
corneal dystrophy or degeneration, and corneal neoplasm, the algorithm's
performance was lastly evaluated against 10 ophthalmologists. For each form of
corneal disease, the algorithm's area under the ROC curve was over 0.910, and
generally speaking, its sensitivity and specificity were on par with or better than the
average values of all ophthalmologists.
6
Table 2.1 Literature Survey
7
Deep learning A novel hierarchical allows The algorithm
for identifying deep learning for precise is trained
corneal diseases network - CNN is identificati exclusively to
from ocular used for the purpose on of detect the
surface slit-lamp of classifying ocular disease ocular surface
3. 2020 photographs [3]. surface images subtypes, n diseases listed
consisting of corneal ot just in the
diseases broad methods
categories section, so it
may miss eye
End-to- diseases with
End a normal
Training ocular
surface.
This Design methodology of Eye disease Detection using Machine Learning consists
of system architecture, machine learning algorithms, techniques and UML diagrams.
Figur
e 3.1 System Architecture
1. Input Eye Image: Responsible for obtaining eye images (fundus images,
retinal scans) from medical imaging devices or databases.
8
Descriptors, and other relevant methods to extract discriminative features from
the preprocessed images.
5. Output: Eye Profile which contains whether the patient’s eye is healthy or its
suffering from eye diseases such as Cataract, Glaucoma, or Conjunctivitis.
Workflow:
Step 1 Image Acquisition: Obtain fundus images, retinal scans, or other eye
- images
Significance:
The system's significance lies in its potential to contribute to early diagnosis, thereby
reducing the risk of blindness caused by these prevalent eye diseases. Leveraging
image classification and feature extraction techniques can aid in accurate disease
identification, facilitating timely interventions and treatment planning.
9
Ultimately, the proposed system holds promise in revolutionizing early disease
detection, offering a potential solution to address the pressing need for timely
interventions in the realm of eye health.
Purpose: MSE is a common metric used to measure the average squared differences
between predicted values and actual values in a regression problem.
Interpretation: A lower MSE value indicates that the model's predictions are closer
to the actual values. It penalizes larger errors more heavily due to the squaring
operation.
Applications: MSE is often used as a loss function during the training of regression
models, such as linear regression, neural networks, and other machine learning
algorithms where the objective is to minimize prediction errors.
3.2.2 Brute-force matching with SIFT descriptors and ratio test (BFSIFT)
Full forms:
10
About BFSIFT:
Advantages:
● High accuracy: Can accurately match features even under various image
transformations like rotation, scale, and illumination changes.
● Efficient for small datasets: Relatively fast for matching features in smaller
images or with smaller datasets.
Disadvantages:
11
● Not scale-invariant: Requires pre-processing of images to ensure they are at
the same scale for optimal performance.
About BFOD:
2. Brute-force matching:
12
▪ Identifies potential matches based on the Hamming
distance, which measures the number of differing bits between
the binary strings.
Advantages:
● Fast and efficient: ORB descriptors are significantly faster to compute than
SIFT, making BFOD suitable for real-time applications.
● Lightweight: Uses less memory than SIFT, making it ideal for resource-
constrained environments.
Disadvantages:
● Lower accuracy: Generally, less accurate than SIFT, especially for extreme
image transformations.
Type: KNN is a simple and intuitive machine learning algorithm used for both
classification and regression tasks.
13
Prediction Process: For a given data point, KNN identifies the K closest data points
(neighbors) from the training set based on a distance metric (commonly Euclidean
distance) in the feature space.
Classification: In classification tasks, the algorithm predicts the class of the new data
point by majority voting among its K nearest neighbors.
Regression: For regression tasks, KNN predicts the value of the new data point by
averaging the values of its K nearest neighbors.
Distance Metrics: Euclidean distance is commonly used, but other distance metrics
like Manhattan distance, Minkowski distance, or cosine similarity can be employed
based on the nature of the data and problem.
14
Purpose: FLANN is used for efficiently finding approximate nearest neighbors in
high-dimensional data. It's particularly valuable in scenarios where traditional brute-
force search methods become computationally expensive due to the data's
dimensionality.
Speed and Efficiency: FLANN is known for its speed and ability to handle large
datasets with high dimensions. It's optimized to balance between search accuracy and
computational cost.
Support for Various Data Types: FLANN supports various data types and distance
metrics, making it adaptable to different kinds of data and applications.
Interfaces: FLANN offers interfaces for different programming languages like C++,
Python, and MATLAB, making it accessible and usable across different
environments.
Extensions and Alternatives: There are several extensions and alternative libraries
inspired by FLANN that aim to provide similar functionality while improving
efficiency or scalability for specific use cases.
15
3.4.1 Class Diagram
16
3.4.2 Use Case Diagram
17
4. Testing and Results
4.1 Testing
● Unit testing is used as the user is simply using sample input and observing its
corresponding outputs.
4.2 Results
18
and password and it will lead user to home page, if not the same login page will
loaded and display the message “Wrong!!” .
4.2.2 Home Page
19
In Figure 4.2.3, after clicking ‘ Browse Images’ button it will open a new window
from where user can browse an input eye image which user want to test through Eye
Disease Detection application for any potential disease as mentioned in the abstract.
20
Figure 4.2.5.2 MSE output
In the Figure 4.2.5.1, 4.2.5.2, which shows us that the MSE method is used to here to
test the input eye image and the results of the MSE method after its successful testing
of the input eye image which displays that our given input eye image is a healthy eye.
Here, I used my eyes image for Eye Disease Detection application.
21
Figure 4.2.6.2 BFOD Output
In Figure 4.2.6.1 and 4.2.6.2, it illustrates that the BFOD method is applied on the
input eye image and after successfully testing the eye image the results are displayed
through the interface. The predicted result is not a accurate one .
4.2.7 Using Brute Force Matching with SIFT Descriptors and Ratio Test
(BFSIFT) method
22
Figure 4.2.7.2 BFSIFT Output
In Figure 4.2.7.1 and 4.2.7.2, it illustrates that the BFSIFT method is applied on the
given input eye image. After successful testing of the eye image, it displayed the
results which are accurate and correct. The given input eye image is a healthy eye.
4.2.8 Using Fast Library for Approximate Nearest Neighbors (FLANN) method
23
Figure 4.2.8.2 FLANN output
In the Figure 4.2.8.1 and 4.2.8.2, it illustrates that the FLANN method is used to test
the given input eye image. After successful testing of the eye image, it displayed the
result. This method in user profile eye disease detection, it shows amount of matches
and similarity percentage.
24
5. CONCLUSION AND FUTURE SCOPE
5.1 Conclusion
The proposed system shows the development of an automated system for early
detection of cataract, glaucoma, and conjunctivitis using machine learning
techniques , which marks a significant stride in ocular health. Leveraging image
analysis algorithms, this system showcases promising accuracy in diagnosing these
prevalent eye diseases. Its potential to offer non-invasive, efficient, and accurate
diagnosis holds promise in reducing vision impairment and blindness risks.
Integrating technology into healthcare, this system has the potential to transform early
disease detection and healthcare decision-making. Further refinement and integration
into clinical settings can significantly impact global eye health by mitigating the
burden of these conditions.
Future Scope
Looking ahead, the future scope for eye disease detection using machine learning
presents several promising avenues for advancement. Enhanced diagnostic
capabilities can be achieved through the exploration of advanced algorithms like deep
learning architectures and the fusion of multimodal imaging data. Personalized
medicine approaches can revolutionize patient care by tailoring machine learning
models to individual patient characteristics and enabling longitudinal analysis for
proactive interventions. Telemedicine and remote monitoring applications can extend
the reach of eye care services, especially in underserved areas, while mobile
applications equipped with AI-powered detection capabilities can empower users for
self-assessment and early intervention. Collaboration with the healthcare ecosystem,
including integration with electronic health records and participation in clinical trials,
can validate the performance of ML models in real-world settings and contribute to
ongoing research in ophthalmology. Ensuring regulatory compliance, adherence to
ethical guidelines, and education and awareness initiatives can facilitate responsible
deployment and widespread adoption of AI-powered solutions, ultimately improving
patient outcomes and transforming the landscape of eye healthcare.
25
BIBLIOGRAPHY
[1]. M. S. Khan, Nafisa Tafshir, Kazi Nabiul Alam, Abdur Rab Dhruba, “Deep
learning for ocular disease recognition: An inner-class balance”,
Computational Intelligence and Neuroscience, issue no. 12, vol. 2022, 2022.
[3]. Hao Gu, Youwen Guo, Lei Gu, Anji Wei, “Deep learning for identifying
corneal diseases from ocular surface slit-lamp photographs”, Nature Scientific
reports, vol. 10, article no. 17851, pp. 1-10, 2020.
[5]. Rubina Sarki, Khandakar Ahmed, Hua Wang, Yanchun Zhang, “Automatic
Detection of Diabetic Eye Disease Through Deep learning using Fundus
Images: A Survey”, IEEE Access, vol. 8, pp. 151133-151149, 2020.
[6]. Xiaomeng Li, Xiaowei Hu, lequan Yu, Lei Zhu, “CANet: Cross-Disease
Attention Network for Joint Diabetic Retinopathy and Diabetic Macular
Edema Grading”, IEEE Access, issue no. 5, vol. 39, pp. 1483-1493, 2020.
[7]. D Shamia, Shajin Prince, D Bini, “An Online Platform for Early Eye Disease
Detection using Deep Convolutional Neural Network”, IEEE 6th International
Conference on Devices, Circuits and Systems(ICDCS), pp. 388-392, 2022.
[8]. https://www.smartdraw.com/uml-diagram/uml-diagram-tool.html
26
[9]. https://www.w3schools.com/python.
APPENDIX
1. IRIS.py
from skimage.metrics import structural_similarity as ssim
import matplotlib.pyplot as plt
import cv2
import urllib.request
import urllib.parse
import urllib.error
import tkinter as tk
from tkinter import font as tkfont
from tkinter import *
import os
from tkinter import filedialog
import sqlite3
import numpy as np
import shutil
from glob import glob
import random
class SampleApp(tk.Tk):
27
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.geometry("700x500+600+200") # Width x Height + Position Right +
Position Left
self.title('IRIS - Interface')
self.frames = {}
for F in (LoginPage, Home, Methods):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
self.show_frame("LoginPage")
class LoginPage(tk.Frame):
28
label.pack(side="top", fill="x", pady=10)
# username entry
username_entry = Entry(self)
username_entry.pack()
# password entry
password_entry = Entry(self, show='*')
password_entry.pack()
def trylogin():
# check if both username and password in the entries are same of the given
ones
if username == username_entry.get() and password == password_entry.get():
controller.show_frame("Home")
else:
print("Wrong")
class Home(tk.Frame):
29
url1 = tk.StringVar()
def urlimages():
firstimage = url1.get()
resource = urllib.request.urlopen(firstimage)
print(resource)
output = open("1.png", "wb")
output.write(resource.read())
output.close()
def fileDialog():
try:
delete = "del 1.png"
os.system(delete)
file = filedialog.askopenfilename(initialdir=os.getcwd(), title='Choose a
file', filetype=(("png", "*.png"), ("jpeg", "*.jpg"), ('All Files', "*.*")))
filedir = r"%s" % file
shutil.move(filedir, os.getcwd())
filename = glob('*.png')[0]
print(filename)
os.rename(file, "1.png")
except:
delete = "del 1.png"
os.system(delete)
30
print("Renaming already existing png file")
filename = glob('*.png')[0]
os.rename(filename, "1.png")
class Methods(tk.Frame):
31
percent = random.randint(85, 94)
def MSE():
connectdb = sqlite3.connect("results.db")
cursor = connectdb.cursor()
img1 = cv2.imread("1.png")
img11 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
imageA = cv2.resize(img11, (450, 237))
database = os.listdir("db")
m = mse(imageA, imageB)
32
title = "Comparing"
fig = plt.figure(title)
smallest = min(percentages)
print(smallest)
minperct = round(smallest[0], 2)
print(minperct)
print(path)
img3 = cv2.imread(path)
33
# show the second image
ax = fig.add_subplot(1, 2, 2)
plt.imshow(imageC, cmap=plt.cm.gray)
disease = path[3:-4]
txt = "Results: \n - " + path + "\n - " + disease
plt.text(0.40, 0.25, txt, transform=fig.transFigure, size=11)
plt.axis("off")
# show the images
plt.show()
def BFOD():
connectdb = sqlite3.connect("results.db")
cursor = connectdb.cursor()
img1 = cv2.imread("1.png")
img11 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
imageA = cv2.resize(img11, (450, 237))
database = os.listdir("db")
matcheslist = ""
34
# Initiate ORB detector
orb = cv2.ORB_create()
# find the keypoints and descriptors with ORB
kp1, des1 = orb.detectAndCompute(imageA, None)
kp2, des2 = orb.detectAndCompute(imageB, None)
title = "Comparing"
fig = plt.figure(title)
highest = percentages[0]
highestperct = round(highest[0], 2)
print(highestperct)
35
path = os.path.join(root, highest[1])
print(path)
img3 = cv2.imread(path)
img3process = cv2.cvtColor(img3, cv2.COLOR_BGR2GRAY)
imageC = cv2.resize(img3process, (450, 237))
def BFSIFT():
connectdb = sqlite3.connect("results.db")
cursor = connectdb.cursor()
# img1 = cv2.imread("1.png")
# img11 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
36
img1 = cv2.imread('1.png', cv2.IMREAD_GRAYSCALE)
imageA = cv2.resize(img1, (450, 237))
database = os.listdir("db")
matcheslist = ""
amount = len(good)
print('Comparing input image to ' + image + " using BFSIFT")
37
title = "Comparing"
fig = plt.figure(title)
38
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1, des2, k=2)
def FLANN():
connectdb = sqlite3.connect("results.db")
cursor = connectdb.cursor()
# img1 = cv2.imread("1.png")
# img11 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
39
img1 = cv2.imread('1.png', cv2.IMREAD_GRAYSCALE)
imageA = cv2.resize(img1, (450, 237))
database = os.listdir("db")
matcheslist = ""
amount = len(good)
print('Comparing input image to ' + image + " using FLANN")
40
title = "Comparing"
fig = plt.figure(title)
except:
pass
41
# find the keypoints and descriptors with SIFT
keypoints1, destination1 = sift.detectAndCompute(img1, None)
keypoints2, destination2 = sift.detectAndCompute(img2, None)
# FLANN parameters
FLANN_INDEX_KDTREE = 1
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks=50) # or pass empty dictionary
flann = cv2.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(destination1, destination2, k=2)
print(draw_params)
print(len(matches))
42
txt = "Results: \n - " + filename + "\n - " + disease #+ "\n - Analysis results are
safe, no diseases found"#
plt.text(0.40, 0.20, txt, transform=fig.transFigure, size=11)
plt.axis("off")
plt.show()
def goback():
controller.show_frame("Home")
43
label4.pack()
if __name__ == "__main__":
try:
remove = "del 1.png"
os.system(remove)
app = SampleApp()
app.mainloop()
finally:
remove = "del 1.png"
os.system(remove)
2. editingphotos.py
import os
from matplotlib import gridspec
import cv2
import read_dataset as data
import hough_circles as hough
import visualization as vis
44
root = "test"
os.path.exists(root)
images = data.read_dataset(root, "png")
print(len(images))
vis.plot_images_grid(images[:30], 5, 6)
3. dataset.py
import os
import cv2
class Dataset:
"""
Representing dataset of images
"""
45
self.images_names = []
for file in os.listdir(root):
title = file.title().lower()
if title.split('.')[-1] == img_format:
self.images_names.append(title)
self.images.append(cv2.imread(os.path.join(root, title)))
4. filtering.py
import cv2
import numpy as np
def bottom_hat_median_blurr(image):
"""
Bottom hat filtering and smoothing with median filter
:param image: image
:return: filtered image
"""
cimg = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
blackhat = cv2.morphologyEx(cimg, cv2.MORPH_BLACKHAT, kernel)
bottom_hat_filtered = cv2.add(blackhat, cimg)
return cv2.medianBlur(bottom_hat_filtered, 17)
5. hough_circles.py
import cv2
import numpy as np
import filtering as filt
def detect_inner_circle(img, canny_param=20, hough_param=20):
46
"""
Detecting inner iris circle after filtering
:param img: image
:param canny_param: higher threshold for canny edge detector
:param hough_param: threshold parameter for Hough circle transform
:return:
"""
filtered = filt.bottom_hat_median_blurr(img)
adjusted = filt.adjust_gamma(filtered, 10)
circles = cv2.HoughCircles(adjusted, cv2.HOUGH_GRADIENT, 1, 20,
param1=canny_param,
param2=hough_param,
minRadius=0)
inner_circle = [0, 0, 0]
if circles is not None:
inner_circle = np.uint16(np.around(circles[0][0])).tolist()
return inner_circle
6. Algorithms
6.1 bruteforce matching with sift decriptors and ratio test.py
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
img1 = cv.imread('canada.png', cv.IMREAD_GRAYSCALE) # queryImage
img2 = cv.imread('canada.png', cv.IMREAD_GRAYSCALE) # trainImage
# Initiate SIFT detector
sift = cv.xfeatures2d.SIFT_create()
47
# Apply ratio test
good = []
for m, n in matches:
if m.distance < 0.75*n.distance:
good.append([m])
6.2 EMD.py
def get_histogram(img):
'''
Get the histogram of an image. For an 8-bit, grayscale image, the
histogram will be a 256 unit vector in which the nth value indicates
the percent of the pixels in the image with the given darkness level.
The histogram's values sum to 1.
'''
h, w = img.shape
hist = [0.0] * 256
for i in range(h):
for j in range(w):
hist[img[i, j]] += 1
return np.array(hist) / (h * w)
a = imread('a.jpg')
b = imread('b.jpg')
a_hist = get_histogram(a)
b_hist = get_histogram(b)
dist = wasserstein_distance(a_hist, b_hist)
print(dist)
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
48
img2 = cv.imread('canada.png', cv.IMREAD_GRAYSCALE) # trainImage
# Initiate SIFT detector
sift = cv.xfeatures2d.SIFT_create()
# FLANN parameters
FLANN_INDEX_KDTREE = 1
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks=50) # or pass empty dictionary
flann = cv.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(des1, des2, k=2)
print(draw_params)
print(len(matches))
49