Exemple #1
0
     def show_frame(self):        
                 
         color_image = cv.QueryFrame(self.capture)
        
         color_image1 = cv.CreateImage(cv.GetSize(color_image), 8, 3)
         grey_image = cv.CreateImage(cv.GetSize(color_image), cv.IPL_DEPTH_8U, 1)
         moving_average = cv.CreateImage(cv.GetSize(color_image), cv.IPL_DEPTH_32F, 3)

         grey = cv.CreateImage(cv.GetSize(color_image), 8, 3)
         HSV = cv.CreateImage(cv.GetSize(color_image), 8, 3)
         red = cv.CreateImage(cv.GetSize(color_image), 8, 3)         

         cv.CvtColor(color_image, grey, cv.CV_RGB2HLS)
         cv.CvtColor(color_image, HSV, cv.CV_RGB2HSV)
         cv.Not(color_image, red)

         cv.ShowImage(self.window1, color_image)
         cv.ShowImage(self.window2, grey) 
         cv.ShowImage(self.window3, HSV) 
         cv.ShowImage(self.window4, red)

         cv.MoveWindow(self.window1, 30, 120) 
         cv.MoveWindow(self.window2, 430, 120)
         cv.MoveWindow(self.window3, 430, 470)
         cv.MoveWindow(self.window4, 30, 470)

         while self.arduino.inWaiting() > 0:
            self.data += self.arduino.read(1)
Exemple #2
0
    def __init__(self, node_name):
        ROS2OpenCV2.__init__(self, node_name)

        self.node_name = node_name

        # The minimum saturation of the tracked color in HSV space,
        # as well as the min and max value (the V in HSV) and a
        # threshold on the backprojection probability image.
        self.smin = rospy.get_param("~smin", 85)
        self.vmin = rospy.get_param("~vmin", 50)
        self.vmax = rospy.get_param("~vmax", 254)
        self.threshold = rospy.get_param("~threshold", 50)

        # Create a number of windows for displaying the histogram,
        # parameters controls, and backprojection image
        cv.NamedWindow("Histogram", cv.CV_WINDOW_NORMAL)
        cv.MoveWindow("Histogram", 700, 50)
        cv.NamedWindow("Parameters", 0)
        cv.MoveWindow("Parameters", 700, 325)
        cv.NamedWindow("Backproject", 0)
        cv.MoveWindow("Backproject", 700, 600)

        # Create the slider controls for saturation, value and threshold
        cv.CreateTrackbar("Saturation", "Parameters", self.smin, 255, self.set_smin)
        cv.CreateTrackbar("Min Value", "Parameters", self.vmin, 255, self.set_vmin)
        cv.CreateTrackbar("Max Value", "Parameters", self.vmax, 255, self.set_vmax)
        cv.CreateTrackbar("Threshold", "Parameters", self.threshold, 255, self.set_threshold)

        # Initialize a number of variables
        self.hist = None
        self.track_window = None
        self.show_backproj = False
Exemple #3
0
    def __init__(self):
        self.t = TurtleBotMove()
        self.node_name = "testDriveDemo"        
     #   rospy.init_node(self.node_name)

        # What we do during shutdown
        rospy.on_shutdown(self.cleanup)

        # Create the OpenCV display window for the RGB image
        self.cv_window_name = self.node_name
        cv.NamedWindow(self.cv_window_name, cv.CV_WINDOW_NORMAL)
        cv.MoveWindow(self.cv_window_name, 25, 75)
        
        # And one for the depth image
        cv.NamedWindow("Depth Image", cv.CV_WINDOW_NORMAL)
        cv.MoveWindow("Depth Image", 25, 350)
        
        # Create the cv_bridge object
        self.bridge = CvBridge()
        
        # Subscribe to the camera image and depth topics and set
        # the appropriate callbacks
        self.image_sub = rospy.Subscriber("/camera/rgb/image_raw", Image, self.image_callback)
        self.depth_sub = rospy.Subscriber("/camera/depth/image_raw", Image, self.depth_callback)
        
        rospy.loginfo("Waiting for image topics...")
Exemple #4
0
    def __init__(self):
        self.node_name = "cv_bridge_demo"

        rospy.init_node(self.node_name)

        # 关闭
        rospy.on_shutdown(self.cleanup)

        # 创建 rgb图像 显示窗口
        self.cv_window_name = self.node_name
        cv.NamedWindow(self.cv_window_name, cv.CV_WINDOW_NORMAL)
        cv.MoveWindow(self.cv_window_name, 25, 75)

        # 创建深度图像显示窗口
        cv.NamedWindow("Depth Image", cv.CV_WINDOW_NORMAL)
        cv.MoveWindow("Depth Image", 25, 350)

        # 创建 ros 图 到 opencv图像转换 对象
        self.bridge = CvBridge()

        # 订阅 深度图像和rgb图像数据发布的话题  以及定义 回调函数
        # the appropriate callbacks
        self.image_sub = rospy.Subscriber("input_rgb_image",
                                          Image,
                                          self.image_callback,
                                          queue_size=1)
        self.depth_sub = rospy.Subscriber("input_depth_image",
                                          Image,
                                          self.depth_callback,
                                          queue_size=1)
        # 登陆信息
        rospy.loginfo("Waiting for image topics...")
        rospy.wait_for_message("input_rgb_image", Image)
        rospy.loginfo("Ready.")
