您的位置:首页 > 运维架构

使用OpenCV在图片中画矩形框

2017-12-19 18:38 357 查看
一种使用OpenCV在图片中画矩形框的简单方法

import cv2

center=[]

circumference=[]

move=[]

def drawCircle(action, x, y, flags, userdata):

# Referencing global variables

global center, circumference,source,dummy

# Action to be taken when left mouse button is pressed

if action==cv2.EVENT_LBUTTONDOWN:

center=[(x,y)]

# Mark the center

cv2.circle(source, center[0], 1, (255,255,0), 2, cv2.LINE_AA );

# Action to be taken when left mouse button is released

elif action==cv2.EVENT_LBUTTONUP:

circumference=[(x,y)]

cv2.rectangle(source,center[0],circumference[0],(0,255,0),3)

elif action==cv2.EVENT_MOUSEMOVE and flags==cv2.EVENT_FLAG_LBUTTON:

move=[(x,y)]

source= dummy.copy()

cv2.rectangle(source,center[0],move[0],(0,255,0),3)

cv2.imshow("Window",source)

source = cv2.imread('D:/Projects/object_recognition/test/test_1.jpg')

# Make a dummy image, will be useful to clear the drawing

dummy = source.copy()

cv2.namedWindow("Window")

# highgui function called when mouse events occur

cv2.setMouseCallback("Window", drawCircle)

k = 0

# loop until escape character is pressed

while k!=27 :

cv2.imshow("Window", source)

cv2.putText(source,"Choose center, and drag, Press ESC to exit and c to clear" ,(10,30), cv2.FONT_HERSHEY_SIMPLEX, 0.7,(255,255,255), 2 );

k = cv2.waitKey(20) & 0xFF

# Another way of cloning

if k==99:

source= dummy.copy()

cv2.destroyAllWindows()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: