使用OpenCV Python检测白色和黑色点
OpenCV python是Python中的图像处理库之一,它使用Numpy数组来存储图像数据,所以所有的图像数组都被表示为ndarray类型。
cv2.findContours() 方法是Python OpenCV模块用于识别二进制图像中的对象。在下面的文章中,我们将使用这个方法来检测图像中的白色和黑色点(对象)。以下是该方法的语法:
cv2.findContours(image, mode, method[, contours[, hierarchy[, offset]]])
在图像中:
- image 是一个8位的单通道图像(二进制图像)。
-
contours 是被检测到的轮廓。
方法
我们将按照以下步骤使用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。