Commit b76d3514 authored by Vadim Pisarevsky's avatar Vadim Pisarevsky

Merge pull request #5128 from ageitgey:ag-prevent-demos-from-autorunning

parents 4a2aad5b e976c448
...@@ -95,80 +95,82 @@ def onmouse(event,x,y,flags,param): ...@@ -95,80 +95,82 @@ def onmouse(event,x,y,flags,param):
cv2.circle(img,(x,y),thickness,value['color'],-1) cv2.circle(img,(x,y),thickness,value['color'],-1)
cv2.circle(mask,(x,y),thickness,value['val'],-1) cv2.circle(mask,(x,y),thickness,value['val'],-1)
# print documentation if __name__ == '__main__':
print __doc__
# print documentation
# Loading images print __doc__
if len(sys.argv) == 2:
filename = sys.argv[1] # for drawing purposes # Loading images
else: if len(sys.argv) == 2:
print "No input image given, so loading default image, ../data/lena.jpg \n" filename = sys.argv[1] # for drawing purposes
print "Correct Usage: python grabcut.py <filename> \n" else:
filename = '../data/lena.jpg' print "No input image given, so loading default image, ../data/lena.jpg \n"
print "Correct Usage: python grabcut.py <filename> \n"
img = cv2.imread(filename) filename = '../data/lena.jpg'
img2 = img.copy() # a copy of original image
mask = np.zeros(img.shape[:2],dtype = np.uint8) # mask initialized to PR_BG img = cv2.imread(filename)
output = np.zeros(img.shape,np.uint8) # output image to be shown img2 = img.copy() # a copy of original image
mask = np.zeros(img.shape[:2],dtype = np.uint8) # mask initialized to PR_BG
# input and output windows output = np.zeros(img.shape,np.uint8) # output image to be shown
cv2.namedWindow('output')
cv2.namedWindow('input') # input and output windows
cv2.setMouseCallback('input',onmouse) cv2.namedWindow('output')
cv2.moveWindow('input',img.shape[1]+10,90) cv2.namedWindow('input')
cv2.setMouseCallback('input',onmouse)
print " Instructions: \n" cv2.moveWindow('input',img.shape[1]+10,90)
print " Draw a rectangle around the object using right mouse button \n"
print " Instructions: \n"
while(1): print " Draw a rectangle around the object using right mouse button \n"
cv2.imshow('output',output) while(1):
cv2.imshow('input',img)
k = 0xFF & cv2.waitKey(1) cv2.imshow('output',output)
cv2.imshow('input',img)
# key bindings k = 0xFF & cv2.waitKey(1)
if k == 27: # esc to exit
break # key bindings
elif k == ord('0'): # BG drawing if k == 27: # esc to exit
print " mark background regions with left mouse button \n" break
value = DRAW_BG elif k == ord('0'): # BG drawing
elif k == ord('1'): # FG drawing print " mark background regions with left mouse button \n"
print " mark foreground regions with left mouse button \n" value = DRAW_BG
value = DRAW_FG elif k == ord('1'): # FG drawing
elif k == ord('2'): # PR_BG drawing print " mark foreground regions with left mouse button \n"
value = DRAW_PR_BG value = DRAW_FG
elif k == ord('3'): # PR_FG drawing elif k == ord('2'): # PR_BG drawing
value = DRAW_PR_FG value = DRAW_PR_BG
elif k == ord('s'): # save image elif k == ord('3'): # PR_FG drawing
bar = np.zeros((img.shape[0],5,3),np.uint8) value = DRAW_PR_FG
res = np.hstack((img2,bar,img,bar,output)) elif k == ord('s'): # save image
cv2.imwrite('grabcut_output.png',res) bar = np.zeros((img.shape[0],5,3),np.uint8)
print " Result saved as image \n" res = np.hstack((img2,bar,img,bar,output))
elif k == ord('r'): # reset everything cv2.imwrite('grabcut_output.png',res)
print "resetting \n" print " Result saved as image \n"
rect = (0,0,1,1) elif k == ord('r'): # reset everything
drawing = False print "resetting \n"
rectangle = False rect = (0,0,1,1)
rect_or_mask = 100 drawing = False
rect_over = False rectangle = False
value = DRAW_FG rect_or_mask = 100
img = img2.copy() rect_over = False
mask = np.zeros(img.shape[:2],dtype = np.uint8) # mask initialized to PR_BG value = DRAW_FG
output = np.zeros(img.shape,np.uint8) # output image to be shown img = img2.copy()
elif k == ord('n'): # segment the image mask = np.zeros(img.shape[:2],dtype = np.uint8) # mask initialized to PR_BG
print """ For finer touchups, mark foreground and background after pressing keys 0-3 output = np.zeros(img.shape,np.uint8) # output image to be shown
and again press 'n' \n""" elif k == ord('n'): # segment the image
if (rect_or_mask == 0): # grabcut with rect print """ For finer touchups, mark foreground and background after pressing keys 0-3
bgdmodel = np.zeros((1,65),np.float64) and again press 'n' \n"""
fgdmodel = np.zeros((1,65),np.float64) if (rect_or_mask == 0): # grabcut with rect
cv2.grabCut(img2,mask,rect,bgdmodel,fgdmodel,1,cv2.GC_INIT_WITH_RECT) bgdmodel = np.zeros((1,65),np.float64)
rect_or_mask = 1 fgdmodel = np.zeros((1,65),np.float64)
elif rect_or_mask == 1: # grabcut with mask cv2.grabCut(img2,mask,rect,bgdmodel,fgdmodel,1,cv2.GC_INIT_WITH_RECT)
bgdmodel = np.zeros((1,65),np.float64) rect_or_mask = 1
fgdmodel = np.zeros((1,65),np.float64) elif rect_or_mask == 1: # grabcut with mask
cv2.grabCut(img2,mask,rect,bgdmodel,fgdmodel,1,cv2.GC_INIT_WITH_MASK) bgdmodel = np.zeros((1,65),np.float64)
fgdmodel = np.zeros((1,65),np.float64)
mask2 = np.where((mask==1) + (mask==3),255,0).astype('uint8') cv2.grabCut(img2,mask,rect,bgdmodel,fgdmodel,1,cv2.GC_INIT_WITH_MASK)
output = cv2.bitwise_and(img2,img2,mask=mask2)
mask2 = np.where((mask==1) + (mask==3),255,0).astype('uint8')
cv2.destroyAllWindows() output = cv2.bitwise_and(img2,img2,mask=mask2)
cv2.destroyAllWindows()
...@@ -10,24 +10,25 @@ import cv2 ...@@ -10,24 +10,25 @@ import cv2
import numpy as np import numpy as np
import sys import sys
if __name__ == '__main__':
print __doc__ print __doc__
try: try:
fn = sys.argv[1] fn = sys.argv[1]
except: except:
fn = "../data/board.jpg" fn = "../data/board.jpg"
src = cv2.imread(fn, 1) src = cv2.imread(fn, 1)
img = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) img = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
img = cv2.medianBlur(img, 5) img = cv2.medianBlur(img, 5)
cimg = src.copy() # numpy function cimg = src.copy() # numpy function
circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 10, np.array([]), 100, 30, 1, 30) circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 10, np.array([]), 100, 30, 1, 30)
a, b, c = circles.shape a, b, c = circles.shape
for i in range(b): for i in range(b):
cv2.circle(cimg, (circles[0][i][0], circles[0][i][1]), circles[0][i][2], (0, 0, 255), 3, cv2.LINE_AA) cv2.circle(cimg, (circles[0][i][0], circles[0][i][1]), circles[0][i][2], (0, 0, 255), 3, cv2.LINE_AA)
cv2.circle(cimg, (circles[0][i][0], circles[0][i][1]), 2, (0, 255, 0), 3, cv2.LINE_AA) # draw center of circle cv2.circle(cimg, (circles[0][i][0], circles[0][i][1]), 2, (0, 255, 0), 3, cv2.LINE_AA) # draw center of circle
cv2.imshow("source", src) cv2.imshow("source", src)
cv2.imshow("detected circles", cimg) cv2.imshow("detected circles", cimg)
cv2.waitKey(0) cv2.waitKey(0)
...@@ -9,35 +9,37 @@ import numpy as np ...@@ -9,35 +9,37 @@ import numpy as np
import sys import sys
import math import math
try: if __name__ == '__main__':
fn = sys.argv[1]
except:
fn = "../data/pic1.png"
print __doc__
src = cv2.imread(fn)
dst = cv2.Canny(src, 50, 200)
cdst = cv2.cvtColor(dst, cv2.COLOR_GRAY2BGR)
if True: # HoughLinesP try:
lines = cv2.HoughLinesP(dst, 1, math.pi/180.0, 40, np.array([]), 50, 10) fn = sys.argv[1]
a,b,c = lines.shape except:
for i in range(a): fn = "../data/pic1.png"
cv2.line(cdst, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (0, 0, 255), 3, cv2.LINE_AA) print __doc__
src = cv2.imread(fn)
dst = cv2.Canny(src, 50, 200)
cdst = cv2.cvtColor(dst, cv2.COLOR_GRAY2BGR)
else: # HoughLines if True: # HoughLinesP
lines = cv2.HoughLines(dst, 1, math.pi/180.0, 50, np.array([]), 0, 0) lines = cv2.HoughLinesP(dst, 1, math.pi/180.0, 40, np.array([]), 50, 10)
a,b,c = lines.shape a,b,c = lines.shape
for i in range(a): for i in range(a):
rho = lines[i][0][0] cv2.line(cdst, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (0, 0, 255), 3, cv2.LINE_AA)
theta = lines[i][0][1]
a = math.cos(theta)
b = math.sin(theta)
x0, y0 = a*rho, b*rho
pt1 = ( int(x0+1000*(-b)), int(y0+1000*(a)) )
pt2 = ( int(x0-1000*(-b)), int(y0-1000*(a)) )
cv2.line(cdst, pt1, pt2, (0, 0, 255), 3, cv2.LINE_AA)
else: # HoughLines
lines = cv2.HoughLines(dst, 1, math.pi/180.0, 50, np.array([]), 0, 0)
a,b,c = lines.shape
for i in range(a):
rho = lines[i][0][0]
theta = lines[i][0][1]
a = math.cos(theta)
b = math.sin(theta)
x0, y0 = a*rho, b*rho
pt1 = ( int(x0+1000*(-b)), int(y0+1000*(a)) )
pt2 = ( int(x0-1000*(-b)), int(y0-1000*(a)) )
cv2.line(cdst, pt1, pt2, (0, 0, 255), 3, cv2.LINE_AA)
cv2.imshow("source", src)
cv2.imshow("detected lines", cdst) cv2.imshow("source", src)
cv2.waitKey(0) cv2.imshow("detected lines", cdst)
cv2.waitKey(0)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment