资源简介

1.本资源主要是基于python-opencv进行开发的,主要功能是用鼠标提取不规则的ROI以便进行后续处理。
2.https://blog.csdn.net/imwaters/article/details/80808491博文配套代码,整理了一些参考资料。代码基本是一样的,有分的麻烦还是下载支持一下。

资源截图

代码片段和文件信息

# -*- coding: utf-8 -*-
“““
Created on Sat Jun 23 15:16:16 2018
@author: Zengxy_GUET
“““

import cv2
import numpy as np
global img
global point1 point2

lsPointsChoose=[]
tpPointsChoose=[]

pointsCount=0
count=0 
pointsMax=5

#-----------------------鼠标操作相关------------------------------------------
#------------------------------------------------------------------------------
lsPointsChoose=[]
tpPointsChoose=[]

pointsCount=0
count=0
pointsMax=5

def on_mouse(event x y flags param):
    global img point1 point2countpointsMax
    global lsPointsChoosetpPointsChoose  #存入选择的点
    global pointsCount   #对鼠标按下的点计数
    global img2ROI_bymouse_flag
    img2 = img.copy()   #此行代码保证每次都重新再原图画  避免画多了
#-----------------------------------------------------------

#    count=count+1
#    print(“callback_count“count)
#--------------------------------------------------------------

    if event == cv2.EVENT_LBUTTONDOWN:         #左键点击

        pointsCount=pointsCount+1
    #       为了保存绘制的区域,画的点稍晚清零
        if(pointsCount==pointsMax+1):
            pointsCount=0
            tpPointsChoose=[]
        print(‘pointsCount:‘pointsCount)
        point1 = (xy)
        print (xy)
#        画出点击的点
        cv2.circle(img2 point1 10 (02550) 5)

#       将选取的点保存到list列表里
        lsPointsChoose.append([xy])  #用于转化为darry 提取多边形ROI
        tpPointsChoose.append((xy))  #用于画点
            #----------------------------------------------------------------------
    #将鼠标选的点用直线链接起来
        print(len(tpPointsChoose))
        for i in range(len(tpPointsChoose)-1):
            print(‘i‘i)
            cv2.line(img2tpPointsChoose[i]tpPointsChoose[i+1](00255)5)
#----------------------------------------------------------------------
#----------点击到pointMax时可以提取去绘图----------------
        if(pointsCount==pointsMax):
#-----------绘制感兴趣区域-----------
#----------------------------------
            ROI_byMouse()
            ROI_bymouse_flag=1
            lsPointsChoose=[]

#--------------------------------------------------------
        cv2.imshow(‘src‘ img2)
#-------------------------右键按下清除轨迹-----------------------------
    if event == cv2.EVENT_RBUTTONDOWN:         #右键点击
        print(“right-mouse“)
        pointsCount=0
        tpPointsChoose=[]
        lsPointsChoose=[]
        print(len(tpPointsChoose))
        for i in range(len(tpPointsChoose)-1):
            print(‘i‘i)
            cv2.line(img2tpPointsChoose[i]tpPointsChoose[i+1](00255)5)
        cv2.imshow(‘src‘ img2)
#-----------------------------------------------------------------------
#%%
#--------------------------------------------------------------
def ROI_byMouse():
    global srcROIROI_flagmask2
    mask = np.zeros(img.shape np.uint8)
    pts = np.array([lsPointsChoose] np.int32)
# pts.shape=(4,2)
    pts = pts.reshape((-112)) # -1代表剩下的维度自动计算
# reshape 后的 pts.shape=(4。1,2)??
#--------------画多边形----

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件    6220854  2018-06-24 09:42  mask0623.bmp

     文件       5013  2018-06-25 23:17  polylineROI-mouse-only_180625.py

     文件       1967  2018-06-25 22:51  rectangle-mouse-only-180625.py

     文件    6220854  2018-06-24 09:42  ROI0623.bmp

     文件    6220854  2018-06-23 15:38  frame_1.bmp

----------- ---------  ---------- -----  ----

             18669542                    5


评论

共有 条评论