资源简介
(OpenCV)图像目标尺寸检测
pyimagesearch技术博客上的一篇文章,《Measuring size of objects in an image with OpenCV》,原文作者:Adrian Rosebrock 。
改代码可直接执行

代码片段和文件信息
# USAGE
# python object_size.py --image images/example_01.png --width 0.955
# python object_size.py --image images/example_02.png --width 0.955
# python object_size.py --image images/example_03.png --width 3.5
# import the necessary packages
from scipy.spatial import distance as dist
from imutils import perspective
from imutils import contours
import numpy as np
import argparse
import imutils
import cv2
def midpoint(ptA ptB):
return ((ptA[0] + ptB[0]) * 0.5 (ptA[1] + ptB[1]) * 0.5)
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument(“-i“ “--image“ required=True
help=“path to the input image“)
ap.add_argument(“-w“ “--width“ type=float required=True
help=“width of the left-most object in the image (in inches)“)
args = vars(ap.parse_args())
# load the image convert it to grayscale and blur it slightly
image = cv2.imread(args[“image“])
gray = cv2.cvtColor(image cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray (7 7) 0)
# perform edge detection then perform a dilation + erosion to
# close gaps in between object edges
edged = cv2.Canny(gray 50 100)
edged = cv2.dilate(edged None iterations=1)
edged = cv2.erode(edged None iterations=1)
# find contours in the edge map
cnts = cv2.findContours(edged.copy() cv2.RETR_EXTERNAL
cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
# sort the contours from left-to-right and initialize the
# ‘pixels per metric‘ calibration variable
(cnts _) = contours.sort_contours(cnts)
pixelsPerMetric = None
# loop over the contours individually
for c in cnts:
# if the contour is not sufficiently large ignore it
if cv2.contourArea(c) < 100:
continue
# compute the rotated bounding box of the contour
orig = image.copy()
box = cv2.minAreaRect(c)
box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box)
box = np.array(box dtype=“int“)
# order the points in the contour such that they appear
# in top-left top-right bottom-right and bottom-left
# order then draw the outline of the rotated bounding
# box
box = perspective.order_points(box)
cv2.drawContours(orig [box.astype(“int“)] -1 (0 255 0) 2)
# loop over the original points and draw them
for (x y) in box:
cv2.circle(orig (int(x) int(y)) 5 (0 0 255) -1)
# unpack the ordered bounding box then compute the midpoint
# between the top-left and top-right coordinates followed by
# the midpoint between bottom-left and bottom-right coordinates
(tl tr br bl) = box
(tltrX tltrY) = midpoint(tl tr)
(blbrX blbrY) = midpoint(bl br)
# compute the midpoint between the top-left and top-right points
# followed by the midpoint between the top-righ and bottom-right
(tlblX tlblY) = midpoint(tl bl)
(trbrX trbrY) = midpoint(tr br)
# draw the midpoints on the image
cv2.circle(orig (int(tltrX) int(tltrY)) 5 (255 0 0) -1)
cv2.circle(orig (int(blbrX) int(blbrY)) 5 (255 0 0) -1)
cv2.circle(orig (int(tlblX)
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2016-03-16 18:54 size-of-ob
目录 0 2016-03-11 12:28 size-of-ob
文件 389285 2016-03-10 14:58 size-of-ob
文件 691806 2016-03-10 16:52 size-of-ob
文件 426623 2016-03-11 11:34 size-of-ob
文件 4150 2016-03-14 14:17 size-of-ob
- 上一篇:数字通信原理 Gallager 课后习题解答
- 下一篇:Vue框架介绍及应用ppt
相关资源
- [免费]图像识别c 源码
- 基于opencv的图像识别识别图像中的色
- 一种基于模板匹配的人民币编号识别
- 水果识别代码
- 图像识别程序代码Version 2.0 January 20
- 微信小程序使用AI开放平台,实现图像
- 医学图像识别研究
- 勘智K210 20种物体识别工程添加图像处
- 基于人工神经网络的图像识别和分类
- bp神经网络图像识别
- tesseract-ocr/tessdata 语言包
- 基于STM32的图像识别基于STM32的图像识
- opencv数码管识别程序
- 基于图像识别的电熨斗点胶机系统
- 基于深度学习的图像检索研究
- gif动态彩图转化黑白动画简单图形识
- 基于卷积神经网络的图像识别
- OpenMv教程由入门到精通
- 基于卷积神经网络的图像识别算法的
- 花卉图像识别与分类
- 背景差分法进行图像识别
- 深度学习在医学图像识别中的研究
- QT+OpenCV实现图像识别
- Halcon区域特征分析:案例-提取遥控器
- 指纹识别算法
- tensorflow的安装、图像识别应用、训练
- CNN做遥感图像目标识别完整代码
- 图像识别巡线智能车设计.zip
- 狗品种图像识别算法CNN
- 机器人视觉测量与控制最新版徐德 国
评论
共有 条评论