python-opencv webcam ["noneType"] -
few weeks ago tryed move mouse pointer using python , opencv ...i didnt had time , today accidentally found piece of code doing problem cant open webcam anymore opencv ...
i`m using ubuntu 10.04 ... /dev/video0 working can luvcview -d /dev/video0
but when camera = highgui.cvcreatecapturecamera(0) , try type(camera) nonetype ...
i`ve apt-get remove --purge python-opencv , reinstalled ...but cant make work ... dont know whats wrong few weeks ago worked , ...
here`s code controling mouse python opencv , xlib ...
#! /usr/bin/env python print "opencv python version of lkdemo" import sys import os sys.path.insert(1, os.path.join(sys.path[0], '..')) xlib import x, display, xutil # import necessary things opencv opencv import cv opencv import highgui ############################################################################# # "constants" win_size = 10 max_count = 500 ############################################################################# # "global" variables image = none pt = none add_remove_pt = false flags = 0 night_mode = false need_to_init = false ############################################################################# # mouse callback # callback on trackbar class window: def __init__(self, display): self.d = display self.objects = [] # find screen open window on self.screen = self.d.screen() self.window = self.screen.root.create_window( 50, 50, 300, 200, 2, self.screen.root_depth, x.inputoutput, x.copyfromparent, # special attribute values background_pixel = self.screen.white_pixel, event_mask = (x.exposuremask | x.structurenotifymask | x.buttonpressmask | x.buttonreleasemask | x.button1motionmask), colormap = x.copyfromparent, ) self.gc = self.window.create_gc( foreground = self.screen.black_pixel, background = self.screen.white_pixel, ) def mousekiller(self): self.gc = self.window.warp_pointer (the_point.x*6, the_point.y*6, src_window = 0, src_x = 0, src_y = 0, src_width = 0, src_height = 0, onerror = none ) # set wm info self.wm_delete_window = self.d.intern_atom('wm_delete_window') self.wm_protocols = self.d.intern_atom('wm_protocols') self.window.set_wm_name('xlib example: draw.py') self.window.set_wm_icon_name('draw.py') self.window.set_wm_class('draw', 'xlibexample') self.window.set_wm_protocols([self.wm_delete_window]) self.window.set_wm_hints(flags = xutil.statehint, initial_state = xutil.normalstate) self.window.set_wm_normal_hints(flags = (xutil.pposition | xutil.psize | xutil.pminsize), min_width = 20, min_height = 20) # map window, making visible self.window.map() def on_mouse (event, x, y, flags, param): # use global pt , add_remove_pt global pt global add_remove_pt if image none: return if event == highgui.cv_event_lbuttondown: # user has click, memorize pt = cv.cvpoint (x, y) add_remove_pt = true ############################################################################# # so, here main part of program if __name__ == '__main__': window(display.display()) capture = highgui.cvcreatecameracapture(-1) # check capture device ok if not capture: print "error opening capture device" sys.exit (1) # display small howto use print "hot keys: \n" \ "\tesc - quit program\n" \ "\tr - auto-initialize tracking\n" \ "\tc - delete points\n" \ "\tn - switch \"night\" mode on/off\n" \ "to add/remove feature point click it\n" # first, create necessary windows highgui.cvnamedwindow ('lkdemo', highgui.cv_window_autosize) # register mouse callback highgui.cvsetmousecallback ('lkdemo', on_mouse, none) while 1: # forever # 1. capture current image frame = highgui.cvqueryframe (capture) if frame none: # no image captured... end processing break if image none: # create images need image = cv.cvcreateimage (cv.cvgetsize (frame), 8, 3) image.origin = frame.origin grey = cv.cvcreateimage (cv.cvgetsize (frame), 8, 1) prev_grey = cv.cvcreateimage (cv.cvgetsize (frame), 8, 1) pyramid = cv.cvcreateimage (cv.cvgetsize (frame), 8, 1) prev_pyramid = cv.cvcreateimage (cv.cvgetsize (frame), 8, 1) points = [[], []] # copy frame, can draw on cv.cvcopy (frame, image) # create grey version of image cv.cvcvtcolor (image, grey, cv.cv_bgr2gray) if night_mode: # night mode: display points cv.cvsetzero (image) if need_to_init: # want search points # create wanted images eig = cv.cvcreateimage (cv.cvgetsize (grey), 32, 1) temp = cv.cvcreateimage (cv.cvgetsize (grey), 32, 1) # default parameters quality = 0.01 min_distance = 10 # search points points [1] = cv.cvgoodfeaturestotrack ( grey, eig, temp, max_count, quality, min_distance, none, 3, 0, 0.04) # refine corner locations cv.cvfindcornersubpix ( grey, points [1], cv.cvsize (win_size, win_size), cv.cvsize (-1, -1), cv.cvtermcriteria (cv.cv_termcrit_iter | cv.cv_termcrit_eps, 20, 0.03)) # release temporary images cv.cvreleaseimage (eig) cv.cvreleaseimage (temp) elif len (points [0]) > 0: # have points, display them # calculate optical flow points [1], status = cv.cvcalcopticalflowpyrlk ( prev_grey, grey, prev_pyramid, pyramid, points [0], len (points [0]), cv.cvsize (win_size, win_size), 3, len (points [0]), none, cv.cvtermcriteria (cv.cv_termcrit_iter|cv.cv_termcrit_eps, 20, 0.03), flags) # initializations point_counter = -1 new_points = [] the_point in points [1]: # go trough points # increment counter point_counter += 1 if add_remove_pt: # have point add, see if close # one. if yes, don't use dx = pt.x - the_point.x dy = pt.y - the_point.y if dx * dx + dy * dy <= 25: # close add_remove_pt = 0 continue if not status [point_counter]: # disable point continue # point correct point new_points.append (the_point) # draw current point cv.cvcircle (image, [the_point.x, the_point.y], 3, cv.cvscalar (0, 255, 0, 0), -1, 8, 0) window(display.display()).mousekiller() # set points keep points [1] = new_points if add_remove_pt: # want add point points [1].append (cv.cvpointto32f (pt)) # refine corner locations points [1][-1] = cv.cvfindcornersubpix ( grey, [points [1][-1]], cv.cvsize (win_size, win_size), cv.cvsize (-1, -1), cv.cvtermcriteria (cv.cv_termcrit_iter | cv.cv_termcrit_eps, 20, 0.03))[0] # no more in "add_remove_pt" mode add_remove_pt = false # swapping prev_grey, grey = grey, prev_grey prev_pyramid, pyramid = pyramid, prev_pyramid points [0], points [1] = points [1], points [0] need_to_init = false # can display image highgui.cvshowimage ('lkdemo', image) # handle events c = highgui.cvwaitkey (10) if c == '\x1b': # user has press esc key, exit break # processing depending on character if c in ['r', 'r']: need_to_init = true elif c in ['c', 'c']: points = [[], []] elif c in ['n', 'n']: night_mode = not night_mode
dont know anymore ... hope had problem , fixed ... thx in advance.
if understand correctly you're trying mouse feedback work.
in order this, need make "responder" function (the "onmouse" function in documentation below). used cv2.setmousecallback function.
for example:
def mousehandle(event, x, y, flag, param): if (flag == 1): print "hoosier at" + str(x) + " , " + str(y);
you need define flag values in code well, example:
cv_event_lbuttondown = 1;
now in main function:
im = cv2.imread('myimage.jpg') cv2.imshow('image', im) cv2.setmousecallback('image', mousehandle, cv_event_lbuttondown)
for reference, here function information opencv documentation: http://opencv.willowgarage.com/documentation/python/highgui_user_interface.html
setmousecallback(windowname, onmouse, param) → none assigns callback mouse events.
parameters: windowname (str) – name of window.
onmouse (pycallableobject) – callable called every time mouse event occurs in specified window. callable should have signature foo(event, x, y, flags, param)-> none
event 1 of cv_event_* , x , y coordinates of mouse pointer in image coordinates (not window coordinates), flags combination of cv_event_flag_* , , param user-defined parameter passed cvsetmousecallback function call.
param (object) – user-defined parameter passed callback function. function cvsetmousecallback sets callback function mouse events occuring within specified window.
Comments
Post a Comment