Ejemplo n.º 1
0
    def make_frame(self):
        if not (self.ow_img is None or self.uvc_img is None):
            #### Create Image ####
            sz = (160, 120)
            img_left = cv2.resize(self.ow_img, sz)
            img_right = cv2.resize(self.uvc_img, sz)
            montage = ResultsMontage((sz[1], sz[0]), 1, 2)
            montage.addResult(img_left)
            montage.addResult(img_right)
            msg = Image()
            msg.header.stamp = rospy.Time.now()
            msg.encoding = 'bgr8'
            msg.step = montage.montage.shape[1] * 3
            msg.height = montage.montage.shape[0]
            msg.width = montage.montage.shape[1]
            msg.data = np.array(montage.montage).tostring()
            # Publish new image
            self.image_pub.publish(msg)
            msg = CompressedImage()
            msg.header.stamp = rospy.Time.now()
            msg.format = 'jpeg'
            msg.data = np.array(cv2.imencode('.jpg',
                                             montage.montage)[1]).tostring()
            self.image_pub_jpg.publish(msg)
            montage = ResultsMontage((sz[1], sz[0]), 2, 2)
            montage.addResult(img_left)
            montage.addResult(img_right)
            msg = Image()
            msg.header.stamp = rospy.Time.now()
            msg.encoding = 'bgr8'
            msg.step = montage.montage.shape[1] * 3
            msg.height = montage.montage.shape[0]
            msg.width = montage.montage.shape[1]
            msg.data = np.array(montage.montage).tostring()
            # Publish new image
            self.image_pub_h.publish(msg)
            msg = CompressedImage()
            msg.header.stamp = rospy.Time.now()
            msg.format = 'jpeg'
            msg.data = np.array(cv2.imencode('.jpg',
                                             montage.montage)[1]).tostring()
            self.image_pub_jpg_h.publish(msg)

            self.ow_img = None
            self.uvc_img = None
    def callback():
        #Change this to a loop over all image files in the rgb folder
        rgb_filenames = [
            os.path.join(args.input_dir, f)
            for f in list(os.walk(args.input_dir))[0][2]
        ]
        depth_filenames = [
            os.path.join(args.output_dir, f)
            for f in list(os.walk(args.output_dir))[0][2]
        ]
        print("Reading images and converting them to the correct size.")

        process_time = 0
        image_saving_time = 0
        print("Converting to depth images and storing in the output directory")

        for i in range(len(rgb_filenames)):
            rgb_image = scipy.misc.imread(rgb_filenames[i], mode="RGB")
            original_height, original_width, num_channels = input_image.shape
            input_image = scipy.misc.imresize(
                input_image, [args.input_height, args.input_width],
                interp='lanczos')

            depth_image = scipy.misc.imread(depth_filenames[i])

            #Form the rgb message
            msg_rgb = Image()
            msg_rgb.header.stamp = rospy.Time.now()
            msg_rgb.format = "png"
            msg_rgb.data = np.array(cv2.imencode('.png',
                                                 rgb_image)[1]).tostring()

            #Form the depth message
            msg_depth = Image()
            msg_depth.header.stamp = rospy.Time.now()
            msg_depth.format = "png"
            msg_depth.data = np.array(cv2.imencode('.png',
                                                   depth_image)[1]).tostring()

            # Publish both images
            print("Publishing rgb image")
            self.image_pub_rgb.publish(msg_rgb)
            print("Publishing depth image")
            self.image_pub_depth.publish(msg_depth)
Ejemplo n.º 3
0
def image_process(image, params):
    #bridge = CvBridge()
    lower = params['lowerY']
    upper = params['upperY']
    debug_info = params['debug_info']
    global error
    try:
        #cv_image = bridge.imgmsg_to_cv2(image)
        raw_data = np.fromstring(image.data, np.uint8)
        cv_image = cv2.imdecode(raw_data, cv2.IMREAD_COLOR)
        gray = cv2.imdecode(raw_data, cv2.IMREAD_GRAYSCALE)
        _, gray = cv2.threshold(gray, 250, 255, cv2.THRESH_BINARY)
        y_len, x_len = gray.shape
        lower, upper = max(0, lower), min(upper, y_len)
        if debug_info: print(lower, upper, 'image crop in Y')
        gray = gray[lower:upper, :]
        y_len, x_len = gray.shape
        total_error = []
        for y in range(10, y_len, 90):
            weighted_mass = 0
            mass = 0
            for x in range(0, x_len):
                if gray[y, x] == 255:
                    mass += 1
                    weighted_mass += x
            if mass > 0:
                final_x = int(weighted_mass / mass)
                total_error.append(
                    float(final_x - x_len // 2) / float(x_len // 2))
                if params['display_processed_image']:
                    cv2.rectangle(gray, (final_x - 10, y),
                                  (final_x + 10, y + 35), 120, 2)
        #cv2.imshow('wooho', gray)

        if debug_info: print(total_error)
        if len(total_error) > 2:
            error = sum(total_error) / len(total_error)

        if params['display_image']: cv2.imshow('raw', cv_image)
        if params['display_processed_image']: cv2.imshow('processed', gray)
        if params['publish_processed_image']:
            msg = Image()
            msg.header.stamp = rospy.Time.now()
            msg.format = "jpeg"
            msg.data = np.array(cv2.imencode('.jpg', gray)[1]).tostring()
            params['img_pub'].publish(msg)

        cv2.waitKey(1)

    except Exception as e:
        if debug_info:
            print(e)
Ejemplo n.º 4
0
def image_process(image, params):
    bridge = CvBridge()
    global error
    try:
        cv_image = bridge.imgmsg_to_cv2(image, desired_encoding="bgr8")
        lower = np.array([110, 50, 50], dtype="uint8")
        upper = np.array([130, 255, 255], dtype="uint8")
        hsv = cv2.cvtColor(cv_image, cv2.COLOR_BGR2HSV)
        mask = cv2.inRange(cv_image, lower, upper)
        #mask = cv2.erode(mask, None, iterations=2)
        #mask = cv2.dilate(mask, None, iterations=2)
        output = cv2.bitwise_and(cv_image, cv_image, mask=mask)
        #output = mask
        #cnts = cv2.findCountours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]

        #raw_data = np.fromstring(image.data, np.uint8)
        #cv_image = cv2.imdecode(raw_data, cv2.IMREAD_COLOR)
        #cv_image = cv2.cvtColor(cv_image, cv2.COLOR_HSV2BGR)
        #print(reds.shape, cv_image.shape, len(cv_image))
        #cv2.imshow('wooho', reds)
        #reds = cv_image[:, :, 2].copy()
        #print(reds.shape)
        #_, reds = cv2.threshold(reds, 250, 255, cv2.THRESH_BINARY)
        #print('here')
        cv2.imshow('raw', cv_image)
        cv2.imshow('mask', mask)
        cv2.imshow('output', output)
        cv2.waitKey(1)
        return

        if debug_info: print(total_error)
        if len(total_error) > 2:
            error = sum(total_error) / len(total_error)

        if params['display_image']: cv2.imshow('raw', cv_image)
        if params['display_processed_image']: cv2.imshow('processed', gray)
        if params['publish_processed_image']:
            msg = Image()
            msg.header.stamp = rospy.Time.now()
            msg.format = "jpeg"
            msg.data = np.array(cv2.imencode('.jpg', gray)[1]).tostring()
            params['img_pub'].publish(msg)

    except Exception as e:
        print(e)