LOW = 140 try: # extract circles cv.HoughCircles(processed, storage, cv.CV_HOUGH_GRADIENT, 2, 32.0, HIGH, LOW) for i in range(0, len(np.asarray(storage))): print "circle #%d" %i Radius = int(np.asarray(storage)[i][0][2]) x = int(np.asarray(storage)[i][0][0]) y = int(np.asarray(storage)[i][0][1]) center = (x, y) # green dot on center and red circle around cv.Circle(orig, center, 1, cv.CV_RGB(0, 255, 0), -1, 8, 0) cv.Circle(orig, center, Radius, cv.CV_RGB(255, 0, 0), 3, 8, 0) cv.Circle(processed, center, 1, cv.CV_RGB(0, 255, 0), -1, 8, 0) cv.Circle(processed, center, Radius, cv.CV_RGB(255, 0, 0), 3, 8, 0) except: print "nothing found" pass # show images cv.ShowImage("image - press 'q' to quit", orig) cv.ShowImage("post-process", processed) cv_key = cv.WaitKey(WAITKEY_DELAY_MS) key_pressed = chr(cv_key & 255)
imwrite('scene_r.bmp', image) # *** GP: from here until the end (except user line) # Load each image in grey left = cv.LoadImage('scene_l.bmp',cv.CV_LOAD_IMAGE_GRAYSCALE) right = cv.LoadImage('scene_r.bmp',cv.CV_LOAD_IMAGE_GRAYSCALE) # Initialize matrices which will store the result of the stereo correspondence. disparity_left = cv.CreateMat(left.height, left.width, cv.CV_16S) disparity_right = cv.CreateMat(left.height, left.width, cv.CV_16S) # data structure initialization state = cv.CreateStereoGCState(16,2) # running the graph-cut algorithm cv.FindStereoCorrespondenceGC(left,right,disparity_left,disparity_right,state) disp_left_visual = cv.CreateMat(left.height, left.width, cv.CV_8U) cv.ConvertScale(disparity_left,disp_left_visual,-16) # cutting the object farthest of a threshold cut(disp_left_visual,left,80) # *** USER: Save image if you want to cv.SaveImage("Binocular_vision.jpg", disp_left_visual) # Show result # *** USER: change the name of the window that will show the result. cv.NamedWindow('Disparity map',cv.CV_WINDOW_AUTOSIZE) cv.ShowImage('Disparity map',disp_left_visual) cv.WaitKey()
def hough_it(self, n_ball, iteration): # create gray scale image of balls gray_image = cv.CreateImage((self.width, self.height), 8, 1) cv.CvtColor(self.cv_image, gray_image, cv.CV_BGR2GRAY) # create gray scale array of balls gray_array = self.cv2array(gray_image) # find Hough circles circles = cv2.HoughCircles(gray_array, cv.CV_HOUGH_GRADIENT, 1, 40, param1=50, \ param2=self.hough_accumulator, minRadius=self.hough_min_radius, \ maxRadius=self.hough_max_radius) # Check for at least one ball found if circles is None: # display no balls found message on head display self.splash_screen("no balls", "found") # no point in continuing so exit with error message sys.exit("ERROR - hough_it - No golf balls found") circles = numpy.uint16(numpy.around(circles)) ball_data = {} n_balls = 0 circle_array = numpy.asarray(self.cv_image) # check if golf ball is in ball tray for i in circles[0, :]: # convert to baxter coordinates ball = self.pixel_to_baxter((i[0], i[1]), self.tray_distance) if self.is_near_ball_tray(ball): # draw the outer circle in red cv2.circle(circle_array, (i[0], i[1]), i[2], (0, 0, 255), 2) # draw the center of the circle in red cv2.circle(circle_array, (i[0], i[1]), 2, (0, 0, 255), 3) elif i[1] > 800: # draw the outer circle in red cv2.circle(circle_array, (i[0], i[1]), i[2], (0, 0, 255), 2) # draw the center of the circle in red cv2.circle(circle_array, (i[0], i[1]), 2, (0, 0, 255), 3) else: # draw the outer circle in green cv2.circle(circle_array, (i[0], i[1]), i[2], (0, 255, 0), 2) # draw the center of the circle in green cv2.circle(circle_array, (i[0], i[1]), 2, (0, 255, 0), 3) ball_data[n_balls] = (i[0], i[1], i[2]) n_balls += 1 circle_image = cv.fromarray(circle_array) cv.ShowImage("Hough Circle", circle_image) # 3ms wait cv.WaitKey(3) # display image on head monitor font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1.0, 1.0, 1) position = (30, 60) s = "Searching for golf balls" cv.PutText(circle_image, s, position, font, self.white) msg = cv_bridge.CvBridge().cv_to_imgmsg(circle_image, encoding="bgr8") self.pub.publish(msg) if self.save_images: # save image of Hough circles on raw image file_name = self.image_dir \ + "hough_circle_" + str(n_ball) + "_" + str(iteration) + ".jpg" cv.SaveImage(file_name, circle_image) # Check for at least one ball found if n_balls == 0: # no balls found # display no balls found message on head display self.splash_screen("no balls", "found") # less than 12 balls found, no point in continuing, exit with error message sys.exit("ERROR - hough_it - No golf balls found") # select next ball and find it's position next_ball = self.find_next_golf_ball(ball_data, iteration) # find best gripper angle to avoid touching neighbouring ball angle = self.find_gripper_angle(next_ball, ball_data) # return next golf ball position and pickup angle return next_ball, angle
cv.Split( image, cv.CreateImage(cv.GetSize(image), 8, 1), channelG, channelB, None ) # Splits the input image into channels (RGB). I only use B and G because the laser is green, not red. cv.Sub( channelG, channelB, imageThreshold ) # Subtracts the channels. Since the green laser's pixels are basically have a G value of 255, they stand out. Uncomment the next two lines to see what I mean. #cv.ShowImage('Test', imageThreshold) #cv.WaitKey(0) cv.InRangeS( imageThreshold, cv.Scalar(24, 24, 24), cv.Scalar(255, 255, 255), imageThreshold ) # The channel seperation doesn't make things bitonal. This is something I need to fix, as it depends on a fixed value, but this just thresholds everything between those RGB values into white. cv.ShowImage('Test', imageThreshold) cv.WaitKey(0) cv.Smooth( imageThreshold, imageThreshold, smoothtype=cv.CV_MEDIAN, param1=9 ) # This median smoothing filter is really handy. It averages the values of each pixel within the window, so any noise gets truned into black (the image is bitonal, so if it's not white, it's black). cv.ShowImage('Test', imageThreshold) cv.WaitKey(0) image = Image.fromstring( 'L', cv.GetSize(imageThreshold), imageThreshold.tostring() ) # I have to get rid of this dependency to Python's imaging library, but this just re-inits the thresholded image. pixels = image.load() # Loads the pixel data, as this object is iterable. # Extraction of data points happens below
image, (pt1[0], pt1[1], pt2[0] - pt1[0], int((pt2[1] - pt1[1]) * 0.7))) eyes = cv.HaarDetectObjects(image, eyeCascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, (15, 15)) if eyes: # For each eye found for eye in eyes: # Draw a rectangle around the eye cv.Rectangle(image, (eye[0][0], eye[0][1]), (eye[0][0] + eye[0][2], eye[0][1] + eye[0][3]), cv.RGB(255, 0, 0), 1, 8, 0) cv.ResetImageROI(image) return image faceCascade = cv.Load( "/usr/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml") eyeCascade = cv.Load("/usr/share/OpenCV/haarcascades/haarcascade_eye.xml") while True: img = cv.QueryFrame(capture) image = DetectRedEyes(img, faceCascade, eyeCascade) cv.ShowImage("camera", image) k = cv.WaitKey(10) if k == 'f': break
######################################################### # PYTHON - Bahcesehir University Game - GH0ST S0FTWARE # ######################################################### # CONTACT # ######################################################### # DEVELOPER : İSMAİL TAŞDELEN # # Mail Address : [email protected] # # LINKEDIN : https://www.linkedin.com/in/ismailtasdelen # # Whatsapp : + 90 534 295 94 31 # ######################################################### """ ESC = 27 def WaitKey(delay=0): c = cv.WaitKey(delay) if c == -1: ret = -1 else: ret = c & ~0b100000000000000000000 return ret if __name__ == '__main__': cv.NamedWindow("GH0ST-S0FTWARE", cv.CV_WINDOW_AUTOSIZE) cv.ShowImage("GH0ST-S0FTWARE", None) c = cv.WaitKey() print "%d - %d" % (c & ~0b100000000000000000000, c)
elif t == cv.IPL_DEPTH_16U: print("Unsigned 16-bit integer") elif t == cv.IPL_DEPTH_16S: print("Signed 16-bit integer") elif t == cv.IPL_DEPTH_32S: print("Signed 32-bit integer") elif t == cv.IPL_DEPTH_32F: print("Single-precision floating point") elif t == cv.IPL_DEPTH_64F: print("Double-precision floating point") else: print("Unknown type") if (len(sys.argv) == 1): print "Usage: Helloworld.py inputimage {outputimage}" print "" print "Will either display the image, or resave it under a new name" else: myimage = cv.LoadImage(sys.argv[1]) print "Width: %d Height %d Channels: %d " % (myimage.height, myimage.width, myimage.nChannels) print_image_type(myimage.depth) if len(sys.argv) > 2: cv.SaveImage(sys.argv[2], myimage) else: cv.NamedWindow(sys.argv[1]) cv.ShowImage(sys.argv[1], myimage) cv.WaitKey(0)
import cv import numpy IMAGE="data/fish_ss.png" img = cv.LoadImageM(IMAGE, cv.CV_LOAD_IMAGE_GRAYSCALE) eig_image = cv.CreateMat(img.rows, img.cols, cv.CV_32FC1) temp_image = cv.CreateMat(img.rows, img.cols, cv.CV_32FC1) for (x,y) in cv.GoodFeaturesToTrack(img, eig_image, temp_image, 15, 0.54, 1.0, useHarris = False): print "good feature at", x,y cv.Circle(img, (int(x),int(y)), 7, cv.RGB(250, 7, 10), 2) cv.ShowImage("foo", img) cv.WaitKey()
def afficher(self, nom=None): """Affiche le Tableau, comme une image, dans une fenêtre Utilise OpenCV pour cela""" if nom is None: nom = self.nom cv.ShowImage(nom, self.vers_iplimage())
def pokaz_podglad_dzaialania_algorytmu(self, gray): cv.NamedWindow("PODGLAD DZIALANIA ALGORYTMU", 1) cv.ShowImage("PODGLAD DZIALANIA ALGORYTMU", gray) cv.WaitKey(0)
cv.CV_RGB(255, 255, 255)) if PLAY: #si se reproduce automaticamente, aumentar los frames y cambiar el estado current_frame += 1 text = "Estado Actual: (Play)" else: text = "Estado Actual: (Pausa)" #condiciones para evitar un error al llegar al frame -1 o NFRAMES + 1 if current_frame < 0.0: print "Se llego al principio del video, no se puede ir mas atras" current_frame = 0.0 elif current_frame >= NFRAMES - 1: print "Final del video, no se puede ir mas adelante" current_frame = NFRAMES - 2 break else: #si se designo un archivo de salida, escribimos ahi el frame actual y avanzamos if VIDEO_WRITER: cv.WriteFrame(VIDEO_WRITER, output) current_frame += 1 cv.PutText(output, text, (5, 15), TEXT_FONT, cv.CV_RGB(255, 255, 255)) cv.ShowImage("Deteccion de Movimiento", output) #mostrar los resultados en el feed de video #FIN = time.time() #print "%s, %s"%(muestra, FIN - INICIO) #muestra += 1 #prom += FIN - INICIO print "Fin" #print "Promedio: %s"%(prom/float(muestra))
def display(self): tmp = clone_image(self.img) overlay_text_on_image(tmp, "%s - %s" % (self.card_name, self.set_name), self.font) cv.ShowImage("match", tmp) cv.ResizeWindow("match", 800, 600)
#!/usr/bin/python # # OpenCV "Hello, world" # http://www.neuroforge.co.uk/index.php/getting-started-with-python-a-opencv # # loads an image from a file, adds some text, # then displays the image and saves it to a file import cv # create a window cv.NamedWindow('a_window', cv.CV_WINDOW_AUTOSIZE) # load an image from the filesystem image = cv.LoadImage('picture.png', cv.CV_LOAD_IMAGE_COLOR) # create some text using the given font, and place the text on the image font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 3, 8) x = 0 y = 60 cv.PutText(image, "Hello World!!!", (x, y), font, 255) # put the image in the window cv.ShowImage('a_window', image) # show the window on the screen cv.WaitKey(10000) # save the image to the filesystem cv.SaveImage('image.png', image)
import cv # Initialize the camera capture = cv.CaptureFromCAM(0) # 0 -> index of camera if capture: # Camera initialized without any errors cv.NamedWindow("cam-test", cv.CV_WINDOW_AUTOSIZE) f = cv.QueryFrame(capture) # capture the frame if f: cv.ShowImage("cam-test", f) cv.WaitKey(0) cv.DestroyWindow("cam-test")
import cv vidFile = cv.CaptureFromFile( '/Users/amruthvenkatraman/Movies/the.legend.of.korra.s02e09.hdtv.x264-w4f.mp4' ) nFrames = int(cv.GetCaptureProperty(vidFile, cv.CV_CAP_PROP_FRAME_COUNT)) fps = cv.GetCaptureProperty(vidFile, cv.CV_CAP_PROP_FPS) waitPerFrameInMillisec = int(1 / fps * 1000 / 1) print 'Num. Frames = ', nFrames print 'Frame Rate = ', fps, ' frames per sec' for f in xrange(nFrames): frameImg = cv.QueryFrame(vidFile) cv.ShowImage("My Video Window", frameImg) cv.WaitKey(waitPerFrameInMillisec) # When playing is done, delete the window # NOTE: this step is not strictly necessary, # when the script terminates it will close all windows it owns anyways cv.DestroyWindow("My Video Window")
def temp_highlight(self, pt, landmark=True): color = cv.CV_RGB(0, 255, 255) newimg = cv.CloneImage(self.img) cv.Circle(newimg, pt, 5, color, -1) cv.ShowImage("Annotator", newimg)
def run(self): image = None MAX_COUNT = 500 win_size = (32, 32) line_draw = 2 frame = cv.QueryFrame(self.capture) image = cv.CreateImage(cv.GetSize(frame), 8, 3) image.origin = frame.origin grey = cv.CreateImage(cv.GetSize(frame), 8, 1) edges = cv.CreateImage(cv.GetSize(frame), 8, 1) prev_grey = cv.CreateImage(cv.GetSize(frame), 8, 1) prev_grey2 = cv.CreateImage(cv.GetSize(frame), 8, 1) prev_grey3 = cv.CreateImage(cv.GetSize(frame), 8, 1) pyramid = cv.CreateImage(cv.GetSize(frame), 8, 1) prev_pyramid = cv.CreateImage(cv.GetSize(frame), 8, 1) points = [] prev_points = [] count = 0 criteria = (cv.CV_TERMCRIT_ITER | cv.CV_TERMCRIT_EPS, 20, 0.03) while True: frame = cv.QueryFrame(self.capture) # cv.Rectangle( frame, self.last_rect[0], self.last_rect[1], cv.CV_RGB(255,0,0), 3, cv.CV_AA, 0 ) # cv.Smooth(frame, frame, cv.CV_GAUSSIAN, 15, 0) cv.Copy(frame, image) cv.CvtColor(image, grey, cv.CV_BGR2GRAY) if count == 0: eig = cv.CreateImage(cv.GetSize(grey), 32, 1) temp = cv.CreateImage(cv.GetSize(grey), 32, 1) quality = 0.01 min_distance = 10 points = cv.GoodFeaturesToTrack(grey, eig, temp, MAX_COUNT, quality, min_distance, None, 3, 0, 0.04) points = cv.FindCornerSubPix(grey, points, win_size, (-1, -1), criteria) else: flags = 0 points, status, track_error = cv.CalcOpticalFlowPyrLK( prev_grey, grey, prev_pyramid, pyramid, prev_points, win_size, 2, criteria, flags) diff_points = [] for i, j in enumerate(points): print j if not j == prev_points[i]: diff_points.append(j) print 'len %d' % len(diff_points) prev_points == points count = len(points) print count prev_grey = grey prev_pyramid = pyramid prev_points = points if line_draw: cv.Canny(grey, edges, 30, 150, 3) if line_draw == 1: cv.CvtColor(edges, image, cv.CV_GRAY2BGR) elif line_draw > 1: cv.Merge(edges, prev_grey2, prev_grey3, None, image) cv.Copy(prev_grey2, prev_grey3, None) cv.Copy(edges, prev_grey2, None) cv.ShowImage("Target", image) # Listen for ESC key c = cv.WaitKey(7) % 0x100 if c == 27: break
def showImage(self): cv.ShowImage("Annotator", self.img)
def snapL(self, L): for i, img in enumerate(L): cv.NamedWindow("snap-%d" % i, 1) cv.ShowImage("snap-%d" % i, img) cv.WaitKey() cv.DestroyAllWindows()
def find_object(img, colour): ''' Finds the objects in an image with given colour. Arguments: img -- the image to be processed colour -- the colour to look for (red, blue or yellow) Returns: Point representing object's centre of mass ''' # Convert to hsv size = cv.GetSize(img) tempImage = cv.CreateImage((size[0] / 2, size[1] / 2), 8, 3) # Reduce noise by down- and up-scaling input image cv.PyrDown(img, tempImage, 7) cv.PyrUp(tempImage, img, 7) hsv = cv.CreateImage(size, cv.IPL_DEPTH_8U, 3) cv.CvtColor(img, hsv, cv.CV_BGR2HSV) # Convert to binary image based on colour mask = cv.CreateMat(size[1], size[0], cv.CV_8UC1) maskSize = cv.GetSize(mask) if (colour == "RED"): redLower = cv.Scalar(mods[0] * 256, mods[1] * 256, mods[2] * 256) redUpper = cv.Scalar(mods[3] * 256, mods[4] * 256, mods[5] * 256) cv.InRangeS(hsv, redLower, redUpper, mask) cv.ShowImage("Red:", mask) elif (colour == "BLUE"): blueLower = cv.Scalar(mods[6] * 256, mods[7] * 256, mods[8] * 256) blueUpper = cv.Scalar(mods[9] * 256, mods[10] * 256, mods[11] * 256) cv.InRangeS(hsv, blueLower, blueUpper, mask) cv.ShowImage("Blue:", mask) elif (colour == "YELLOW"): yellowLower = cv.Scalar(mods[12] * 256, mods[13] * 256, mods[14] * 256) yellowUpper = cv.Scalar(mods[15] * 256, mods[16] * 256, mods[17] * 256) cv.InRangeS(hsv, yellowLower, yellowUpper, mask) cv.ShowImage("Yellow:", mask) elif (colour == "YWHITE"): blackLower = cv.Scalar(mods[18] * 256, mods[19] * 256, mods[20] * 256) blackUpper = cv.Scalar(mods[21] * 256, mods[22] * 256, mods[23] * 256) cv.InRangeS(hsv, blackLower, blackUpper, mask) cv.ShowImage("YellowWhite:", mask) elif (colour == "BWHITE"): blackLower = cv.Scalar(mods[18] * 256, mods[19] * 256, mods[20] * 256) blackUpper = cv.Scalar(mods[21] * 256, mods[22] * 256, mods[23] * 256) cv.InRangeS(hsv, blackLower, blackUpper, mask) cv.ShowImage("BlueWhite:", mask) # Count white pixels to make sure program doesn't crash if it finds nothing if (cv.CountNonZero(mask) < 3): return ((0, 0), 0) # Clean up the image to reduce anymore noise in the binary image cv.Smooth(mask, mask, cv.CV_GAUSSIAN, 9, 9, 0, 0) convKernel = cv.CreateStructuringElementEx(9, 9, 0, 0, cv.CV_SHAPE_RECT) cv.Erode(mask, mask, convKernel, 1) cv.Dilate(mask, mask, convKernel, 1) moments = cv.Moments(mask, 1) M00 = cv.GetSpatialMoment(moments, 0, 0) M10 = cv.GetSpatialMoment(moments, 1, 0) M01 = cv.GetSpatialMoment(moments, 0, 1) if M00 == 0: M00 = 0.01 center_of_mass = (round(M10 / M00), round(M01 / M00)) if (colour == "BLUE" or colour == "YELLOW"): return (center_of_mass, find_orientation(mask, center_of_mass)) else: return (center_of_mass, 0)
def showProcessedFrame(self): cv.ShowImage('filterdemo', self.processedFrame)
def __showWindow(self): cv.ShowImage(self.__windowName, self.__capturedImageTarget) return cv.WaitKey(10) <= 0
import freenect import cv import numpy as np freenect.start() cv.NamedWindow('Depth') while 1: depth = freenect.get_depth_np() cv.ShowImage('Depth', depth.astype(np.uint8)) cv.WaitKey(10)
fdraw.insert(0, (x1, y1, gv)) nfeat[angmax] = 0. #draw from smallest to highest gradient magnitude for i in xrange(len(fdraw)): x1, y1, gv = fdraw[i] cv.Line(target, (px - x1, py + y1), (px + x1, py - y1), cv.CV_RGB(gv, gv, gv), 1, 8) else: #don't draw if there's no reponse pass im = cv.LoadImage('gente.jpg') #image for visualization vhog = cv.CreateImage(cv.GetSize(im), 8, 1) hog = calc_hog(im) draw_hog(vhog, hog, 8) cv.ShowImage('hi', vhog) #clear for reuse cv.Set(vhog, 0) draw_hog(vhog, hog, 16) cv.ShowImage('lo', vhog) key = cv.WaitKey(0)
def Display(image): cv.NamedWindow("Red Eye Test") cv.ShowImage("Red Eye Test", image) cv.WaitKey(0) cv.DestroyWindow("Red Eye Test")
def showResizedImage(image, size, name): resized_image = cv.CreateMat(size[0], size[1], image.type) cv.NamedWindow(name) cv.Resize(image, resized_image) cv.ShowImage(name, resized_image)
import frame_convert cv.NamedWindow('Depth') cv.NamedWindow('Video') ind = 0 print(('%s\nPress ESC to stop' % __doc__)) def get_depth(ind): return frame_convert.pretty_depth_cv(freenect.sync_get_depth(ind)[0]) def get_video(ind): return frame_convert.video_cv(freenect.sync_get_video(ind)[0]) while 1: print(ind) try: depth = get_depth(ind) video = get_video(ind) except TypeError: ind = 0 continue ind += 1 cv.ShowImage('Depth', depth) cv.ShowImage('Video', video) if cv.WaitKey(10) == 27: break #freenect.sync_stop() # NOTE: Uncomment if your machine can't handle it
#!/usr/bin/env python import cv capture = cv.CaptureFromCAM(0) cv.WaitKey(200) frame = cv.QueryFrame(capture) font = cv.InitFont(cv.CV_FONT_HERSHEY_DUPLEX, 1, 1, 0, 2, 8) while True: frame = cv.QueryFrame(capture) # cv.PutText(frame, "ShapeOko CAM", (10,460), font, cv.RGB(17, 110, 255)) cv.Line(frame, (320,0), (320,480) , 255) cv.Line(frame, (0,240), (640,240) , 255) cv.Circle(frame, (320,240), 100, 255) cv.ShowImage("Window",frame) c = (cv.WaitKey(16) & 255) if c==27: #Break if user enters 'Esc'. break
def find_places(self, c): # find long side of ball tray l1_sq = ((c[1][0] - c[0][0]) * (c[1][0] - c[0][0])) + \ ((c[1][1] - c[0][1]) * (c[1][1] - c[0][1])) l2_sq = ((c[2][0] - c[1][0]) * (c[2][0] - c[1][0])) + \ ((c[2][1] - c[1][1]) * (c[2][1] - c[1][1])) if l1_sq > l2_sq: # c[0] to c[1] is a long side cc = [c[0], c[1], c[2], c[3]] else: # c[1] to c[2] is a long side cc = [c[1], c[2], c[3], c[0]] # ball tray corners in baxter coordinates for i in range(4): self.ball_tray_corner[i] = self.pixel_to_baxter( cc[i], self.tray_distance) # ball tray places in pixel coordinates ref_x = cc[0][0] ref_y = cc[0][1] dl_x = (cc[1][0] - cc[0][0]) / 8 dl_y = (cc[1][1] - cc[0][1]) / 8 ds_x = (cc[2][0] - cc[1][0]) / 6 ds_y = (cc[2][1] - cc[1][1]) / 6 p = {} p[0] = (ref_x + (3 * dl_x) + (3 * ds_x), ref_y + (3 * dl_y) + (3 * ds_y)) p[1] = (ref_x + (5 * dl_x) + (3 * ds_x), ref_y + (5 * dl_y) + (3 * ds_y)) p[2] = (ref_x + (3 * dl_x) + (1 * ds_x), ref_y + (3 * dl_y) + (1 * ds_y)) p[3] = (ref_x + (5 * dl_x) + (1 * ds_x), ref_y + (5 * dl_y) + (1 * ds_y)) p[4] = (ref_x + (3 * dl_x) + (5 * ds_x), ref_y + (3 * dl_y) + (5 * ds_y)) p[5] = (ref_x + (5 * dl_x) + (5 * ds_x), ref_y + (5 * dl_y) + (5 * ds_y)) p[6] = (ref_x + (1 * dl_x) + (3 * ds_x), ref_y + (1 * dl_y) + (3 * ds_y)) p[7] = (ref_x + (7 * dl_x) + (3 * ds_x), ref_y + (7 * dl_y) + (3 * ds_y)) p[8] = (ref_x + (1 * dl_x) + (1 * ds_x), ref_y + (1 * dl_y) + (1 * ds_y)) p[9] = (ref_x + (7 * dl_x) + (1 * ds_x), ref_y + (7 * dl_y) + (1 * ds_y)) p[10] = (ref_x + (1 * dl_x) + (5 * ds_x), ref_y + (1 * dl_y) + (5 * ds_y)) p[11] = (ref_x + (7 * dl_x) + (5 * ds_x), ref_y + (7 * dl_y) + (5 * ds_y)) for i in range(12): # mark position of ball tray places cv.Circle(self.cv_image, (int(p[i][0]), int(p[i][1])), 5, (0, 250, 0), -1) # ball tray places in baxter coordinates self.ball_tray_place[i] = self.pixel_to_baxter( p[i], self.tray_distance) # display the ball tray places cv.ShowImage("Egg tray", self.cv_image) if self.save_images: # save ball tray image with overlay of ball tray and ball positions file_name = self.image_dir + "ball_tray.jpg" cv.SaveImage(file_name, self.cv_image) # 3ms wait cv.WaitKey(3)
def track(self): # in case something else is still open cv.DestroyAllWindows() tracker = Speed() imgArr = [] capture = cv.CaptureFromCAM(self.camera_index) if not capture: QMessageBox.information(self, "Camera Error", "Camera not found") return cv.NamedWindow("Video", cv.CV_WINDOW_AUTOSIZE) cv.MoveWindow("Video", 320, 0) cv.NamedWindow("Tracking", cv.CV_WINDOW_AUTOSIZE) cv.MoveWindow("Tracking", 800, 82) moments = 0 cv.CreateTrackbar("Start at color", "Video", self.low_color, 179, self.update_low_color) cv.CreateTrackbar("End at color", "Video", self.high_color, 179, self.update_high_color) start_time = 0 camera_on = True tracking = False needs_saving = False background = 0 while camera_on: if (not self.busy_updating): frame = cv.QueryFrame(capture) if not frame: break #convert color to hue space for easier tracking imgHSV = cv.CreateImage(cv.GetSize(frame), 8, 3) cv.CvtColor(frame, imgHSV, cv.CV_BGR2HSV) imgThresh = cv.CreateImage(cv.GetSize(frame), 8, 1) # implement interactive thresholding cv.InRangeS(imgHSV, cv.Scalar(self.low_color, self.MED_SV, self.MED_SV), cv.Scalar(self.high_color, self.MAX_SV, self.MAX_SV), imgThresh) # find image momentsm, compute object position # by dividing by area moments = cv.Moments(cv.GetMat(imgThresh)) x_mov = cv.GetSpatialMoment(moments, 1, 0) y_mov = cv.GetSpatialMoment(moments, 0, 1) area = cv.GetCentralMoment(moments, 0, 0) # size is 480 360 for webcam # 324, 243 for massive-imaged external camera small_thresh = cv.CreateImage((self.fit_camera_width, self.fit_camera_height), 8, 1) cv.Resize(imgThresh, small_thresh) small_frame = cv.CreateImage((self.fit_camera_width, self.fit_camera_height), 8, 3) cv.Resize(frame, small_frame) cv.ShowImage("Tracking", small_thresh) cv.ShowImage("Video", small_frame) k = cv.WaitKey(1) # press q or escape to quit camera view if k == 27 or k == 113: camera_on = False tracking = False cv.DestroyAllWindows() break; # click "Record!" or press "g" to start tracking speed/recording elif k == 103 or self.start_record: needs_saving = True start_time = time.clock() tracking = True # store background in memory and proceed to recording background = cv.CloneImage(frame) tracker.start_time = start_time self.start_record = False imgArr.append(background) # click "Stop recording" or press "d" to stop tracking speed/recording # save everything in the proper format and close recording windows elif k == 100 or self.end_record: if needs_saving: tracking = False tracker.stop_time = time.clock() curr_dir = os.listdir(".") # create the output folder if it doesn't already exist if self.output_folder not in curr_dir: path_var = "./" + str(self.output_folder) os.mkdir(path_var) # default saving directory is output_folder/Trial_<start time of trial> new_vid_folder = "./" + str(self.output_folder) + "/Trial_" + str(start_time) os.mkdir(new_vid_folder) tracker.out_folder = new_vid_folder # save the first frame of the film as the background image background_name = str(tracker.out_folder) + "/background.png" cv.SaveImage(background_name, background) index = 0 for img in imgArr: output_name = str(tracker.out_folder) + "/frame_" + str(index) + ".png" cv.SaveImage(output_name, img) index += 1 # compute velocity and acceleration values tracker.update() # save the tracking done so far (by pickling) # can later be exported as a text file tracker_file = str(tracker.out_folder) + "/Data" f = open(tracker_file, 'w') pickle.dump(tracker, f) f.close() cv.DestroyAllWindows() break else: cv.DestroyAllWindows() break if tracking: # store object position if area > 0: posX = float(x_mov)/float(area) posY = float(y_mov)/float(area) curr_time = time.clock() tracker.add_pos(posX, posY, curr_time - start_time) start_time = curr_time imgArr.append(cv.CloneImage(frame))