Skip to content

Commit 1f5c442

Browse files
committed
blob detection with dfs
1 parent 08b7bdd commit 1f5c442

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

Blob Detection/BlobDetection.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import cv2
2+
from tkinter.filedialog import *
3+
4+
5+
# we will find number of blobs with pixel value 255 in the following image
6+
7+
# finding binary image
8+
print("\nImage should preferably be white (lighter) blobs on black (darker) background ")
9+
photo = askopenfilename()
10+
img = cv2.imread(photo,cv2.IMREAD_GRAYSCALE)
11+
img = cv2.resize(img,(300,300))
12+
n,l = img.shape
13+
count = 0
14+
15+
# blur the image
16+
ksize = (5,5) # kernel size
17+
img = cv2.blur(img,ksize)
18+
19+
# thresholding the image
20+
for i in range(n):
21+
for j in range(l):
22+
if(img[i,j]<=127):
23+
img[i,j]=0
24+
else:
25+
img[i,j]=255
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+
54+
cv2.namedWindow('image',cv2.WINDOW_NORMAL)
55+
cv2.imshow("image",img)
56+
cv2.waitKey(1000)
57+
58+
59+
for i in range(n):
60+
for j in range(l):
61+
if(img[i,j]==255):
62+
count += 1 #to count number of white blobs
63+
dfs(i,j)
64+
65+
print("count is",count)

Blob Detection/ReadMe.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Blob detection
2+
This python script finds blobs in an image using DFS algorithm.
3+
4+
## Setup Instructions
5+
### Install python3
6+
sudo apt-get install python3
7+
### Install pip (package installer for python)
8+
sudo apt-get install python3-pip
9+
### Install OpenCV library with pip
10+
pip3 install opencv-python
11+
### Install tkinter library
12+
sudo apt-get install python3-tk
13+
14+
## Details/Output
15+
The program asks user to select an image to count the number of blobs.
16+
The image should preferably have **lighter blobs on a darker background**.
17+
Eg. white blobs on a black background
18+
The output is the count of blobs in the image.
19+
20+
## Author
21+
Github: invigorzz313

Blob Detection/blobImg.png

15.1 KB
Loading

0 commit comments

Comments
 (0)