当前位置: 首页 > news >正文

用python和opencv来测量目标到相机的距离_毕业课题项目——基于单目摄像头的距离测量...

1 importnumpy as np2 importcv23 #找到目标函数

4 deffind_marker(image):5 #convert the image to grayscale, blur it, and detect edges

6 #将图像转换成灰度、模糊和检测边缘

7 gray =cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)8 gray = cv2.GaussianBlur(gray, (5, 5), 0)9 edged = cv2.Canny(gray, 35, 125)10

11 #find the contours in the edged image and keep the largest one;

12 #在边缘图像中找到轮廓并保持最大的轮廓

13 #we'll assume that this is our piece of paper in the image

14 #我们假设这是我们在图像中的一张纸

15 (_, cnts, _) =cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)16 #求最大面积

17 c = max(cnts, key =cv2.contourArea)18

19 #compute the bounding box of the of the paper region and return it

20 #计算纸张区域的边界框并返回它

21 #cv2.minAreaRect() c代表点集,返回rect[0]是最小外接矩形中心点坐标,

22 #rect[1][0]是width,rect[1][1]是height,rect[2]是角度

23 returncv2.minAreaRect(c)24

25 #距离计算函数

26 defdistance_to_camera(knownWidth, focalLength, perWidth):27 #compute and return the distance from the maker to the camera

28 #计算并返回从目标到相机的距离

29 return (knownWidth * focalLength) /perWidth30

31 #initialize the known distance from the camera to the object, which

32 #in this case is 24 inches

33 #初始化已知距离从相机到对象,在这种情况下是24英寸

34 KNOWN_DISTANCE = 24.0

35

36 #initialize the known object width, which in this case, the piece of

37 #paper is 11 inches wide

38 #初始化已知的物体宽度,在这种情况下,纸是11英寸宽。

39 #A4纸的长和宽(单位:inches)

40 KNOWN_WIDTH = 11.69

41 KNOWN_HEIGHT = 8.27

42

43 #initialize the list of images that we'll be using

44 #初始化我们将要使用的图像列表

45 IMAGE_PATHS = ["Picture1.jpg", "Picture2.jpg", "Picture3.jpg"]46

47 #load the furst image that contains an object that is KNOWN TO BE 2 feet

48 #from our camera, then find the paper marker in the image, and initialize

49 #the focal length

50 #加载包含一个距离我们相机2英尺的物体的第一张图像,然后找到图像中的纸张标记,并初始化焦距

51 #读入第一张图,通过已知距离计算相机焦距

52 image = cv2.imread("E:\\lena.jpg") #应使用摄像头拍的图

53 marker =find_marker(image)54 focalLength = (marker[1][0] * KNOWN_DISTANCE) / KNOWN_WIDTH #D’ = (W x F) / P

55

56 #通过摄像头标定获取的像素焦距

57 #focalLength = 811.82

58 print('focalLength =',focalLength)59

60 #打开摄像头

61 camera =cv2.VideoCapture(0)62

63 whilecamera.isOpened():64 #get a frame

65 (grabbed, frame) =camera.read()66 marker =find_marker(frame)67 if marker ==0:68 print(marker)69 continue

70 inches = distance_to_camera(KNOWN_WIDTH, focalLength, marker[1][0])71

72 #draw a bounding box around the image and display it

73 #在图像周围绘制一个边界框并显示它

74 box =cv2.boxPoints(marker)75 box =np.int0(box)76 cv2.drawContours(frame, [box], -1, (0, 255, 0), 2)77

78 #inches 转换为 cm

79 cv2.putText(frame, "%.2fcm" % (inches *30.48/ 12),80 (frame.shape[1] - 200, frame.shape[0] - 20), cv2.FONT_HERSHEY_SIMPLEX,81 2.0, (0, 255, 0), 3)82

83 #show a frame

84 cv2.imshow("capture", frame)85 if cv2.waitKey(1) & 0xFF == ord('q'):86 break

87 camera.release()88 cv2.destroyAllWindows()

相关文章:

  • c语言判断素数_C语言的那些经典程序 第十一期
  • delphi usb通讯 com通讯_你知道电脑上怎么设置“串口号COM”吗?教你一个方法,1分钟学会...
  • c++ format长度限制_2020年C证(安全员)考试总结及C证(安全员)模拟试题
  • post多个参数_2020Python编程高频面试题:Post和get区别?
  • docker rabbitmq_SpringBoot2.x系列教程64--消息队列之RabbitMQ安装及环境变量配置
  • before start of result set_ECMAScript 6 入门教程—Iterator 和 for...of 循环
  • 前端打印样式乱了_好程序员HTML5大前端分享web前端面试题集锦三
  • python第八周小测验_Python小测试_9
  • 初学者选黑卡还是微单_回答自测你是摄影初学者,还是进阶?
  • python创建随机数组_python随机数组如何生成?
  • java stringbuffer 空格_Java面试真题 |String 的特点是什么?它有哪些重要的方法?...
  • 想要导航提示进入页_一个值得全体信息流优化师和SEMer珍藏的落地页
  • python实现kmean算法_python实现K-means算法
  • python入门干货_Python干货整理,从入门说起(7.4)
  • jsp页面乱码_SpringMVC是如何实现作用域传值和页面跳转的?【源码解读】
  • 《微软的软件测试之道》成书始末、出版宣告、补充致谢名单及相关信息
  • 4个实用的微服务测试策略
  • CSS中外联样式表代表的含义
  • interface和setter,getter
  • java取消线程实例
  • Python语法速览与机器学习开发环境搭建
  • React+TypeScript入门
  • Stream流与Lambda表达式(三) 静态工厂类Collectors
  • web标准化(下)
  • 欢迎参加第二届中国游戏开发者大会
  • 开放才能进步!Angular和Wijmo一起走过的日子
  • 我与Jetbrains的这些年
  • Prometheus VS InfluxDB
  • #gStore-weekly | gStore最新版本1.0之三角形计数函数的使用
  • $$$$GB2312-80区位编码表$$$$
  • (SpringBoot)第七章:SpringBoot日志文件
  • (webRTC、RecordRTC):navigator.mediaDevices undefined
  • (附源码)springboot 校园学生兼职系统 毕业设计 742122
  • (附源码)ssm高校升本考试管理系统 毕业设计 201631
  • (接口自动化)Python3操作MySQL数据库
  • (一)Thymeleaf用法——Thymeleaf简介
  • (转)http协议
  • (转)详解PHP处理密码的几种方式
  • *Django中的Ajax 纯js的书写样式1
  • .naturalWidth 和naturalHeight属性,
  • .NET 4.0中的泛型协变和反变
  • .net core 微服务_.NET Core 3.0中用 Code-First 方式创建 gRPC 服务与客户端
  • .Net 高效开发之不可错过的实用工具
  • .NET设计模式(11):组合模式(Composite Pattern)
  • [ 渗透测试面试篇 ] 渗透测试面试题大集合(详解)(十)RCE (远程代码/命令执行漏洞)相关面试题
  • [ 数据结构 - C++] AVL树原理及实现
  • [BZOJ 4598][Sdoi2016]模式字符串
  • [C]编译和预处理详解
  • [ERROR ImagePull]: failed to pull image k8s.gcr.io/kube-controller-manager失败
  • [Fri 26 Jun 2015 ~ Thu 2 Jul 2015] Deep Learning in arxiv
  • [GYCTF2020]Ez_Express
  • [html] 动态炫彩渐变背景
  • [IDF]被改错的密码
  • [NOIP2007 普及组] 纪念品分组--贪心算法
  • [OIDC in Action] 3. 基于OIDC(OpenID Connect)的SSO(添加Github OAuth 2.0的支持)