Exemple #5
0
    def __init__(self):
        self.node_name = "cv_bridge_demo"

        rospy.init_node(self.node_name)

        # What we do during shutdown
        rospy.on_shutdown(self.cleanup)

        # Create the OpenCV display window for the RGB image
        self.cv_window_name = self.node_name
        cv.NamedWindow(self.cv_window_name, cv.CV_WINDOW_NORMAL)
        cv.MoveWindow(self.cv_window_name, 25, 75)

        # And one for the depth image
        cv.NamedWindow("Depth Image", cv.CV_WINDOW_NORMAL)
        cv.MoveWindow("Depth Image", 25, 350)

        # Create the cv_bridge object
        self.bridge = CvBridge()

        # Subscribe to the camera image and depth topics and set
        # the appropriate callbacks
        self.image_sub = rospy.Subscriber("/camera/rgb/image_color",
                                          Image,
                                          self.image_callback,
                                          queue_size=1)
        self.depth_sub = rospy.Subscriber("/camera/depth_registered/image",
                                          Image,
                                          self.depth_callback,
                                          queue_size=1)

        rospy.loginfo("Waiting for image topics...")
        rospy.wait_for_message("input_rgb_image", Image)
        rospy.loginfo("Ready.")
    def close_adaptive(self, frame):
#Gray Scale        

        frame = cv2.GaussianBlur(frame, (3,3),2)
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        
##Morphological Operations        
        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(2,2))
        gray = cv2.morphologyEx(gray, cv2.MORPH_OPEN, kernel, iterations=2)    
        gray = cv2.morphologyEx(gray, cv2.MORPH_CLOSE, kernel,iterations=5)

##Creating a copy to draw on        
        overlayed = frame
        
#getting size of image        
        m,n = gray.shape

#Adaptive Thresholding
        edges = cv2.adaptiveThreshold(gray,self.a_maxval,self.a_method,self.a_threshtype,self.a_block_size,self.a_constant)

        if self.debug_mode:
        
            cv.NamedWindow("Gray", 0)
            cv.MoveWindow("Gray", 0, 0)
            cv2.imshow("Gray", gray)
            
            cv.NamedWindow("Edges", 0)
            cv.MoveWindow("Edges", 330, 0)
            cv2.imshow("Edges", edges)

        return edges
    def __init__(self):
        self.node_name = "turtlebot_openCV"

        rospy.init_node(self.node_name)

        # Create the OpenCV display window for the RGB image
        cv.NamedWindow("Color Image", cv.CV_WINDOW_NORMAL)
        cv.MoveWindow("Color Image", 25, 75)

        # And one for the depth image
        cv.NamedWindow("Depth Image", cv.CV_WINDOW_NORMAL)
        cv.MoveWindow("Depth Image", 25, 350)

        # Create the cv_bridge object
        self.bridge = CvBridge()

        # Subscribe to the camera image and depth topics and set
        # the appropriate callbacks
        self.image_sub = rospy.Subscriber("input_rgb_image",
                                          Image,
                                          self.image_callback,
                                          queue_size=1)
        self.depth_sub = rospy.Subscriber("input_depth_image",
                                          Image,
                                          self.depth_callback,
                                          queue_size=1)
Exemple #8
0
def show_camera_and_get_images(capture, laser_ctrl, mapx=None, mapy=None):
    while 1:
        img, k = show_camera_and_wait_for_key(capture, 5, mapx, mapy)
        if k >= 0:
            img1, img2 = get_images(capture, laser_ctrl, mapx, mapy)
            cv.NamedWindow('img1', cv.CV_WINDOW_AUTOSIZE)
            cv.NamedWindow('img2', cv.CV_WINDOW_AUTOSIZE)

            cv.ShowImage('img1', img1)
            cv.ShowImage('img2', img2)

            cv.MoveWindow('img1', 10, 10)
            cv.MoveWindow('img2', 660, 10)
            return img1, img2
Exemple #9
0
    def __init__(self):
        self.node_name = "line_follower_turtlebot"

        rospy.init_node(self.node_name)
        rospy.on_shutdown(self.cleanup)

        # Determine size of window size and name
        self.cv_window_name = self.node_name
        cv.NamedWindow(self.cv_window_name, cv.CV_WINDOW_NORMAL)
        cv.MoveWindow(self.cv_window_name, 25, 75)

        # Create the cv_bridge object
        self.bridge = CvBridge()

        # Subscribe to the Camera Image Raw
        self.image_sub = rospy.Subscriber("/camera/rgb/image_raw",
                                          Image,
                                          self.image_callback,
                                          queue_size=1)

        # Publish to Robot Velocity
        self.pub = rospy.Publisher('/cmd_vel_mux/input/teleop',
                                   Twist,
                                   queue_size=1)
        self.rate = rospy.Rate(10)

        #Login Info
        rospy.loginfo("Bela Ciao")
        rospy.wait_for_message("/camera/rgb/image_raw", Image)
        rospy.loginfo("Line Following Turtlebot")
Exemple #10
0
    def __init__(self):
        self.demo_on = False
        self.node_name = "walle_recognizer"

        rospy.init_node(self.node_name)

        # What we do during shutdown
        rospy.on_shutdown(self.cleanup)

        # Create the OpenCV display window for the RGB image
        self.cv_window_name = self.node_name
        cv.NamedWindow("RGB Image", cv.CV_WINDOW_NORMAL)
        cv.MoveWindow("RGB Image", 25, 75)

        # And one for the depth image
        cv.NamedWindow("Depth Image", cv.CV_WINDOW_NORMAL)
        cv.MoveWindow("Depth Image", 25, 350)
        self.known_objects = []
        self.sift = cv2.ORB()

        self.current_depth = None
        # self.current_color = None
        self.current_stage = None

        # Create the cv_bridge object
        self.bridge = CvBridge()

        self.pub = rospy.Publisher("/walle/recognizer/publish", String)
        self.rate = rospy.Rate(10)  # 10hz

        self.last_recognized = None

        # Subscribe to the camera image and depth topics and set
        # the appropriate callbacks

        self.image_sub = rospy.Subscriber("/camera/rgb/image_color", Image,
                                          self.image_callback)
        self.depth_sub = rospy.Subscriber("/camera/depth/image_raw", Image,
                                          self.depth_callback)

        rospy.Subscriber("/walle/recognizer/listen",
                         String,
                         self.receive,
                         queue_size=1)

        rospy.loginfo("Waiting for image topics...")
