2020-04-07 14:33:33 登录注册 RSS

当前位置: 公理网 >> 投诉不良 >> python3+opencv生成不规则黑白mask

python3+opencv生成不规则黑白mask
发布时间:02-18| 来源:公理网 | 点击发表评论

#-*-coding:utf-8-*-importcv2importnumpyasnp#-----------------------鼠标操作相关------------------------------------------lsPointsChoose=[]tpPointsChoose=[]pointsCount=0count=0pointsMax=10defon_mouse(event,x,y,flags,param):globalimg,point1,point2,count,pointsMaxgloballsPointsChoose,tpPointsChoose#存入选择的点globalpointsCount#对鼠标按下的点计数globalimg2,ROI_bymouse_flagimg2=img.copy()#此行代码保证每次都重新再原图画避免画多了#-----------------------------------------------------------#count=count+1#print("callback_count",count)#--------------------------------------------------------------ifevent==cv2.EVENT_LBUTTONDOWN:#左键点击pointsCount=pointsCount+1#感觉这里没有用?2018年8月25日20:06:42#为了保存绘制的区域,画的点稍晚清零#if(pointsCount==pointsMax+1):#pointsCount=0#tpPointsChoose=[]print('pointsCount:',pointsCount)point1=(x,y)print(x,y)#画出点击的点cv2.circle(img2,point1,10,(0,255,0),2)#将选取的点保存到list列表里lsPointsChoose.append([x,y])#用于转化为darry提取多边形ROItpPointsChoose.append((x,y))#用于画点#----------------------------------------------------------------------#将鼠标选的点用直线连起来print(len(tpPointsChoose))foriinrange(len(tpPointsChoose)-1):print('i',i)cv2.line(img2,tpPointsChoose[i],tpPointsChoose[i+1],(0,0,255),2)#----------------------------------------------------------------------#----------点击到pointMax时可以提取去绘图----------------if(pointsCount==pointsMax):#-----------绘制感兴趣区域-----------ROI_byMouse()ROI_bymouse_flag=1lsPointsChoose=[]cv2.imshow('src',img2)#-------------------------右键按下清除轨迹-----------------------------ifevent==cv2.EVENT_RBUTTONDOWN:#右键点击print("right-mouse")pointsCount=0tpPointsChoose=[]lsPointsChoose=[]print(len(tpPointsChoose))foriinrange(len(tpPointsChoose)-1):print('i',i)cv2.line(img2,tpPointsChoose[i],tpPointsChoose[i+1],(0,0,255),2)cv2.imshow('src',img2)defROI_byMouse():globalsrc,ROI,ROI_flag,mask2mask=np.zeros(img.shape,np.uint8)pts=np.array([lsPointsChoose],np.int32)#pts是多边形的顶点列表(顶点集)pts=pts.reshape((-1,1,2))#这里reshape的第一个参数为-1,表明这一维的长度是根据后面的维度的计算出来的。#OpenCV中需要先将多边形的顶点坐标变成顶点数×1×2维的矩阵,再来绘制#--------------画多边形---------------------mask=cv2.polylines(mask,[pts],True,(255,255,255))##-------------填充多边形---------------------mask2=cv2.fillPoly(mask,[pts],(255,255,255))cv2.imshow('mask',mask2)cv2.imwrite('mask.jpg',mask2)ROI=cv2.bitwise_and(mask2,img)#cv2.imwrite('ROI.bmp',ROI)#cv2.imshow('ROI',ROI)#-----------------------定点ROI绘制,程序中未使用-------------------deffixed_ROI():mask=np.zeros(img.shape,np.uint8)pts=np.array([[x1,y1],[x2,y2],[x3,y3],[x4,y4]],np.int32)#顶点集pts=pts.reshape((-1,1,2))mask=cv2.polylines(mask,[pts],True,(255,255,255))mask2=cv2.fillPoly(mask,[pts],(255,255,255))cv2.imshow('mask',mask2)#cv2.imwrite('mask.bmp',mask2)#cv2.drawContours(mask,points,-1,(255,255,255),-1)ROI=cv2.bitwise_and(mask2,img)cv2.imshow('ROI',ROI)#cv2.imwrite('ROI.bmp',ROI)img=cv2.imread('yuantu.jpg')#---------------------------------------------------------#--图像预处理,设置其大小#height,width=img.shape[:2]#size=(int(width*0.3),int(height*0.3))#img=cv2.resize(img,size,interpolation=cv2.INTER_AREA)#------------------------------------------------------------ROI=img.copy()cv2.namedWindow('src')cv2.setMouseCallback('src',on_mouse)cv2.imshow('src',img)cv2.waitKey(0)
20190702114613813.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzE0ODQ1MTE5,size_16,color_FFFFFF,t_70"/>

?
点赞2
收藏
分享

授予成功创建个人博客专栏的用户。专栏中添加五篇以上博文即可点亮!撰写博客专栏浓缩技术精华,专栏达人就是你!

授予每个自然月内发布4篇或4篇以上原创或翻译IT博文的用户。不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!

授予每个自然周发布1篇到3篇原创IT博文的用户。本勋章将于次周周三上午根据用户上周的博文发布情况由系统自动颁发。

最新新闻

手机浏览

公理网 版权所有

公理网 Total 0.032604(s) query 6, 报料QQ:点击这里

给我发消息