-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindingIndependentObjectCount.py
35 lines (28 loc) · 1.1 KB
/
findingIndependentObjectCount.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import cv2
from skimage import measure
from skimage import filters
import matplotlib.pyplot as plt
import numpy as np
n = 12
l = 256
im = np.zeros((l, l))
np.random.seed(1) # random da aynı sonuç çıkması için
points = l * np.random.random((2, n ** 2)) # [2,144] bir dizi oluşturur random sayılardan ve 256 ile çarpar (0 - 256 arası degerler)
im[(points[0]).astype(int), (points[1]).astype(int)] = 1 # denk gelen koordinatlara 1 girer
im = filters.gaussian(im, sigma= l / (4. * n)) # yumşatma yaparak 1 i etrafa yayar yuvarlak oluşturur
blobs = im > 0.7 * im.mean() # matrisi True False matrisine dönüştürür. im.mean() matrisin ortalamasıdır.
all_labels = measure.label(blobs) # True-False matrislerini numaralandırarak sayar
blobs_labels = measure.label(blobs, background=0)
print(np.max(blobs_labels)) # object Count
plt.figure(figsize=(9,3.5))
plt.subplot(131)
plt.imshow(blobs, cmap='gray')
plt.axis('off')
plt.subplot(132)
plt.imshow(all_labels, cmap='nipy_spectral')
plt.axis('off')
plt.subplot(133)
plt.imshow(blobs_labels, cmap='nipy_spectral')
plt.axis('off')
plt.tight_layout()
plt.show()