Exemple #11
0
    def __init__(self, node_name):
        ROS2OpenCV2.__init__(self, node_name)

        self.node_name = node_name
        self.color_under_detect = rospy.get_param("~color_under_detect", "red")
        # call masking alglorthm to get the color mask
        self.mymask = Masking(color=self.color_under_detect,
                              shape="triangle",
                              masker=None,
                              detector=None,
                              matcher=None,
                              matching_method=None)

        # The minimum saturation of the tracked color in HSV space,
        # as well as the min and max value (the V in HSV) and a
        # threshold on the backprojection probability image.
        self.smin = rospy.get_param("~smin", 85)
        self.vmin = rospy.get_param("~vmin", 50)
        self.vmax = rospy.get_param("~vmax", 254)
        self.threshold = rospy.get_param("~threshold", 50)

        # Create a number of windows for displaying the histogram,
        # parameters controls, and backprojection image
        cv.NamedWindow("Histogram", cv.CV_WINDOW_NORMAL)
        cv.MoveWindow("Histogram", 700, 50)
        cv.NamedWindow("Parameters", 0)
        cv.MoveWindow("Parameters", 700, 325)
        cv.NamedWindow("Backproject", 0)
        cv.MoveWindow("Backproject", 700, 600)
        cv.NamedWindow("Tracked_obj", 0)
        cv.MoveWindow("Tracked_obj", 1000, 50)

        # Create the slider controls for saturation, value and threshold
        cv.CreateTrackbar("Saturation", "Parameters", self.smin, 255,
                          self.set_smin)
        cv.CreateTrackbar("Min Value", "Parameters", self.vmin, 255,
                          self.set_vmin)
        cv.CreateTrackbar("Max Value", "Parameters", self.vmax, 255,
                          self.set_vmax)
        cv.CreateTrackbar("Threshold", "Parameters", self.threshold, 255,
                          self.set_threshold)

        # Initialize a number of variables
        self.hist = None
        self.track_window = None
        self.show_backproj = False
Exemple #12
0
	def __init__(self):
		self.node_name="cv_bridge_demo"
		rospy.init_node(self.node_name)

		rospy.on_shutdown(self.cleanup)
		self.cv_window_name=self.node_name
		cv.NamedWindow(self.cv_window_name,cv.CV_WINDOW_NORMAL)
		cv.MoveWindow(self.cv_window_name,25,75)

		cv.NamedWindow("Depth Image",cv.CV_WINDOW_NORMAL)
		cv.MoveWindow("depth Image",25,350)

		self.bridge = CvBridge()

		self.image_sub=
		rospy.Subscriber("/camera/rgb/image_color",Image,self.image_callback)
		self.depth_sub=
		rospy.Subscriber("/camera/depth/image_raw",Image,self.depth_callback)
		rospy.loginfo("Waiting for image topic...")
    def cam_shift(self, frame):
        # First blue the image
        blur = cv2.blur(frame, (5, 5))
        
        # Convert from RGB to HSV spave
        hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV)
        
        # Create a mask using the current saturation and value parameters
        mask = cv2.inRange(hsv, np.array((38., self.smin, self.vmin)), np.array((75., 250., self.vmax)))
        
        x0, y0, w, h = [0,0,320,240]
        x1 = x0 + w
        y1 = y0 + h
        self.track_window = (x0, y0, x1, y1)
        hsv_roi = hsv[y0:y1, x0:x1]
        mask_roi = mask[y0:y1, x0:x1]
        self.hist = cv2.calcHist( [hsv_roi], [0], mask_roi, [16], [0, 180] )
        cv2.normalize(self.hist, self.hist, 0, 255, cv2.NORM_MINMAX);
        self.hist = self.hist.reshape(-1)
        self.show_hist()
        
        # If we have a histogram, tracking it with CamShift
        if self.hist is not None:
            # Compute the backprojection from the histogram
            backproject = cv2.calcBackProject([hsv], [0], self.hist, [0, 180], 1)
            
            # Mask the backprojection with the mask created earlier
            backproject &= mask

            # Threshold the backprojection
            ret, backproject = cv2.threshold(backproject, self.threshold, 255, cv.CV_THRESH_TOZERO)

            x, y, w, h = self.track_window
            if self.track_window is None or w <= 0 or h <=0:
                self.track_window = 0, 0, self.frame_width - 1, self.frame_height - 1
            
            # Set the criteria for the CamShift algorithm
            term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )
            
            # Run the CamShift algorithm
            self.track_box, self.track_window = cv2.CamShift(backproject, self.track_window, term_crit)

        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(2,2))
        backproject = cv2.morphologyEx(backproject, cv2.MORPH_CLOSE, kernel,iterations=20)
        
        if self.debug_mode:
            # Display the resulting backprojection
            cv.NamedWindow("Backproject", 0)
            cv.MoveWindow("Backproject", 0, 240)
            cv2.imshow("Backproject", backproject)

        return backproject
    def __init__(self, name, start_values):

        self.name = name

        cv.NamedWindow(name, flags=cv.CV_WINDOW_NORMAL)
        cv.MoveWindow(name, 20, 20)

        assert len(start_values) == 6

        self.values = OrderedDict()
        for channel, value in start_values.items():
            self.values[channel] = value
            cv.CreateTrackbar(channel, name, value, 255,
                              self.make_modifier(channel))
