python+opencv 画矩形

#-*-conding:utf-8-*-

import cv2

src_img = cv2.imread('src1.jpg')
img = src_img.copy()
tmp = src_img.copy()

def draw_rectangle(event, x, y, flags, param):
    global ix, iy, img, tmp
    if event==cv2.EVENT_LBUTTONDOWN:
        img = src_img.copy()
        ix,iy = x, y
        cv2.circle(img,(ix, iy), 3, (0,0,255), -1)
        cv2.imshow('calib', img)
    elif (event==cv2.EVENT_MOUSEMOVE and (flags ==cv2.EVENT_FLAG_LBUTTON)):
        tmp = img.copy()
        cv2.rectangle(tmp,(ix,iy),(x,y), (0,0,255), 1)
        cv2.imshow('calib', tmp)
    elif event==cv2.EVENT_LBUTTONUP:
        img = src_img.copy()
        cv2.rectangle(img,(ix,iy),(x,y), (0,255,0), 1)
        cv2.imshow('calib', img)
        tmp = img.copy()

if __name__=='__main__':

    cv2.namedWindow('calib', 0)
    cv2.setMouseCallback('calib', draw_rectangle,0)
    cv2.imshow('calib', img)
    cv2.waitKey(0)

 

你可能感兴趣的:(OpenCV,python)