|
1 |
| -import cv2 |
| 1 | +import cv2 |
2 | 2 | from tkinter.filedialog import *
|
3 | 3 |
|
4 | 4 |
|
|
7 | 7 | # finding binary image
|
8 | 8 | print("\nImage should preferably be white (lighter) blobs on black (darker) background ")
|
9 | 9 | photo = askopenfilename()
|
10 |
| -img = cv2.imread(photo,cv2.IMREAD_GRAYSCALE) |
11 |
| -img = cv2.resize(img,(300,300)) |
12 |
| -n,l = img.shape |
| 10 | +img = cv2.imread(photo, cv2.IMREAD_GRAYSCALE) |
| 11 | +img = cv2.resize(img, (300, 300)) |
| 12 | +n, l = img.shape |
13 | 13 | count = 0
|
14 | 14 |
|
15 | 15 | # blur the image
|
16 |
| -ksize = (5,5) # kernel size |
17 |
| -img = cv2.blur(img,ksize) |
| 16 | +ksize = (5, 5) # kernel size |
| 17 | +img = cv2.blur(img, ksize) |
18 | 18 |
|
19 | 19 | # thresholding the image
|
20 | 20 | for i in range(n):
|
21 | 21 | for j in range(l):
|
22 |
| - if(img[i,j]<=127): |
23 |
| - img[i,j]=0 |
| 22 | + if (img[i, j] <= 127): |
| 23 | + img[i, j] = 0 |
24 | 24 | else:
|
25 |
| - img[i,j]=255 |
| 25 | + img[i, j] = 255 |
26 | 26 |
|
27 |
| -def dfs(i,j): |
28 |
| - img[i,j]=127 # implying that we have visited this pixel for further reference |
29 |
| - if (i-1>=0): |
30 |
| - if(img[i-1,j]==255): |
31 |
| - dfs(i-1,j) |
32 |
| - if (j-1>=0): |
33 |
| - if(img[i,j-1]==255): |
34 |
| - dfs(i,j-1) |
35 |
| - if (j+1<l): |
36 |
| - if(img[i,j+1]==255): |
37 |
| - dfs(i,j+1) |
38 |
| - if (i+1<n): |
39 |
| - if(img[i+1,j]==255): |
40 |
| - dfs(i+1,j) |
41 |
| - if((i-1>=0) and (j-1>=0)): |
42 |
| - if(img[i-1,j-1]==255): |
43 |
| - dfs(i-1,j-1) |
44 |
| - if((i-1>=0) and (j+1<l)): |
45 |
| - if(img[i-1,j+1]==255): |
46 |
| - dfs(i-1,j+1) |
47 |
| - if((i+1<n) and (j-1>=0)): |
48 |
| - if(img[i+1,j-1]==255): |
49 |
| - dfs(i+1,j-1) |
50 |
| - if((i+1<n) and (j+1<l)): |
51 |
| - if(img[i+1,j+1]==255): |
52 |
| - dfs(i+1,j+1) |
53 | 27 |
|
54 |
| -cv2.namedWindow('image',cv2.WINDOW_NORMAL) |
55 |
| -cv2.imshow("image",img) |
| 28 | +def dfs(i, j): |
| 29 | + img[i, j] = 127 # implying that we have visited this pixel for further reference |
| 30 | + if (i-1 >= 0): |
| 31 | + if (img[i-1, j] == 255): |
| 32 | + dfs(i-1, j) |
| 33 | + if (j-1 >= 0): |
| 34 | + if (img[i, j-1] == 255): |
| 35 | + dfs(i, j-1) |
| 36 | + if (j+1 < l): |
| 37 | + if (img[i, j+1] == 255): |
| 38 | + dfs(i, j+1) |
| 39 | + if (i+1 < n): |
| 40 | + if (img[i+1, j] == 255): |
| 41 | + dfs(i+1, j) |
| 42 | + if ((i-1 >= 0) and (j-1 >= 0)): |
| 43 | + if (img[i-1, j-1] == 255): |
| 44 | + dfs(i-1, j-1) |
| 45 | + if ((i-1 >= 0) and (j+1 < l)): |
| 46 | + if (img[i-1, j+1] == 255): |
| 47 | + dfs(i-1, j+1) |
| 48 | + if ((i+1 < n) and (j-1 >= 0)): |
| 49 | + if (img[i+1, j-1] == 255): |
| 50 | + dfs(i+1, j-1) |
| 51 | + if ((i+1 < n) and (j+1 < l)): |
| 52 | + if (img[i+1, j+1] == 255): |
| 53 | + dfs(i+1, j+1) |
| 54 | + |
| 55 | + |
| 56 | +cv2.namedWindow('image', cv2.WINDOW_NORMAL) |
| 57 | +cv2.imshow("image", img) |
56 | 58 | cv2.waitKey(1000)
|
57 | 59 |
|
58 | 60 |
|
59 | 61 | for i in range(n):
|
60 | 62 | for j in range(l):
|
61 |
| - if(img[i,j]==255): |
62 |
| - count += 1 #to count number of white blobs |
63 |
| - dfs(i,j) |
| 63 | + if (img[i, j] == 255): |
| 64 | + count += 1 # to count number of white blobs |
| 65 | + dfs(i, j) |
64 | 66 |
|
65 |
| -print("count is",count) |
| 67 | +print("count is", count) |
0 commit comments