Wednesday, August 5, 2015

用滑鼠再圖片上畫框框

import cv2
import numpy as np
global px,py    

drawing = False # true if mouse is pressed
mode = True # if True, draw rectangle. Press 'm' to toggle to curve
ix,iy = -1,-1

# mouse callback function
def draw_circle(event,x,y,flags,param):
    
    global ix,iy,drawing,mode
    #Click 
    if event == cv2.EVENT_LBUTTONDOWN:
        drawing = True
        ix,iy = x,y
        print "ix=",ix,"iy=",iy

    elif event == cv2.EVENT_LBUTTONUP:
        drawing = False
        if mode == True:
  cv2.rectangle(img,(ix,iy),(x,y),(0,255,255),0)

img = cv2.imread('room.jpg')
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_circle)

while(1):
 
    cv2.imshow('image',img)
    k = cv2.waitKey(1) & 0xFF
    if k == ord('m'):
        mode = not mode
    elif k == 27:
        break

cv2.destroyAllWindows()



No comments:

Post a Comment