来源OpenCV的官方教程:https://github.com/dloperab/PyImageSearch-CV-DL-CrashCourse#day-6-measuring-size-of-objects-in-an-image-with-opencv

使用方法:

将下面的代码保存为 object_size.py 文件

  1. 新建一个文件main.py

    import os
    os.system("python object_size.py --image 图片路径 --width 0.955")
    这里width后面的参数含义是图像最左边的物体离边缘的距离

  2. 在命令行中输入 python object_size.py --image 图片路径 --width 0.955

核心代码:object_size.py

# 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
import osdef 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 = imutils.grab_contours(cnts)# 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 itif cv2.contourArea(c) < 100:continue# compute the rotated bounding box of the contourorig = 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# boxbox = perspective.order_points(box)cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)# loop over the original points and draw themfor (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 imagecv2.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), int(tlblY)), 5, (255, 0, 0), -1)cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1)# draw lines between the midpointscv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)),(255, 0, 255), 2)cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)),(255, 0, 255), 2)# compute the Euclidean distance between the midpointsdA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY))dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY))# if the pixels per metric has not been initialized, then# compute it as the ratio of pixels to supplied metric# (in this case, inches)if pixelsPerMetric is None:pixelsPerMetric = dB / args["width"]# compute the size of the objectdimA = dA / pixelsPerMetricdimB = dB / pixelsPerMetric# draw the object sizes on the imagecv2.putText(orig, "{:.1f}in".format(dimA),(int(tltrX - 15), int(tltrY - 10)), cv2.FONT_HERSHEY_SIMPLEX,0.65, (255, 255, 255), 2)cv2.putText(orig, "{:.1f}in".format(dimB),(int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX,0.65, (255, 255, 255), 2)# show the output imagecv2.imshow("Image", orig)cv2.waitKey(0)

OpenCV检验物体尺寸相关推荐

  1. opencv实战---物体尺寸测量

    物体尺寸测量的思路是找一个确定尺寸的物体作为参照物,根据已知的计算未知物体尺寸. 如下图所示,绿色的板子尺寸为220*300(单位:毫米),通过程序计算白色纸片的长度. 目录 1.相关库 2.读图+图 ...

  2. opencv图像处理中的一些滤波器+利用滤波器提取条形码(解析二维码)+公交卡倾斜矫正+物体尺寸丈量

    一般来说,图像的能量主要集中在其低频部分,噪声所在的频段主要在高频段,同时图像中的细节信息也主要集中在其高频部分,因此,如何去掉高频干扰同时又保持细节信息是关键.为了去除噪声,有必要对图像进行平滑,可 ...

  3. OpenCV测量物体的尺寸技能 get~

    点击上方"Python爬虫与数据挖掘",进行关注 回复"书籍"即可获赠Python从入门到进阶共10本电子书 今 日 鸡 汤 盛年不重来,一日难再晨. 及时当勉 ...

  4. openCV 轮廓查找-测量物体尺寸

    一,利用openCV的findContours轮廓查找功能,用已知物体的尺寸(比如硬币)作为参考,根据实际尺寸与像素尺寸的比列,求出图片中物体的实际大小.存在的问题有两个: 图片的阴影导致轮廓不准确, ...

  5. 【机器视觉案例】(5) AI视觉,手势调节物体尺寸,附python完整代码

    各位同学好,今天和大家分享一下如何使用opencv+mediapipe完成远程手势调节图片尺寸的案例.先放张图看效果.当拇指和食指竖起时,根据食指间的连线的长度自由缩放图片尺寸.图片的中点始终位于指尖 ...

  6. 基于stm32的非接触式物体尺寸形态测量仪(改进版)

    目录(完整论文+程序+视频可通过主页私信获) 第一章 绪论 1.1 背景和意义 1.1.1 背景 1.1.2 意义 1.2  国内外研究现状 第二章 测量仪设计方案 2.1 方案一 2.2 方案二 2 ...

  7. python编程实现图片内多个物体尺寸测量

    要实现图片内多个物体尺寸测量,你可以使用计算机视觉库,如 OpenCV 来实现. 首先,你需要读取图片,然后对图像进行预处理,以便更容易地检测到图像中的物体.例如,你可以使用边缘检测算法来提取边缘,或 ...

  8. 【入门讲解】Python使用OpenCV设置图片尺寸。

    前文 前几天看到了"某音特效",人物头像动漫化的效果,感觉这个特效蛮不错的,之前也有找资料学习写了动漫化的人物文章. 接触到了OpenCV,关于人脸识别.人脸检测方面的python ...

  9. 机器学习水果识别——python+opencv实现物体特征提取

    文章目录 一.用python+opencv实现物体特征值提取 1.读取图像.转为灰度图像并降噪 2.获取水果轮廓 将最大轮廓画入最开始的img图像并显示 将小于某一规模的轮廓删除 3.提取水果的面积周 ...

最新文章

  1. Message、Handler、Message Queue、Looper之间的关系
  2. 今晚20:00 | 港科大郑光廷院士详解人工视觉技术发展及应用
  3. 考夫曼:破解大脑代码并创建真正的人工智能
  4. python templates_python templates在哪
  5. python的栈在哪个库_Python实现栈的方法
  6. python提示list index out of range_为什么python报错说“list index out of range”
  7. 循环 直到 python_如果您在Python中存在慢循环,则可以对其进行修复……直到无法解决为止...
  8. c++矩阵连乘的动态规划算法并输出_你在Java中用过动态规划吗?
  9. 手工释放linux内存——/proc/sys/vm/drop_caches
  10. 如何“在21天内自学C++”
  11. 查找类似图片关键字查找图片
  12. K8S调试工具之--nsenter
  13. java String的intern()方法
  14. LinuxProbe 0x13 网站服务程序、SELinux安全子系统、个人用户主页功能、虚拟网站主机功能
  15. new(创建)一个对象时都发生了什么?
  16. spring整合mongoDB 和 Redis 极简入门
  17. android内存释放处理
  18. [陈鹏导师精益项目实战]华东区电机企业精益生产项目第五期启动
  19. ACS712中文资料_描述(电流传感器)
  20. 【JMeter 菜鸟实操之五】ant+jenkins 完善html结果报告

热门文章

  1. 格式化代码php,格式化php代码的两种方法
  2. A Brief Overview Of Vulkan API
  3. 功成身退:AMD Mantle不再优化了
  4. 【ASK】设置网卡启动遇到的事!
  5. 判断表达式是否正确闭合,返回未闭合元素的下标。
  6. 使用spring框架时,使用xml还是注解
  7. Office web apps 服务器运行一段时间之后CPU就是达到100%
  8. ------webkitformboundary
  9. UcSTAR统一通信平台
  10. 蓝桥杯2020答案c语言b组,2020十月份蓝桥杯B组省赛题解大全(害!附题面文件和部分代码~)...