赞
踩
要点 :1、鼠标回调函数获取四边形四点坐标值(list型)
2、将四边形四点坐标值(list型)转换为array型
3、通过透视函数变换实现四边形-》矩阵过程
使用 :1、鼠标双击四个坐标点,依次为左上、右上、左下、右下
(1)----------------(2)**
(1)-------------------(2)
|----------------------|
(3)-------------------(4)
2、再次双击任意位置确认定标完成
(用杰伦做效果展示嘻嘻嘻)
1、第一帧定标图
2、视频流定标
3、视频流矩阵变换输出效果
import cv2 import numpy as np camera = cv2.VideoCapture(1) #摄像头 # camera = cv2.VideoCapture('E:/视频/quanjing.mp4') #视频流 count = 0 #鼠标事件计数值(左键双击一次计数值加一) Recpoints = [] #空列表用于储存原始图像四点坐标 point2 = np.array([[0, 0], [320, 0], [0, 180], [320, 180]], dtype="float32") def draw_circle(event,x,y,flags,param): global count global Recpoints if event==cv2.EVENT_LBUTTONDBLCLK: count = count + 1 # print('jishu',count) # print('[',x,',',y,']') Recpoints.append([x,y]) print(Recpoints) cv2.circle(img,(x,y),10,(0,0,255),-1) ret,img = camera.read() #img作为第一帧选定坐标点的图像 cv2.namedWindow('image',cv2.WINDOW_NORMAL) cv2.namedWindow('video',cv2.WINDOW_NORMAL) cv2.setMouseCallback('image',draw_circle) ret, frame = camera.read() while(count < 5): cv2.imshow('image',img) point1 = np.array(Recpoints,dtype = "float32") for (x, y) in Recpoints: cv2.circle(frame, (x, y), 10, (0, 255, 0), -1) cv2.imshow('video',frame) ret, frame = camera.read() if cv2.waitKey(20)&0xFF==27: #按ESC退出 break print('dingbiao-end') #四个坐标点定标结束 # print('pit1',point1) # print('pit2',point2) cv2.destroyWindow('image') #关闭前两个窗口 cv2.destroyWindow('video') cv2.namedWindow('out', cv2.WINDOW_NORMAL) #透视变换后窗口 while(1): #透视变换 ret, frame = camera.read() w = frame.shape[0] h = frame.shape[1] M = cv2.getPerspectiveTransform(point1, point2) out_img = cv2.warpPerspective(frame, M, (w, h)) cv2.imshow('out', out_img) ret, image = camera.read() if cv2.waitKey(20) & 0xFF == 27: # 按ESC退出 break cv2.destroyAllWindows() `` # 以上代码仅供参考
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。