Exemple #15
0
	def __init__(self):
		self.node_name = 'cv_bridge_demo'
		rospy.init_node(self.node_name)
		#what we do during shutdown
		rospy.on_shutdown(self.cleanup)
		
		#create the opencv display window for the rgb image
		self.cv_window_name = self.node_name
		cv.NamedWindow(self.cv_window_name,cv.CV_WINDOW_NORMAL)
		cv.MoveWindow(self.cv_window_name,25,75)
		
		# and one for the depth image
		cv.NamedWindow("depth image",cv.CV_WINDOW_NORMAL)
		cv.MoveWindow('depth image',25,300)
		
		# create the cv_bridge object
		self.bridge = CvBridge()
		
		# subscribe to the camera image and depth topics and set
		# the appropriate callbacks
		self.image_sub = rospy.Subscriber('/camera/rgb/image_color',Image,self.image_callback)
		self.depth_sub = rospy.Subcriber('/camera/depth_registered/image_rect',Image,self.depth_callback)
		
		rospy.loginfo("waiting for the image topics...")
 def __init__(self):
     self.node_name = "cv_bridge_demo"
     #Initialize the ros node
     rospy.init_node(self.node_name)
     # What we do during shutdown
     # Create the OpenCV display window for the RGB image
     self.cv_window_name = self.node_name
     cv.NamedWindow(self.cv_window_name, cv.CV_WINDOW_NORMAL)
     cv.MoveWindow(self.cv_window_name, 25, 75)
     # Create the cv_bridge object
     self.bridge = CvBridge()
     # Subscribe to the camera image and depth topics and set
     # the appropriate callbacks
     self.image_sub = rospy.Subscriber("/camera/rgb/image_rect_color", Image, self.image_callback)
     self.image_pub = rospy.Publisher("image_mask", Image)
     rospy.loginfo("Waiting for image topics...")
Exemple #17
0
def single_measure_gui():
    #setup
    capture, mapx, mapy = init_capture_device(capture_device)
    laser_controller = init_laser_controller(laser_controller_port)
    setup_camera_window()

    #get images and calculate distance
    img1, img2 = show_camera_and_get_images(capture, laser_controller, mapx,
                                            mapy)
    distance, posX, posY, area = measure_distance_from_images(img1, img2)

    #output
    r_img = result_image(img2, posX, posY, distance)
    cv.MoveWindow('res', 10, 10)
    cv.ShowImage('res', r_img)
    cv.SaveImage("img1.jpg", img1)
    cv.SaveImage("img2.jpg", r_img)
    print "%0.2f,%d,%d,%d" % (distance, posX, posY, area)

    cv.WaitKey(50000)
Exemple #18
0
    def __init__(self):
        self.node_name = "cv_bridge_demo"
        
        rospy.init_node(self.node_name)
        
        # What we do during shutdown
        rospy.on_shutdown(self.cleanup)
        
        # Create the OpenCV display window for the RGB image
        self.cv_window_name = self.node_name
        cv.NamedWindow(self.cv_window_name, cv.CV_WINDOW_NORMAL)
        cv.MoveWindow(self.cv_window_name, 25, 75)
        
        # And one for the depth image

        
        self.target_visible = False
        self.tl = tf.TransformListener()

        self.xt = -1
        self.yt = -1
        self.w = -1
        self.i = -1
        self.j = -1
        self.k = -1
        self.yaw = -1;
        self.roll= -1;
        self.pitch = -1;
        self.MarkId = -1;
        # Create the cv_bridge object
        self.bridge = CvBridge()
        self.arList = []
        # Subscribe to the camera image and depth topics and set
        # the appropriate callbacks
        self.image_sub = rospy.Subscriber("/camera/rgb/image_color", Image, self.image_callback)
       
        self.ar_sub = rospy.Subscriber('ar_pose_marker', AlvarMarkers, self.registerAR_cb)

        self.is_visible = False;

        rospy.loginfo("Waiting for image topics...")
Exemple #19
0
    def __init__(self, node_name):
        ROS2OpenCV2.__init__(self, node_name)

        self.node_name = node_name
        

        self.match_threshold = rospy.get_param("~match_threshold", 0.7)
        self.find_multiple_targets = rospy.get_param("~find_multiple_targets", False)
        self.n_pyr = rospy.get_param("~n_pyr", 2)
        self.min_template_size = rospy.get_param("~min_template_size", 25)

        self.scale_factor = rospy.get_param("~scale_factor", 1.2)
        self.scale_and_rotate = rospy.get_param("~scale_and_rotate", False)
        
        self.use_depth_for_detection = rospy.get_param("~use_depth_for_detection", False)
        self.fov_width = rospy.get_param("~fov_width", 1.094)
        self.fov_height = rospy.get_param("~fov_height", 1.094)
        self.max_object_size = rospy.get_param("~max_object_size", 0.28)


        # The minimum saturation of the tracked color in HSV space,
        # as well as the min and max value (the V in HSV) and a 
        # threshold on the backprojection probability image.
        self.smin = rospy.get_param("~smin", 85)
        self.vmin = rospy.get_param("~vmin", 50)
        self.vmax = rospy.get_param("~vmax", 254)
        self.threshold = rospy.get_param("~threshold", 50)
                       
        # Create a number of windows 
        cv.NamedWindow("Backproject", 0)
        cv.MoveWindow("Backproject", 700, 600)
        
        # Initialize a number of variables
        self.hist = None
        self.track_window = None
        self.show_backproj = False



        self.detector_loaded = False
