Diff of /Finalcode.py [000000] .. [b20d48]

Switch to unified view

a b/Finalcode.py
1
import cv2
2
import argparse
3
import numpy as np
4
import matplotlib.pyplot as plt
5
%matplotlib inline
6
7
s = r'C:\Users\Arnab Sinha\Documents\GitHub\Kidney-Stone-Detection-IP\images'
8
image_no = '\image1.jpg'
9
s = s + image_no
10
11
img = cv2.imread(s,0)
12
13
def build_filters():
14
    #returns a list of kernels in several orientations
15
    filters = []
16
    ksize = 31
17
    for theta in np.arange(0, np.pi, np.pi / 32):
18
        params = {'ksize': (ksize, ksize), 'sigma': 0.0225, 'theta': theta, 'lambd': 15.0,
19
                  'gamma': 0.01, 'psi': 0, 'ktype': cv2.CV_32F}
20
        
21
        kern = cv2.getGaborKernel(**params)
22
        kern /= 1.5*kern.sum()
23
        filters.append((kern, params))
24
    return filters
25
26
27
def process(img, filters):
28
    #returns the img filtered by the filter list
29
    accum = np.zeros_like(img)
30
    for kern, params in filters:
31
        fimg = cv2.filter2D(img, cv2.CV_8UC3, kern)
32
        np.maximum(accum, fimg, accum)
33
    return accum
34
35
def Histeq(img):
36
    equ = cv2.equalizeHist(img)
37
    return equ
38
39
def GaborFilter(img):
40
    filters = build_filters()
41
    p = process(img, filters)
42
    return p
43
44
def Laplacian(img,par):  
45
    lap = cv2.Laplacian(img,cv2.CV_64F)
46
    sharp = img - par*lap
47
    sharp = np.uint8(cv2.normalize(sharp, None, 0 , 255, cv2.NORM_MINMAX))
48
    return sharp
49
50
def Watershed(img):
51
    ret, thresh = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
52
53
    # noise removal
54
    kernel = np.ones((3,3),np.uint8)
55
    opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2)
56
57
    # sure background area
58
    sure_bg = cv2.dilate(opening,kernel,iterations=3)
59
60
    # Finding sure foreground area
61
    dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5)
62
    ret, sure_fg = cv2.threshold(dist_transform,0.23*dist_transform.max(),255,0)
63
64
    # Finding unknown region
65
    sure_fg = np.uint8(sure_fg)
66
    unknown = cv2.subtract(sure_bg,sure_fg)
67
    
68
    # Marker labelling
69
    ret, markers = cv2.connectedComponents(sure_fg)
70
71
    # Add one to all labels so that sure background is not 0, but 1
72
    markers = markers+1
73
74
    # Now, mark the region of unknown with zero
75
    markers[unknown==255] = 0
76
77
    img2 = cv2.imread(s,1)
78
    img2 = cv2.medianBlur(img2,5)
79
    markers = cv2.watershed(img2,markers)
80
    img2[markers == -1] = [255,0,0]
81
82
    return img2
83
84
if image_no=='\image1.jpg':
85
    img3 = Laplacian(img,0.239)
86
    
87
elif image_no=='\image2.jpg':
88
    img3 = GaborFilter(img)
89
    img3 = Histeq(img3)
90
91
elif image_no=='\image4.jpg':
92
    img3 = GaborFilter(img)
93
94
img3 = Watershed(img)
95
96
plt.imshow(img3,'gray')
97
plt.title('Marked')
98
plt.xticks([]),plt.yticks([])