python: 'cv2.' -> 'cv.' via 'import cv2 as cv'
This commit is contained in:
+28
-28
@@ -31,7 +31,7 @@ Key 's' - To save the results
|
||||
from __future__ import print_function
|
||||
|
||||
import numpy as np
|
||||
import cv2
|
||||
import cv2 as cv
|
||||
import sys
|
||||
|
||||
BLUE = [255,0,0] # rectangle color
|
||||
@@ -58,45 +58,45 @@ def onmouse(event,x,y,flags,param):
|
||||
global img,img2,drawing,value,mask,rectangle,rect,rect_or_mask,ix,iy,rect_over
|
||||
|
||||
# Draw Rectangle
|
||||
if event == cv2.EVENT_RBUTTONDOWN:
|
||||
if event == cv.EVENT_RBUTTONDOWN:
|
||||
rectangle = True
|
||||
ix,iy = x,y
|
||||
|
||||
elif event == cv2.EVENT_MOUSEMOVE:
|
||||
elif event == cv.EVENT_MOUSEMOVE:
|
||||
if rectangle == True:
|
||||
img = img2.copy()
|
||||
cv2.rectangle(img,(ix,iy),(x,y),BLUE,2)
|
||||
cv.rectangle(img,(ix,iy),(x,y),BLUE,2)
|
||||
rect = (min(ix,x),min(iy,y),abs(ix-x),abs(iy-y))
|
||||
rect_or_mask = 0
|
||||
|
||||
elif event == cv2.EVENT_RBUTTONUP:
|
||||
elif event == cv.EVENT_RBUTTONUP:
|
||||
rectangle = False
|
||||
rect_over = True
|
||||
cv2.rectangle(img,(ix,iy),(x,y),BLUE,2)
|
||||
cv.rectangle(img,(ix,iy),(x,y),BLUE,2)
|
||||
rect = (min(ix,x),min(iy,y),abs(ix-x),abs(iy-y))
|
||||
rect_or_mask = 0
|
||||
print(" Now press the key 'n' a few times until no further change \n")
|
||||
|
||||
# draw touchup curves
|
||||
|
||||
if event == cv2.EVENT_LBUTTONDOWN:
|
||||
if event == cv.EVENT_LBUTTONDOWN:
|
||||
if rect_over == False:
|
||||
print("first draw rectangle \n")
|
||||
else:
|
||||
drawing = True
|
||||
cv2.circle(img,(x,y),thickness,value['color'],-1)
|
||||
cv2.circle(mask,(x,y),thickness,value['val'],-1)
|
||||
cv.circle(img,(x,y),thickness,value['color'],-1)
|
||||
cv.circle(mask,(x,y),thickness,value['val'],-1)
|
||||
|
||||
elif event == cv2.EVENT_MOUSEMOVE:
|
||||
elif event == cv.EVENT_MOUSEMOVE:
|
||||
if drawing == True:
|
||||
cv2.circle(img,(x,y),thickness,value['color'],-1)
|
||||
cv2.circle(mask,(x,y),thickness,value['val'],-1)
|
||||
cv.circle(img,(x,y),thickness,value['color'],-1)
|
||||
cv.circle(mask,(x,y),thickness,value['val'],-1)
|
||||
|
||||
elif event == cv2.EVENT_LBUTTONUP:
|
||||
elif event == cv.EVENT_LBUTTONUP:
|
||||
if drawing == True:
|
||||
drawing = False
|
||||
cv2.circle(img,(x,y),thickness,value['color'],-1)
|
||||
cv2.circle(mask,(x,y),thickness,value['val'],-1)
|
||||
cv.circle(img,(x,y),thickness,value['color'],-1)
|
||||
cv.circle(mask,(x,y),thickness,value['val'],-1)
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -111,25 +111,25 @@ if __name__ == '__main__':
|
||||
print("Correct Usage: python grabcut.py <filename> \n")
|
||||
filename = '../data/lena.jpg'
|
||||
|
||||
img = cv2.imread(filename)
|
||||
img = cv.imread(filename)
|
||||
img2 = img.copy() # a copy of original image
|
||||
mask = np.zeros(img.shape[:2],dtype = np.uint8) # mask initialized to PR_BG
|
||||
output = np.zeros(img.shape,np.uint8) # output image to be shown
|
||||
|
||||
# input and output windows
|
||||
cv2.namedWindow('output')
|
||||
cv2.namedWindow('input')
|
||||
cv2.setMouseCallback('input',onmouse)
|
||||
cv2.moveWindow('input',img.shape[1]+10,90)
|
||||
cv.namedWindow('output')
|
||||
cv.namedWindow('input')
|
||||
cv.setMouseCallback('input',onmouse)
|
||||
cv.moveWindow('input',img.shape[1]+10,90)
|
||||
|
||||
print(" Instructions: \n")
|
||||
print(" Draw a rectangle around the object using right mouse button \n")
|
||||
|
||||
while(1):
|
||||
|
||||
cv2.imshow('output',output)
|
||||
cv2.imshow('input',img)
|
||||
k = cv2.waitKey(1)
|
||||
cv.imshow('output',output)
|
||||
cv.imshow('input',img)
|
||||
k = cv.waitKey(1)
|
||||
|
||||
# key bindings
|
||||
if k == 27: # esc to exit
|
||||
@@ -147,7 +147,7 @@ if __name__ == '__main__':
|
||||
elif k == ord('s'): # save image
|
||||
bar = np.zeros((img.shape[0],5,3),np.uint8)
|
||||
res = np.hstack((img2,bar,img,bar,output))
|
||||
cv2.imwrite('grabcut_output.png',res)
|
||||
cv.imwrite('grabcut_output.png',res)
|
||||
print(" Result saved as image \n")
|
||||
elif k == ord('r'): # reset everything
|
||||
print("resetting \n")
|
||||
@@ -166,14 +166,14 @@ if __name__ == '__main__':
|
||||
if (rect_or_mask == 0): # grabcut with rect
|
||||
bgdmodel = np.zeros((1,65),np.float64)
|
||||
fgdmodel = np.zeros((1,65),np.float64)
|
||||
cv2.grabCut(img2,mask,rect,bgdmodel,fgdmodel,1,cv2.GC_INIT_WITH_RECT)
|
||||
cv.grabCut(img2,mask,rect,bgdmodel,fgdmodel,1,cv.GC_INIT_WITH_RECT)
|
||||
rect_or_mask = 1
|
||||
elif rect_or_mask == 1: # grabcut with mask
|
||||
bgdmodel = np.zeros((1,65),np.float64)
|
||||
fgdmodel = np.zeros((1,65),np.float64)
|
||||
cv2.grabCut(img2,mask,rect,bgdmodel,fgdmodel,1,cv2.GC_INIT_WITH_MASK)
|
||||
cv.grabCut(img2,mask,rect,bgdmodel,fgdmodel,1,cv.GC_INIT_WITH_MASK)
|
||||
|
||||
mask2 = np.where((mask==1) + (mask==3),255,0).astype('uint8')
|
||||
output = cv2.bitwise_and(img2,img2,mask=mask2)
|
||||
output = cv.bitwise_and(img2,img2,mask=mask2)
|
||||
|
||||
cv2.destroyAllWindows()
|
||||
cv.destroyAllWindows()
|
||||
|
||||
Reference in New Issue
Block a user