圆月山庄资源网 Design By www.vgjia.com

本文实例为大家分享了python-opencv鼠标事件画框圈定目标的具体代码,供大家参考,具体内容如下

在视频/相机中,用鼠标画矩形框,圈定目标,从而获得鼠标的起始坐标点a、终止坐标点b

# -*- coding: utf-8 -*-
"""
Created on Tue Dec 27 09:32:02 2016

@author: http://blog.csdn.net/lql0716
"""
import cv2
import numpy as np

current_pos = None
tl = None
br = None

#鼠标事件
def get_rect(im, title='get_rect'): # (a,b) = get_rect(im, title='get_rect')
 mouse_params = {'tl': None, 'br': None, 'current_pos': None,
 'released_once': False}

 cv2.namedWindow(title)
 cv2.moveWindow(title, 100, 100)

 def onMouse(event, x, y, flags, param):

 param['current_pos'] = (x, y)

 if param['tl'] is not None and not (flags & cv2.EVENT_FLAG_LBUTTON):
  param['released_once'] = True

 if flags & cv2.EVENT_FLAG_LBUTTON:
  if param['tl'] is None:
  param['tl'] = param['current_pos']
  elif param['released_once']:
  param['br'] = param['current_pos']

 cv2.setMouseCallback(title, onMouse, mouse_params)
 cv2.imshow(title, im)

 while mouse_params['br'] is None:
 im_draw = np.copy(im)

 if mouse_params['tl'] is not None:
  cv2.rectangle(im_draw, mouse_params['tl'],
  mouse_params['current_pos'], (255, 0, 0))

 cv2.imshow(title, im_draw)
 _ = cv2.waitKey(10)

 cv2.destroyWindow(title)

 tl = (min(mouse_params['tl'][0], mouse_params['br'][0]),
 min(mouse_params['tl'][1], mouse_params['br'][1]))
 br = (max(mouse_params['tl'][0], mouse_params['br'][0]),
 max(mouse_params['tl'][1], mouse_params['br'][1]))

 return (tl, br) #tl=(y1,x1), br=(y2,x2)

#读取摄像头/视频,然后用鼠标事件画框 
def readVideo(pathName, skipFrame): #pathName为视频文件路径,skipFrame为视频的第skipFrame帧
 cap = cv2.VideoCapture(0) #读取摄像头
 if not cap.isOpened(): #如果为发现摄像头,则按照路径pathName读取视频文件
 cap = cv2.VideoCapture(pathName) #读取视频文件,如pathName='D:/test/test.mp4'
 c = 1

 while(cap.isOpened()):
 ret, frame = cap.read()
 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
 if(c>=skipFrame):
  mask = np.zeros(gray.shape, dtype=np.uint8) #掩码操作,该矩阵与图片大小类型一致,为初始化全0像素值,之后对其操作区域赋值为1即可
  if(c==skipFrame):
  (a,b) = get_rect(frame, title='get_rect') #鼠标画矩形框
  img01, img02 = frame, frame
  gray01, gray02 = gray, gray
  else:
  img1, img2 = prev_frame, frame
  gray1, gray2 = prev_frame, frame
  cv2.imshow('frame', frame)
 c = c + 1
 prev_gray = gray
 prev_frame = frame
 if cv2.waitKey(1) & 0xFF == ord('q'): #点击视频窗口,按q键退出
  break
 cap.release()
 cv2.destroyAllWindows()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
python鼠标事件,python,opencv鼠标事件获取坐标,python画框圈定目标

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?