Exemple #20
0
    def __init__(self):
        self.node_name = "cv_bridge_demo"
        rospy.init_node(self.node_name)
        # What we do during shutdown
        rospy.on_shutdown(self.cleanup)
        # Create the OpenCV display window for the RGB image
        self.cv_window_name = self.node_name
        cv2.NamedWindow(self.cv_window_name, cv2.CV_WINDOW_NORMAL)
        cv2.MoveWindow(self.cv_window_name, 25, 75)

        # Create the cv_bridge object
        self.bridge = CvBridge()

        # Subscribe to the camera image and depth topics and set
        # the appropriate callbacks
        self.image_sub = rospy.Subscriber("/usb_cam/image_raw",
                                          Image,
                                          self.image_callback,
                                          queue_size=1)

        rospy.loginfo("Waiting for image topics...")
        rospy.wait_for_message("/usb_cam/image_raw", Image)
        rospy.loginfo("Ready.")
    def prob_hough(self, frame):

        overlayed = frame
#Probabilistic Line Detection

        plines = cv2.HoughLinesP(frame, self.h_rho, np.pi/self.h_theta, self.h_thresh, np.array([]), self.h_minlinelength)[0]
        print plines

# Draw Red lines
        for l in plines[:1]:
            cv2.line(overlayed, (l[0],l[1]), (l[2],l[3]), (0,0,255), 2)
                
        x = (l[2]-l[0])+l[0]/2
        y = (l[1]+l[3])/2
            
        print "x = %d & y = %d" % (x , y)

# Debugging Windows

        if self.debug_mode:
        
            cv.NamedWindow("Hough", 0)
            cv.MoveWindow("Hough", 660, 0)
            cv2.imshow("Hough", overlayed)        
Exemple #22
0
            print "Giving up trying to set camera settings. Retry a few times, fix it or run with --disable-cam-settings"
            sys.exit(1)
    cap = cap_camera(int(args.cam))
elif args.file:
    cap = cap_file(args.file)
else:
    raise ValueError("Must specify either --cam or --file!")

detector_state = DetectorState((640, 480), 128, 5, approx_object_size=15.0)

d = OrderedDict([('h_low', 0), ('h_high', 255), ('s_low', 0), ('s_high', 255),
                 ('v_low', 0), ('v_high', 255)])
white_sample = threshold.Threshold("white_sample", d)

cv.NamedWindow("image", flags=cv.CV_WINDOW_NORMAL)
cv.MoveWindow("image", 20, 20)
cv.ResizeWindow("image", 700, 700)

cv.NamedWindow("original", flags=cv.CV_WINDOW_NORMAL)
cv.MoveWindow("original", 650, 20)
cv.ResizeWindow("original", 640, 480)

cv.NamedWindow("heuristics", flags=cv.CV_WINDOW_NORMAL)
cv.MoveWindow("heuristics", 400, 400)
cv.ResizeWindow("heuristics", 640, 480)

