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

Switch to unified view

a b/WaterShed.py
1
#%%     #this line is for VSCode editor only
2
import cv2
3
#from skimage.measure import compare_ssim
4
import argparse
5
#import imutils
6
import numpy as np
7
import matplotlib.pyplot as plt
8
%matplotlib inline
9
10
img = cv2.imread(r'C:\\Users\\ANSHUL KIYAWAT\\Desktop\\image2.jpg',0)
11
12
ret, thresh = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
13
14
plt.subplot(121),plt.imshow(thresh,'gray')
15
plt.title('Thresh_Binary_INV+cv2.Thresh_OTSU')
16
plt.xticks([]),plt.yticks([])
17
18
# noise removal
19
kernel = np.ones((3,3),np.uint8)
20
opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2)
21
22
# sure background area
23
sure_bg = cv2.dilate(opening,kernel,iterations=3)
24
25
# Finding sure foreground area
26
dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5)
27
ret, sure_fg = cv2.threshold(dist_transform,0.1*dist_transform.max(),255,0)
28
29
# Finding unknown region
30
sure_fg = np.uint8(sure_fg)
31
unknown = cv2.subtract(sure_bg,sure_fg)
32
33
plt.subplot(121),plt.imshow(sure_fg,'gray')
34
plt.title('Sure')
35
plt.xticks([]),plt.yticks([])
36
37
plt.subplot(122),plt.imshow(unknown,'gray')
38
plt.title('Unknown')
39
plt.xticks([]),plt.yticks([])
40
41
# Marker labelling
42
ret, markers = cv2.connectedComponents(sure_fg)
43
44
# Add one to all labels so that sure background is not 0, but 1
45
markers = markers+1
46
47
# Now, mark the region of unknown with zero
48
markers[unknown==255] = 0
49
50
51
plt.subplot(121),plt.imshow(markers,'gray')
52
plt.title('Marked')
53
plt.xticks([]),plt.yticks([])
54
55
img = cv2.imread(r'C:\\Users\\ANSHUL KIYAWAT\\Desktop\\image2.jpg',1)
56
img = cv2.medianBlur(img,5)
57
markers = cv2.watershed(img,markers)
58
img[markers == -1] = [255,0,0]
59
60
plt.imshow(img,'gray')
61
plt.title('Marked')
62
plt.xticks([]),plt.yticks([])