使用OpenCV Python進行黑白點檢測
OpenCV Python是Python中的一種影像處理庫,它使用NumPy陣列儲存影像資料,因此所有影像陣列都表示為ndarray型別。
Python OpenCV模組中的cv2.findContours()方法用於檢測二值影像中的物件。在下面的文章中,我們將使用此方法來檢測影像中的黑白點(物件)。以下是此方法的語法:
cv2.findContours(image, mode, method[, contours[, hierarchy[, offset]]])
其中:
影像是8位單通道影像(二值影像)。
輪廓是檢測到的輪廓。
方法
我們將遵循以下步驟,使用OpenCV模組檢測/計數影像中的黑點和白點。
載入影像。
將影像轉換為灰度。
應用中值模糊以平滑影像。
定義閾值。
查詢輪廓
遍歷輪廓並使用輪廓面積進行過濾
使用cv2.findContours()方法
除了cv2.findContours()方法外,我們還將使用以下方法:
cv2.medianBlur(): 平滑輸入影像。
cv2.cvtColor(): 將彩色影像轉換為灰度影像。
cv2.threshold(): 定義閾值。
cv2.contourArea(): 基於面積過濾輪廓(點)
示例
讓我們以輸入影像“WhiteDots2.jpg”為例,檢測暗背景影像上的白點。
import cv2
image = cv2.imread('Images/WhiteDots2.jpg')
blur = cv2.medianBlur(image, 5)
gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray,200,255, cv2.THRESH_BINARY)[1]
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
min_area = 0.1
white_dots = []
for c in cnts:
area = cv2.contourArea(c)
if area > min_area:
cv2.drawContours(image, [c], -1, (36, 255, 12), 2)
white_dots.append(c)
print("White Dots count is:",len(white_dots))
cv2.imshow('image', image)
cv2.waitKey()
輸出
White Dots count is: 11
輸入影像

輸出影像

在上面的示例中,我們成功檢測到輸入影像上的16個白點,所有白點都以綠色突出顯示。
示例
在這個例子中,我們將使用輸入影像“black-doted-butterflies.jpg”來檢測白色背景影像上的黑點。
import cv2
img = cv2.imread('Images/black-doted-butterflies.jpg')
blur = cv2.medianBlur(img, 5)
gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray,100,255, cv2.THRESH_BINARY_INV)[1]
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
min_area = 10
black_dots = []
for c in cnts:
area = cv2.contourArea(c)
if area > min_area:
cv2.drawContours(img, [c], -1, (36, 255, 12), 2)
black_dots.append(c)
print("Black Dots Count is:",len(black_dots))
cv2.imshow('Output image', img)
cv2.waitKey()
輸出
Black Dots Count is: 25
輸入影像

輸出影像

透過使用cv2.findContours()方法,我們成功地檢測到了蝴蝶影像上的25個黑點。檢測到的黑點以綠色突出顯示。
示例
在這個例子中,我們將使用輸入影像“BlackAndWhite.jpg”來檢測單個影像中的黑點和白點。
import cv2
img = cv2.imread('Images/BlackAndWhite.jpg')
blur = cv2.medianBlur(img, 5)
gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)
thresh_for_black_dots = cv2.threshold(gray,100,255, cv2.THRESH_BINARY_INV)[1]
thresh_for_white_dots = cv2.threshold(gray,200,255, cv2.THRESH_BINARY)[1]
cnts_for_black_dots = cv2.findContours(thresh_for_black_dots, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts_for_white_dots = cv2.findContours(thresh_for_white_dots, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts_for_black_dots = cnts_for_black_dots[0] if len(cnts_for_black_dots) == 2 else cnts_for_black_dots[1]
cnts_for_white_dots = cnts_for_white_dots[0] if len(cnts_for_white_dots) == 2 else cnts_for_white_dots[1]
min_area = 1
black_dots = []
white_dots = []
for c in cnts_for_white_dots:
area = cv2.contourArea(c)
if area > min_area:
cv2.drawContours(img, [c], -1, (36, 255, 12), 2)
black_dots.append(c)
white_dots.append(c)
for c in cnts_for_black_dots:
area = cv2.contourArea(c)
if area > min_area:
cv2.drawContours(img, [c], -1, (36, 255, 12), 2)
black_dots.append(c)
print("Black Dots Count is:",len(black_dots))
print("White Dots count is:",len(white_dots))
cv2.imshow('Output image:', img)
cv2.waitKey()
輸出
Black Dots Count is: 249 White Dots count is: 134
我們已經成功地使用Python OpenCV庫透過不同的方法檢測到了黑點和白點。
輸入影像

輸出影像

使用SimpleBlobDetector
在OpenCV中,SimpleBlobDetector是一個用於從影像中提取斑點的類。cv2.SimpleBlobDetector_create()方法建立一個檢測器,根據SimpleBlobDetector演算法檢測給定影像上的斑點。
示例
在這個例子中,我們將使用cv2.SimpleBlobDetector_create()方法來檢測白色背景影像上的黑點。
import cv2
import numpy as np;
im = cv2.imread("Images/BlackDots.jpg", cv2.IMREAD_GRAYSCALE)
# create the detector with default parameters.
detector = cv2.SimpleBlobDetector_create()
# Detect dots.
keypoints = detector.detect(im)
print("Black Dots Count is:",len(keypoints))
# Draw detected blobs as red circles.
im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]), (0,0,250), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imshow("Output image:", im_with_keypoints)
cv2.waitKey(0)
輸出
Black Dots Count is: 18
在上面的程式碼中,cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS確保圓的大小與斑點的大小相對應。在輸出中,我們可以看到給定影像中共有49個黑點。
輸入影像

輸出影像

資料結構
網路
關係資料庫管理系統(RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP