Opencv Q&A_1
2022/3/4 23:48:21
本文主要是介绍Opencv Q&A_1,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
2022/03/04
利用opencv计算角度
代码
import cv2 as cv import math img = cv.imread('D:\work\\automation\Skill\Python\pythonWORK\cv\material\\angle.jpg') pointlist = [] #存放坐标的列表 def mouseclick(event,x,y,flag,params): #鼠标事件函数(事件类型,事件坐标) if event == cv.EVENT_LBUTTONDOWN: #判断事件类型 pointlist.append([x,y]) cv.circle(img,(x,y),8,(0,0,255),cv.FILLED) #画圈(图片,中心坐标,大小,颜色,确认变色) if len(pointlist) == 2: #画连线 cv.line(img,(pointlist[0][0],pointlist[0][1]),(pointlist[1][0],pointlist[1][1]),(0,0,255),2) if len(pointlist) == 3: cv.line(img, (pointlist[1][0], pointlist[1][1]),( pointlist[2][0], pointlist[2][1]),(0,0,255),2) # print(pointlist) # while True: # cv.imshow('img',img) # cv.setMouseCallback('img',mouseclick) #检测鼠标事件(图片,事件函数) # flag_ = cv.waitKey(1) # if flag_ & 0xFF == ord('c'): #返回值进行与操作得到二进制进行比较(ord也为二进制操作) # pointlist.clear() # img = cv.imread('D:\work\\automation\Skill\Python\pythonWORK\cv\material\\angle.jpg') # if flag_ & 0xFF == ord('q'): # break def getangle(pointlist_): #计算角度(余弦公式) a = math.sqrt(math.pow(pointlist_[1][0] - pointlist_[2][0], 2) + math.pow(pointlist_[1][1] - pointlist_[2][1], 2)) b = math.sqrt(math.pow(pointlist_[0][0] - pointlist_[2][0], 2) + math.pow(pointlist_[0][1] - pointlist_[2][1], 2)) c = math.sqrt(math.pow(pointlist_[0][0] - pointlist_[1][0], 2) + math.pow(pointlist_[0][1] - pointlist_[1][1], 2)) # print(str(a)+ 'and' + str(b)+ 'and' + str(c)) theta = round(math.acos((pow(a,2)+pow(c,2)-pow(b,2))/(2*a*c))/math.pi*180,2) return theta while True: cv.imshow('img',img) cv.setMouseCallback('img',mouseclick) #检测鼠标事件(图片,事件函数) flag_ = cv.waitKey(1) if flag_ & 0xFF == ord('c'): #返回值进行与操作得到二进制进行比较(ord也为二进制操作) pointlist.clear() #清空列表 img = cv.imread('D:\work\\automation\Skill\Python\pythonWORK\cv\material\\angle.jpg') #重新加载未修改图片 if flag_ & 0xFF == ord('q'): break if len(pointlist) == 3: #列表存放三个坐标时显示角度同时清空列表 cv.putText(img,str(getangle(pointlist)),(pointlist[1][0]-20,pointlist[1][1]+20),cv.FONT_HERSHEY_SIMPLEX,2,(255,0,0),3) pointlist.clear()
运行效果
遇到的问题
Q1:cv2.waitKey(n)的使用
A1:cv2.imshow()是展示出图片,而cv2.waitKey(n)保持图片展示,当n≠0,cv2.imshow()展示图片n(ms)后关闭图片,期间等待键盘指令输入并返回该输入值;当n=0,则一直等待。
Q2:cv2.setMouseCallback()的使用
A2:检测事件并反馈。由自定义函数确定事件和反馈功能。事件发生时,自动对自定义函数传入事件类型,坐标。(还有flag和param后续尝试)
Q3:cv2.circle、cv2.line、cv2.putText函数
A3:详情见代码注释
这篇关于Opencv Q&A_1的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15PingCAP 黄东旭参与 CCF 秀湖会议,共探开源教育未来
- 2024-05-13PingCAP 戴涛:构建面向未来的金融核心系统
- 2024-05-09flutter3.x_macos桌面os实战
- 2024-05-09Rust中的并发性:Sync 和 Send Traits
- 2024-05-08使用Ollama和OpenWebUI在CPU上玩转Meta Llama3-8B
- 2024-05-08完工标准(DoD)与验收条件(AC)究竟有什么不同?
- 2024-05-084万 star 的 NocoDB 在 sealos 上一键起,轻松把数据库编程智能表格
- 2024-05-08Mac 版Stable Diffusion WebUI的安装
- 2024-05-08解锁CodeGeeX智能问答中3项独有的隐藏技能
- 2024-05-08RAG算法优化+新增代码仓库支持,CodeGeeX的@repo功能效果提升