侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Python OpenCV实现鼠标画框效果

Python  /  管理员 发布于 7年前   334

使用Python+OpenCV实现鼠标画框的代码,供大家参考,具体内容如下

# -*-coding: utf-8 -*-""" @Project: IntelligentManufacture @File : user_interaction.py @Author : panjq @E-mail : [email protected] @Date : 2019-02-21 15:03:18"""# -*- coding: utf-8 -*- import cv2from utils import image_processingimport numpy as npglobal imgglobal point1, point2global g_rect def on_mouse(event, x, y, flags, param): global img, point1, point2,g_rect img2 = img.copy() if event == cv2.EVENT_LBUTTONDOWN: # 左键点击,则在原图打点  print("1-EVENT_LBUTTONDOWN")  point1 = (x, y)  cv2.circle(img2, point1, 10, (0, 255, 0), 5)  cv2.imshow('image', img2)  elif event == cv2.EVENT_MOUSEMOVE and (flags & cv2.EVENT_FLAG_LBUTTON): # 按住左键拖曳,画框  print("2-EVENT_FLAG_LBUTTON")  cv2.rectangle(img2, point1, (x, y), (255, 0, 0), thickness=2)  cv2.imshow('image', img2)  elif event == cv2.EVENT_LBUTTONUP: # 左键释放,显示  print("3-EVENT_LBUTTONUP")  point2 = (x, y)  cv2.rectangle(img2, point1, point2, (0, 0, 255), thickness=2)  cv2.imshow('image', img2)  if point1!=point2:   min_x = min(point1[0], point2[0])   min_y = min(point1[1], point2[1])   width = abs(point1[0] - point2[0])   height = abs(point1[1] - point2[1])   g_rect=[min_x,min_y,width,height]   cut_img = img[min_y:min_y + height, min_x:min_x + width]   cv2.imshow('ROI', cut_img) def get_image_roi(rgb_image): ''' 获得用户ROI区域的rect=[x,y,w,h] :param rgb_image: :return: ''' bgr_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR) global img img=bgr_image cv2.namedWindow('image') while True:  cv2.setMouseCallback('image', on_mouse)  # cv2.startWindowThread() # 加在这个位置  cv2.imshow('image', img)  key=cv2.waitKey(0)  if key==13 or key==32:#按空格和回车键退出   break cv2.destroyAllWindows() img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) return g_rect def select_user_roi(image_path): ''' 由于原图的分辨率较大,这里缩小后获取ROI,返回时需要重新scale对应原图 :param image_path: :return: ''' orig_image = image_processing.read_image(image_path) orig_shape = np.shape(orig_image) resize_image = image_processing.resize_image(orig_image, resize_height=800,resize_width=None) re_shape = np.shape(resize_image) g_rect=get_image_roi(resize_image) orgi_rect = image_processing.scale_rect(g_rect, re_shape,orig_shape) roi_image=image_processing.get_rect_image(orig_image,orgi_rect) image_processing.cv_show_image("RECT",roi_image) image_processing.show_image_rect("image",orig_image,orgi_rect) return orgi_rect  if __name__ == '__main__': # image_path="../dataset/images/IMG_0007.JPG" image_path="../dataset/test_images/lena.jpg"  # rect=get_image_roi(image) rect=select_user_roi(image_path) print(rect)

其中image_processing.py文件如下:

