Python 下opencv 应用: 物体数数

物体数数,物体个数计算,就是计算一个图形中有多少物体。

python 下用opencv 打开一个图形文件,图形处理,计算图中物体的个数。

除了opencv 要安装好外,还要装好‘imutils。imutils 的下载和安装在 Python 下应用opencv 的简单功能演示 一文中有介绍。

这个原始代码来自 https://www.pyimagesearch.com/2016/11/21/raspbian-opencv-pre-configured-and-pre-installed/ 的一个教学讲稿。

代码讲解

先看开始的代码,使用方法:python count_shapes.py --image images/shapes.png

后面images/shapes.png是需要处理的图形文件名字,可以在命令行改变,看不同的图。

输入必要的程序包,argparse,imutils, cv2

然后是命令输入行参数分析,得到需要处理图形文件名,缺省是images/shapes.png

# USAGE
# python count_shapes.py --image images/shapes.png

# import the necessary packages
import argparse
import imutils
import cv2

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", default='images/shapes.png',
	help="path to the input image")
args = vars(ap.parse_args())

输入图形文件到image , 核查文件名 print args["image"],这是可以在命令行输入的文件名。

彩色图变灰色,显示原图和灰色图。

Python 下opencv 应用: 物体数数_第1张图片

# load the input image and convert it to grayscale
print(args["image"])
image = cv2.imread(args["image"])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow('image',image)
cv2.imshow('gray',gray)

 高斯降噪,边缘检测,得到轮廓图,显示轮廓图。

Python 下opencv 应用: 物体数数_第2张图片

# blur the image (to reduce false-positive detections) and then
# perform edge detection
blurred = cv2.GaussianBlur(gray, (3, 3), 0)
edged = cv2.Canny(blurred, 50, 130)
cv2.imshow("blur",blurred)
cv2.imshow('edged',edged)

在轮廓图中,寻找轮廓列表,初始化计算为0

# find contours in the edge map and initialize the total number of
# shapes found
cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,
	cv2.CHAIN_APPROX_SIMPLE)
print(cnts)
cnts = imutils.grab_contours(cnts)
total = 0

循环每一个轮廓,计算轮廓包含的面积像素个数,太小(25),就认为是噪声,忽略掉。

正常的话,画图轮廓,计数+1。

# loop over the contours one by one
for c in cnts:
	# if the contour area is small, then the area is likely noise, so
	# we should ignore the contour
	if cv2.contourArea(c) < 25:
		continue

	# otherwise, draw the contour on the image and increment the total
	# number of shapes found
	cv2.drawContours(image, [c], -1, (204, 0, 255), 2)
	total += 1

打印出总物体个数,并显示勾画轮廓的图形。

Python 下opencv 应用: 物体数数_第3张图片

# show the output image and the final shape count
print("[INFO] found {} shapes".format(total))
cv2.imshow("Image", image)
cv2.waitKey(0)

打印显示的结果是:

images/shapes.png
[INFO] found 17 shapes

有17个,数一数,对吗?

最后列出完整的代码

# USAGE
# python count_shapes.py --image images/shapes.png

# import the necessary packages
import argparse
import imutils
import cv2

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", default='images/shapes.png',
	help="path to the input image")
args = vars(ap.parse_args())

# load the input image and convert it to grayscale
print(args["image"])
image = cv2.imread(args["image"])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow('image',image)
cv2.imshow('gray',gray)

# blur the image (to reduce false-positive detections) and then
# perform edge detection
blurred = cv2.GaussianBlur(gray, (3, 3), 0)
edged = cv2.Canny(blurred, 50, 130)
#cv2.imshow("blur",blurred)
cv2.imshow('edged',edged)

# find contours in the edge map and initialize the total number of
# shapes found
cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,
	cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
total = 0

# loop over the contours one by one
for c in cnts:
	# if the contour area is small, then the area is likely noise, so
	# we should ignore the contour
	if cv2.contourArea(c) < 25:
		continue

	# otherwise, draw the contour on the image and increment the total
	# number of shapes found
	cv2.drawContours(image, [c], -1, (204, 0, 255), 2)
	total += 1

# show the output image and the final shape count
print("[INFO] found {} shapes".format(total))
cv2.imshow("Image", image)
cv2.waitKey(0)

 

你可能感兴趣的:(python)