Mendeteksi Blob Menggunakan OpenCV

Blob adalah sebuah group dari pixel (dalam sebuah image) yang saling terhubung dengan property yang sama.

Blob detection cukup mudah dilakukan, berikut contoh code untuk mendeteksi disc floret (lingkaran tengah tempat pollen) bunga matahari.

import cv2
import numpy as np
import matplotlib.pyplot as plt

%matplotlib inline

img = cv2.imread('Sunflowers.jpg', cv2.IMREAD_GRAYSCALE)

detector = cv2.SimpleBlobDetector_create()

keypoints = detector.detect(img)

blank = np.zeros((1,1))
blobs = cv2.drawKeypoints(img, keypoints, blank, (255,0,0), cv2.DRAW_MATCHES_FLAGS_DEFAULT)

plt.imshow(blobs)

Params

Kita bisa mengatur parameter dari SimpleBlobDetector untuk menentukan property blob yang akan dideteksi. Untuk mengatur parameter digunakan fungsi cv2.SimpleBlobDetector_Params()

Untuk jelasnya, berikut program sederhana untuk mendeteksi blob berbentuk lingkaran.

img = cv2.imread('blobs.jpg', 0)


#deteksi blob tanpa parameter (sama seperti code sebelumnya)
detector = cv2.SimpleBlobDetector_create()
keypoints = detector.detect(img)

blank = np.zeros((1,1))
blobs = cv2.drawKeypoints(img, keypoints, blank, (255,0,0), cv2.DRAW_MATCHES_FLAGS_DEFAULT)

blobs_count = len(keypoints)
txt = 'jumlah blobs =' + str(blobs_count)
cv2.putText(blobs, txt, (20, 550), cv2.FONT_HERSHEY_SIMPLEX, 1, (100, 0, 0), 2)

plt.imshow(blobs)
#deteksi blob menggunakan parameter

params = cv2.SimpleBlobDetector_Params()

params.filterByArea = True
params.minArea = 100

params.filterByCircularity = True
params.minCircularity = 0.9

#params.filterByConvexity = False
#params.minConvexity = 0.2

params.filterByInertia = True
params.minInertiaRatio = 0.01

detector = cv2.SimpleBlobDetector_create(params)

keypoints = detector.detect(img)

blank = np.zeros((1,1))
blobs = cv2.drawKeypoints(img, keypoints, blank, (255,255,255), cv2.DRAW_MATCHES_FLAGS_DEFAULT)

blobs_count = len(keypoints)
txt = 'jumlah blobs lingkaran =' + str(blobs_count)
cv2.putText(blobs, txt, (20, 550), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)

plt.imshow(blobs)

Param Area untuk mengukur berdasarkan besar area.

params.filterByArea = True
params.minArea = 100

Param Circularity untuk mengukur apakah sebuah blob mendekati lingkaran. Contoh regular hexagon memiliki nilai circularity lebih tinggi dibanding kotak. Berikut contoh set parameter circularity:

params.filterByCircularity = True
params.minCircularity = 0.9

Param convexity lebih jelas dengan melihat gambar dibawah. C

params.filterByConvexity = False
params.minConvexity = 0.2

Param Inertia, makin rendah makin elipse, makin tinggi makin mendekati lingkaran. Contoh. lingkaran memiliki value 1, 0 >= ellipse <= 1, garis adalah 0.

params.filterByInertia = True
params.minInertiaRatio = 0.01

Image sunflower https://drive.google.com/file/d/1h6D2sDb7Zew_7dk_LjB_mjc2hoh0VxOf/view?usp=sharing

Image blobs https://drive.google.com/file/d/1yg7aXo5EQFz8ZID0pGOodyQ0oMKneDfD/view?usp=sharing

File google colab : https://colab.research.google.com/drive/1dWX2Dfp4h5n0marEral_hXeqkiC1jGkB?usp=sharing

Sharing is caring:

Leave a Comment