# -*-coding: utf-8 -*-""" @Project: IntelligentManufacture @File : image_processing.py @Author : panjq @E-mail : [email protected] @Date : 2019-02-14 15:34:50""" import osimport globimport cv2import numpy as npimport matplotlib.pyplot as plt def show_image(title, image): ''' 调用matplotlib显示RGB图片 :param title: 图像标题 :param image: 图像的数据 :return: ''' # plt.figure("show_image") # print(image.dtype) plt.imshow(image) plt.axis('on') # 关掉坐标轴为 off plt.title(title) # 图像题目 plt.show() def cv_show_image(title, image): ''' 调用OpenCV显示RGB图片 :param title: 图像标题 :param image: 输入RGB图像 :return: ''' channels=image.shape[-1] if channels==3:  image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # 将BGR转为RGB cv2.imshow(title,image) cv2.waitKey(0) def read_image(filename, resize_height=None, resize_width=None, normalization=False): ''' 读取图片数据,默认返回的是uint8,[0,255] :param filename: :param resize_height: :param resize_width: :param normalization:是否归一化到[0.,1.0] :return: 返回的RGB图片数据 '''  bgr_image = cv2.imread(filename) # bgr_image = cv2.imread(filename,cv2.IMREAD_IGNORE_ORIENTATION|cv2.IMREAD_COLOR) if bgr_image is None:  print("Warning:不存在:{}", filename)  return None if len(bgr_image.shape) == 2: # 若是灰度图则转为三通道  print("Warning:gray image", filename)  bgr_image = cv2.cvtColor(bgr_image, cv2.COLOR_GRAY2BGR)  rgb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB) # 将BGR转为RGB # show_image(filename,rgb_image) # rgb_image=Image.open(filename) rgb_image = resize_image(rgb_image,resize_height,resize_width) rgb_image = np.asanyarray(rgb_image) if normalization:  # 不能写成:rgb_image=rgb_image/255  rgb_image = rgb_image / 255.0 # show_image("src resize image",image) return rgb_imagedef resize_image(image,resize_height, resize_width): ''' :param image: :param resize_height: :param resize_width: :return: ''' image_shape=np.shape(image) height=image_shape[0] width=image_shape[1] if (resize_height is None) and (resize_width is None):#错误写法:resize_height and resize_width is None  return image if resize_height is None:  resize_height=int(height*resize_width/width) elif resize_width is None:  resize_width=int(width*resize_height/height) image = cv2.resize(image, dsize=(resize_width, resize_height)) return imagedef scale_image(image,scale): ''' :param image: :param scale: (scale_w,scale_h) :return: ''' image = cv2.resize(image,dsize=None, fx=scale[0],fy=scale[1]) return image  def get_rect_image(image,rect): ''' :param image: :param rect: [x,y,w,h] :return: ''' x, y, w, h=rect cut_img = image[y:(y+ h),x:(x+w)] return cut_imgdef scale_rect(orig_rect,orig_shape,dest_shape): ''' 对图像进行缩放时,对应的rectangle也要进行缩放 :param orig_rect: 原始图像的rect=[x,y,w,h] :param orig_shape: 原始图像的维度shape=[h,w] :param dest_shape: 缩放后图像的维度shape=[h,w] :return: 经过缩放后的rectangle ''' new_x=int(orig_rect[0]*dest_shape[1]/orig_shape[1]) new_y=int(orig_rect[1]*dest_shape[0]/orig_shape[0]) new_w=int(orig_rect[2]*dest_shape[1]/orig_shape[1]) new_h=int(orig_rect[3]*dest_shape[0]/orig_shape[0]) dest_rect=[new_x,new_y,new_w,new_h] return dest_rect def show_image_rect(win_name,image,rect): ''' :param win_name: :param image: :param rect: :return: ''' x, y, w, h=rect point1=(x,y) point2=(x+w,y+h) cv2.rectangle(image, point1, point2, (0, 0, 255), thickness=2) cv_show_image(win_name, image) def rgb_to_gray(image): image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) return image def save_image(image_path, rgb_image,toUINT8=True): if toUINT8:  rgb_image = np.asanyarray(rgb_image * 255, dtype=np.uint8) if len(rgb_image.shape) == 2: # 若是灰度图则转为三通道  bgr_image = cv2.cvtColor(rgb_image, cv2.COLOR_GRAY2BGR) else:  bgr_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR) cv2.imwrite(image_path, bgr_image) def combime_save_image(orig_image, dest_image, out_dir,name,prefix): ''' 命名标准:out_dir/name_prefix.jpg :param orig_image: :param dest_image: :param image_path: :param out_dir: :param prefix: :return: ''' dest_path = os.path.join(out_dir, name + "_"+prefix+".jpg") save_image(dest_path, dest_image)  dest_image = np.hstack((orig_image, dest_image)) save_image(os.path.join(out_dir, "{}_src_{}.jpg".format(name,prefix)), dest_image) if __name__=="__main__": image_path="../dataset/test_images/src.jpg" image = read_image(image_path, resize_height=None, resize_width=None) image = rgb_to_gray(image) orig_shape=np.shape(image)#shape=(h,w) orig_rect=[50,100,100,200]#x,y,w,h print("orig_shape:{}".format(orig_shape)) show_image_rect("orig",image,orig_rect)  dest_image=resize_image(image,resize_height=None,resize_width=200) dest_shape=np.shape(dest_image) print("dest_shape:{}".format(dest_shape)) dest_rect=scale_rect(orig_rect, orig_shape, dest_shape) show_image_rect("dest",dest_image,dest_rect)

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


  • 上一条:
    python matplotlib库绘制条形图练习题
    下一条:
    python opencv鼠标事件实现画框圈定目标获取坐标信息
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客