heuristics = HeuristicStack({
    (PhysicalSizeHeuristic(debug=False), 1.0),
    #(LargestHeuristic(debug=False), 0.5),
    (NormalBlobSizeHeuristic(debug=False), 0.5),
    (DensityHeuristic2(debug=False), 0.5)
Exemple #23
0
    def __init__(self, camera_num, debug = False):
        """
        Here all needed variables and data is initialized
        This function is run when starting
        """
        global cam
        #Initialize the camera and its values, Get the first frame
        cam = cv2.VideoCapture(camera_num)
        self.cam_width = cam.set(cv.CV_CAP_PROP_FRAME_WIDTH, 320)
        self.cam_height = cam.set(cv.CV_CAP_PROP_FRAME_HEIGHT, 240)        
    
        self.cam_width = cam.get(cv.CV_CAP_PROP_FRAME_WIDTH)
        self.cam_height = cam.get(cv.CV_CAP_PROP_FRAME_HEIGHT)
        self.debug = debug
        retval, self.frame = cam.read()
        
        self.count = 0
        #Create windows to show videoprocessing
        if self.debug:
            cv2.namedWindow( "Camera", cv.CV_WINDOW_AUTOSIZE )
            cv.MoveWindow('Camera', 450, 0)
            cv2.namedWindow( "Thresholded", cv.CV_WINDOW_AUTOSIZE )
            cv.MoveWindow("Thresholded", 450,350)
            cv2.namedWindow( "contour", cv.CV_WINDOW_AUTOSIZE )
            cv.MoveWindow("contour", 0,350)

        """
        Import the threshold values that are set by the thresholds.py program
        """
        thresholdvalues=""
        try:
            f = open('thresholdvalues.txt',  'r')
            thresholdvalues = f.read()
            f.close()
        except:
            print "No threshold values found. Creating blank list"
            for i in range(36):
                thresholdvalues+="0 "

        clrs=thresholdvalues.split()
        for i in range(len(clrs)):
            clrs[i]=int(clrs[i])
        
        #Choose the thresholds (variable names speak for them self)
        self.ball_threshold_low =        np.asarray([clrs[0],clrs[1],clrs[2]], np.uint8)
        self.ball_threshold_high =       np.asarray([clrs[3],clrs[4],clrs[5]], np.uint8)

        self.blue_gate_threshold_low =   np.asarray([clrs[6],clrs[7],clrs[8]], np.uint8)
        self.blue_gate_threshold_high =  np.asarray([clrs[9],clrs[10],clrs[11]], np.uint8)

        self.yellow_gate_threshold_low = np.asarray([clrs[12],clrs[13],clrs[14]], np.uint8)
        self.yellow_gate_threshold_high =np.asarray([clrs[15],clrs[16],clrs[17]], np.uint8)

        self.black_threshold_low =       np.asarray([clrs[18],clrs[19],clrs[20]], np.uint8)
        self.black_threshold_high =      np.asarray([clrs[21],clrs[22],clrs[23]], np.uint8)

        self.white_threshold_low =       np.asarray([clrs[24],clrs[25],clrs[26]], np.uint8)
        self.white_threshold_high =      np.asarray([clrs[27],clrs[28],clrs[29]], np.uint8)

        self.green_threshold_low =       np.asarray([clrs[30],clrs[31],clrs[32]], np.uint8)
        self.green_threshold_high =      np.asarray([clrs[33],clrs[34],clrs[35]], np.uint8)

        #Buffer
        self.buffer = FrameBufferThread()
        self.buffer.daemon = True
        self.buffer.start() 
Exemple #24
0
for i in range(4):
        cv.Smooth(dst,smoothed,cv.CV_BILATERAL, 30,1,32,32)
        cv.Smooth(smoothed,dst,cv.CV_BILATERAL, 30,1,32,32)
        cv.Smooth(dst,smoothed,cv.CV_BILATERAL, 30,1,32,32)

lap = cv.CreateImage(cv.GetSize(smoothed), cv.IPL_DEPTH_16S, 3)
#laplace = cv.Laplace(smoothed, lap)
gray = cv.CreateImage(cv.GetSize(smoothed), 8, 1)
canny = cv.CreateImage(cv.GetSize(smoothed), 8, 1)
cv.CvtColor(smoothed, gray, cv.CV_BGR2GRAY)
cv.Canny(gray,canny,20,20,3)

#ret,thresh = cv.threshold(imgray,127,255,0)
          
cv.NamedWindow('Original')
cv.MoveWindow('Original', 10, 10)
cv.ShowImage('Original', im)  
cv.NamedWindow('Smoothed')
cv.MoveWindow('Smoothed', 600, 100)
cv.ShowImage('Smoothed',smoothed)
cv.NamedWindow('Laplace')
cv.MoveWindow('Laplace', 600, 100)
cv.ShowImage('Canny',canny)
cv.ShowImage('Gray',gray)
cv.WaitKey(0) 
#cv.SaveImage("smoothed.png",dst)

#dst = cv.CreateImage(cv.GetSize(im), cv.IPL_DEPTH_16S, 3)
#cv.SaveImage("foo-laplace.png", lap)

    def __init__(self):
        self.node_name = "parameter_calibration"

        rospy.init_node(self.node_name)

        # What we do during shutdown
        rospy.on_shutdown(self.cleanup)

        # Create the OpenCV display window for the RGB image
        self.cv_window_name = self.node_name
        cv.NamedWindow(self.cv_window_name, cv.CV_WINDOW_NORMAL)
        cv.MoveWindow(self.cv_window_name, 25, 75)

        # Create the cv_bridge object
        self.bridge = CvBridge()

        # Initialize necessary parameters
        self.frame_size = None
        self.frame_width = None
        self.frame_height = None
        self.mask = None
        self.cameraMatrix = np.array([
            (2529.3016912669586, 0.0, 1007.0532160786125),
            (0.0, 2524.6309899852313, 650.2969085717225), (0.0, 0.0, 1.0)
        ])
        self.distCoeffs = np.array([
            -0.006795069030464255, -0.5045652004390003, 0.004947680741251182,
            0.005813011948658337, 0.0
        ])
        self.projectionMatrix = np.array([
            (2494.93408203125, 0.0, 1015.7040773447079, 0.0),
            (0.0, 2516.773681640625, 652.354580721294, 0.0),
            (0.0, 0.0, 1.0, 0.0)
        ])

        # Filter parameters
        self.medianBlur_ksize = rospy.get_param("~medianBlur_ksize", 3)
        self.GaussianBlur_ksize_width = rospy.get_param(
            "~GaussianBlur_ksize_width", 5)
        self.GaussianBlur_ksize_height = rospy.get_param(
            "~GaussianBlur_ksize_height", 5)
        self.GaussianBlur_sigmaX = rospy.get_param("~GaussianBlur_sigmaX", 0)
        self.GaussianBlur_sigmaY = rospy.get_param("~GaussianBlur_sigmaY", 0)
        self.Canny_threshold1 = rospy.get_param("~Canny_threshold1", 90)
        self.Canny_threshold2 = rospy.get_param("~Canny_threshold2", 15)
        self.Canny_apertureSize = rospy.get_param("~Canny_apertureSize", 3)
        self.Canny_L2gradient = rospy.get_param("~L2gradient", False)

        # Store all the feature parameters together for passing to filters
        self.medianBlur_params = dict(ksize=self.medianBlur_ksize)
        self.GaussianBlur_params = dict(ksize=(self.GaussianBlur_ksize_width,
                                               self.GaussianBlur_ksize_height),
                                        sigmaX=self.GaussianBlur_sigmaX,
                                        sigmaY=self.GaussianBlur_sigmaY,
                                        borderType=BORDER_DEFAULT)
        self.Canny_params = dict(threshold1=self.Canny_threshold1,
                                 threshold2=self.Canny_threshold2,
                                 apertureSize=self.Canny_apertureSize,
                                 L2gradient=self.Canny_L2gradient)

        # Create an image publisher to ouput processed image
        self.image_pub = rospy.Publisher("processed_image", Image)

        # Subscribe to the camera image and depth topics and set the appropriate callbacks
        self.image_sub = rospy.Subscriber("input_rgb_image",
                                          Image,
                                          self.image_callback,
                                          queue_size=1)

        rospy.loginfo("Waiting for image topics...")
        rospy.wait_for_message("input_rgb_image", Image)
        rospy.loginfo("Ready.")
Exemple #26
0
#! /usr/bin/env python

import cv2.cv as cv

cap = cv.CreateFileCapture("../c/tree.avi")
img = cv.QueryFrame(cap)
print "Got frame of dimensions (", img.width, " x ", img.height, ")"

cv.NamedWindow("win", cv.CV_WINDOW_AUTOSIZE)
cv.ShowImage("win", img)
cv.MoveWindow("win", 200, 200)
cv.WaitKey(0)
cv.DestroyAllWindows()
Exemple #27
0
    def __init__(self, node_name):        
        self.node_name = node_name

        rospy.init_node(node_name)
        rospy.loginfo("Starting node " + str(node_name))

        rospy.on_shutdown(self.cleanup)
        self.drink_type=""
        self._wake=0

        # Initialize the Region of Interest and its publisher
        self.ROI = RegionOfInterest()
        self.roi_pub = rospy.Publisher("/roi", RegionOfInterest, queue_size=1)
        self.dis_pub = rospy.Publisher("dist", Int32, queue_size=1)
        self.drink_ = rospy.Subscriber('drink_type',String,self.callbackDrink)
        self.wake_ = rospy.Subscriber('ros2_wake',Int32,self.callbackWake)
        
                
        self.hmin = rospy.get_param("~hmin", 22)
        self.hmax = rospy.get_param("~hmax", 62)
        self.smin = rospy.get_param("~smin", 115)
        self.smax = rospy.get_param("~smax", 255)
        self.vmin = rospy.get_param("~vmin", 50)
        self.vmax = rospy.get_param("~vmax", 255)
######

        while self._wake!=1:
            rospy.sleep(1)
        
        if self.drink_type == '雪碧':	#blue
            self.hmin = rospy.get_param("~hmin", 122)
            self.hmax = rospy.get_param("~hmax", 157)
            self.smin = rospy.get_param("~smin", 59)
            self.smax = rospy.get_param("~smax", 255)
            self.vmin = rospy.get_param("~vmin", 59)
            self.vmax = rospy.get_param("~vmax", 255)
            rospy.loginfo("匹配雪碧")
        
        if self.drink_type == '橙汁':	#yellow
            self.hmin = rospy.get_param("~hmin", 70)
            self.hmax = rospy.get_param("~hmax", 155)
            self.smin = rospy.get_param("~smin", 90)
            self.smax = rospy.get_param("~smax", 255)
            self.vmin = rospy.get_param("~vmin", 50)
            self.vmax = rospy.get_param("~vmax", 255)
            rospy.loginfo("匹配橙汁")
            
        if self.drink_type == '可乐':	#red
            self.hmin = rospy.get_param("~hmin", 155)
            self.hmax = rospy.get_param("~hmax", 190)
            self.smin = rospy.get_param("~smin", 67)
            self.smax = rospy.get_param("~smax", 224)
            self.vmin = rospy.get_param("~vmin", 83)
            self.vmax = rospy.get_param("~vmax", 223) 
            rospy.loginfo("匹配可乐")           
######
        #self.show_text=rospy.get_param("~show_text",True)
        # Initialize a number of global variables
        self.frame = None
        self.frame_size = None
        self.frame_width = None
        self.frame_height = None
        self.depth_image = None
        self.marker_image = None
        self.display_image = None
        self.keystroke = None
        self.keep_marker_history = False

        self.cX=0
        self.cY=0
        cv.NamedWindow("Parameters", 0)
        cv.MoveWindow("Parameters", 700, 325)

        # Create the slider controls for saturation, value and threshold
        cv.CreateTrackbar("Min H", "Parameters", self.hmin, 255, self.set_hmin)
        cv.CreateTrackbar("Max H", "Parameters", self.hmax, 255, self.set_hmax)
        cv.CreateTrackbar("Min S", "Parameters", self.smin, 255, self.set_smin)
        cv.CreateTrackbar("Max S", "Parameters", self.smax, 255, self.set_smax)
        cv.CreateTrackbar("Min Value", "Parameters", self.vmin, 255, self.set_vmin)
        cv.CreateTrackbar("Max Value", "Parameters", self.vmax, 255, self.set_vmax)

        # Create the main display window
        self.cv_window_name = self.node_name
        cv.NamedWindow(self.cv_window_name, cv.CV_WINDOW_NORMAL)
        cv.NamedWindow("reality", cv.CV_WINDOW_NORMAL)
        cv.NamedWindow("depth", cv.CV_WINDOW_NORMAL)

        # Create the cv_bridge object
        self.bridge = CvBridge()

        # Subscribe to the image and depth topics and set the appropriate callbacks
        # The image topic names can be remapped in the appropriate launch file
        self.image_sub = rospy.Subscriber("input_rgb_image", Image, self.image_callback, queue_size=1)
        self.depth_sub = rospy.Subscriber("input_depth_image", Image, self.depth_callback, queue_size=1)
Exemple #28
0
    temp1 = cv.CloneImage(frame)
    cv.Rectangle(temp1, point1, point2, color, 1)
    cv.PutText(temp1, "Place in box", (430, 240), font, color)
    cv.PutText(temp1, "then hit q", (430, 260), font, color)

    #taking snapshot after q is pressed
    if cv.WaitKey(10) == 113:
        flag = 1
        cv.SetImageROI(temp1, (300, 200, 100, 100))
        template = cv.CloneImage(temp1)
        cv.ResetImageROI(temp1)
        cv.DestroyWindow("Image")

    if flag == 0:
        cv.NamedWindow("Image", 1)
        cv.MoveWindow("Image", 300, 0)
        cv.ShowImage("Image", temp1)
        continue

    W, H = cv.GetSize(frame)
    w, h = cv.GetSize(template)

    width = W - w + 1
    height = H - h + 1
    result = cv.CreateImage((width, height), 32, 1)
    #matching the template by searching for the given region in the image
    cv.MatchTemplate(frame, template, result, cv.CV_TM_SQDIFF_NORMED)
    (min_x, max_y, minloc, maxloc) = cv.MinMaxLoc(result)
    (x, y) = minloc
    X1 = (x * 1366) / 640
    Y1 = (y * 768) / 480
Exemple #29
0
    def __init__(self):
        rospy.init_node('video2ros', anonymous=False)
        
        rospy.on_shutdown(self.cleanup)
        
        """ Define the input (path to video file) as a ROS parameter so it
            can be defined in a launch file or on the command line """
        self.input = rospy.get_param("~input", "")
        
        """ Define the image publisher with generic topic name "output" so that it can
            be remapped in the launch file. """
        image_pub = rospy.Publisher("output", Image)
        
        # The target frames per second for the video
        self.fps = rospy.get_param("~fps", 25)
        
        # Do we restart the video when the end is reached?
        self.loop = rospy.get_param("~loop", False)
        
        # Start the video paused?
        self.start_paused = rospy.get_param("~start_paused", False)
        
        # Show text feedback by default?
        self.show_text = rospy.get_param("~show_text", True)
        
        # Resize the original video?
        self.resize_video = rospy.get_param("~resize_video", False)
        
        # If self.resize_video is True, set the desired width and height here
        self.resize_width = rospy.get_param("~resize_width", 0)
        self.resize_height = rospy.get_param("~resize_height", 0)
        
        # Initialize a few variables
        self.paused = self.start_paused
        self.loop_video = True
        self.keystroke = None
        self.restart = False
        self.last_frame = None
        
        # Initialize the text font
        font_face = cv2.FONT_HERSHEY_SIMPLEX
        font_scale = 0.5
        
        # Define the capture object as pointing to the input file
        self.capture = cv2.VideoCapture(self.input)
        
        # Get the original frames per second
        fps = self.capture.get(cv.CV_CAP_PROP_FPS)
        
        # Get the original frame size
        self.frame_size = (self.capture.get(cv.CV_CAP_PROP_FRAME_HEIGHT), self.capture.get(cv.CV_CAP_PROP_FRAME_WIDTH))
        
        # Check that we actually have a valid video source
        if fps == 0.0:
            print "Video source", self.input, "not found!"
            return None        
        
        # Bring the fps up to the specified rate
        try:
            fps = int(fps * self.fps / fps)
        except:
            fps = self.fps
    
        # Create the display window
        cv.NamedWindow("Video Playback", True) # autosize the display
        cv.MoveWindow("Video Playback", 375, 25)

        # Create the CvBridge object
        bridge = CvBridge()
    
        # Enter the main processing loop
        while not rospy.is_shutdown():
            # Get the next frame from the video
            frame = self.get_frame()
            
            # Convert the frame to ROS format
            try:
                image_pub.publish(bridge.cv_to_imgmsg(cv.fromarray(frame), "bgr8"))
            except CvBridgeError, e:
                print e
            
            # Create a copy of the frame for displaying the text
            display_image = frame.copy()
            
            if self.show_text:
                cv2.putText(display_image, "FPS: " + str(self.fps), (10, 20), font_face, font_scale, cv.RGB(255, 255, 0))
                cv2.putText(display_image, "Keyboard commands:", (20, int(self.frame_size[0] * 0.6)), font_face, font_scale, cv.RGB(255, 255, 0))
                cv2.putText(display_image, " ", (20, int(self.frame_size[0] * 0.65)), font_face, font_scale, cv.RGB(255, 255, 0))
                cv2.putText(display_image, "space - toggle pause/play", (20, int(self.frame_size[0] * 0.72)), font_face, font_scale, cv.RGB(255, 255, 0))
                cv2.putText(display_image, "     r - restart video from beginning", (20, int(self.frame_size[0] * 0.79)), font_face, font_scale, cv.RGB(255, 255, 0))
                cv2.putText(display_image, "     t - hide/show this text", (20, int(self.frame_size[0] * 0.86)), font_face, font_scale, cv.RGB(255, 255, 0))
                cv2.putText(display_image, "     q - quit the program", (20, int(self.frame_size[0] * 0.93)), font_face, font_scale, cv.RGB(255, 255, 0))
            
            # Merge the original image and the display image (text overlay)
            display_image = cv2.bitwise_or(frame, display_image)
            
            # Now display the image.
            cv2.imshow("Video Playback", display_image)
                    
            """ Handle keyboard events """
            self.keystroke = cv.WaitKey(1000 / fps)

            """ Process any keyboard commands """
            if 32 <= self.keystroke and self.keystroke < 128:
                cc = chr(self.keystroke).lower()
                if cc == 'q':
                    """ user has press the q key, so exit """
                    rospy.signal_shutdown("User hit q key to quit.")
                elif cc == ' ':
                    """ Pause or continue the video """
                    self.paused = not self.paused
                elif cc == 'r':
                    """ Restart the video from the beginning """
                    self.restart = True
                elif cc == 't':
                    """ Toggle display of text help message """
                    self.show_text = not self.show_text        
Exemple #30
0
def setup_camera_window():
    cv.NamedWindow('camera', cv.CV_WINDOW_AUTOSIZE)
    cv.MoveWindow('camera', 10, 10)