How to use the perception.image.ColorImage function in Perception

To help you get started, we’ve selected a few Perception examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github BerkeleyAutomation / perception / perception / image.py View on Github external
min_i = box.min_pt[1]
        min_j = box.min_pt[0]
        max_i = box.max_pt[1]
        max_j = box.max_pt[0]

        # draw the vertical lines
        for j in range(min_j, max_j):
            box_data[min_i, j, :] = BINARY_IM_MAX_VAL * np.ones(self.channels)
            box_data[max_i, j, :] = BINARY_IM_MAX_VAL * np.ones(self.channels)

        # draw the horizontal lines
        for i in range(min_i, max_i):
            box_data[i, min_j, :] = BINARY_IM_MAX_VAL * np.ones(self.channels)
            box_data[i, max_j, :] = BINARY_IM_MAX_VAL * np.ones(self.channels)

        return ColorImage(box_data, self._frame)
github BerkeleyAutomation / perception / perception / image.py View on Github external
def bgr2rgb(self):
        """ Converts data using the cv conversion. """
        new_data = cv2.cvtColor(self.raw_data, cv2.COLOR_BGR2RGB)
        return ColorImage(new_data, frame=self.frame, encoding='rgb8')
github BerkeleyAutomation / perception / perception / image.py View on Github external
size : int, float, or tuple
            * int   - Percentage of current size.
            * float - Fraction of current size.
            * tuple - Size of the output image.

        interp : :obj:`str`, optional
            Interpolation to use for re-sizing ('nearest', 'lanczos', 'bilinear',
            'bicubic', or 'cubic')

        Returns
        -------
        :obj:`ColorImage`
            The resized image.
        """
        resized_data = imresize(self.data, size, interp=interp).astype(np.uint8)
        return ColorImage(resized_data, self._frame)
github BerkeleyAutomation / perception / perception / image.py View on Github external
if not Image.can_convert(x):
            raise ValueError('Cannot convert array to an Image!')

        dtype = x.dtype
        height = x.shape[0]
        width = x.shape[1]
        channels = 1
        if len(x.shape) == 3:
            channels = x.shape[2]
        if dtype == np.uint8:
            if channels == 1:
                if np.any((x % BINARY_IM_MAX_VAL) > 0):
                    return GrayscaleImage(x, frame)
                return BinaryImage(x, frame)
            elif channels == 3:
                return ColorImage(x, frame)
            else:
                raise ValueError(
                    'No available image conversion for uint8 array with 2 channels')
        elif dtype == np.uint16:
            if channels != 1:
                raise ValueError(
                    'No available image conversion for uint16 array with 2 or 3 channels')
            return GrayscaleImage(x, frame)
        elif dtype == np.float32 or dtype == np.float64:
            if channels == 1:
                return DepthImage(x, frame)
            elif channels == 2:
                return GdImage(x, frame)
            elif channels == 3:
                logging.warning('Converting float array to uint8')
                return ColorImage(x.astype(np.uint8), frame)
github BerkeleyAutomation / perception / perception / image.py View on Github external
----------
        filename : :obj:`str`
            The file to load the data from. Must be one of .png, .jpg,
            .npy, or .npz.

        frame : :obj:`str`
            A string representing the frame of reference in which the new image
            lies.

        Returns
        -------
        :obj:`ColorImage`
            The new color image.
        """
        data = Image.load_data(filename).astype(np.uint8)
        return ColorImage(data, frame)
github BerkeleyAutomation / perception / perception / image.py View on Github external
Returns
        -------
        :obj:`ColorImage`
            color image to visualize the image difference
        """
        red = np.array([BINARY_IM_MAX_VAL, 0, 0])
        yellow = np.array([BINARY_IM_MAX_VAL, BINARY_IM_MAX_VAL, 0])
        green = np.array([0, BINARY_IM_MAX_VAL, 0])
        overlap_data = np.zeros([self.height, self.width, 3])
        unfilled_px = np.where((self.data == 0) & (binary_im.data > 0))
        overlap_data[unfilled_px[0], unfilled_px[1], :] = red
        filled_px = np.where((self.data > 0) & (binary_im.data > 0))
        overlap_data[filled_px[0], filled_px[1], :] = green
        spurious_px = np.where((self.data > 0) & (binary_im.data == 0))
        overlap_data[spurious_px[0], spurious_px[1], :] = yellow
        return ColorImage(overlap_data.astype(np.uint8), frame=self.frame)
github BerkeleyAutomation / perception / perception / image.py View on Github external
Parameters
        ----------
        binary_im : :obj:`BinaryImage`
            A BinaryImage of the same size as this image, with pixel values of either
            zero or one. Wherever this image has zero pixels, we'll zero out the
            pixels of the new image.

        Returns
        -------
        :obj:`Image`
            A new Image of the same type, masked by the given binary image.
        """
        data = np.copy(self._data)
        ind = np.where(binary_im.data == 0)
        data[ind[0], ind[1], :] = 0.0
        return ColorImage(data, self._frame)
github BerkeleyAutomation / perception / perception / image.py View on Github external
Returns
        -------
        :obj:`ColorImage`
            color image with zero pixels filled in
        """
        # get original shape
        orig_shape = (self.height, self.width)
        
        # resize the image
        resized_data = self.resize(rescale_factor, interp='nearest').data

        # inpaint smaller image
        mask = 1 * (np.sum(resized_data, axis=2) == 0)
        inpainted_data = cv2.inpaint(resized_data, mask.astype(np.uint8),
                                     win_size, cv2.INPAINT_TELEA)
        inpainted_im = ColorImage(inpainted_data, frame=self.frame)

        # fill in zero pixels with inpainted and resized image
        filled_data = inpainted_im.resize(
            orig_shape, interp='bilinear').data
        new_data = self.data
        new_data[self.data == 0] = filled_data[self.data == 0]
        return ColorImage(new_data, frame=self.frame)
github BerkeleyAutomation / perception / perception / image.py View on Github external
Returns
        -------
        :obj:`ColorImage`
            color image with cols swapped
        """
        if len(channel_swap) != 2:
            raise ValueError('Illegal value for channel swap')
        ci = channel_swap[0]
        cj = channel_swap[1]
        if ci < 0 or ci > 2 or cj < 0 or cj > 2:
            raise ValueError('Channels must be between 0 and 1')
        new_data = self.data.copy()
        new_data[:, :, ci] = self.data[:, :, cj]
        new_data[:, :, cj] = self.data[:, :, ci]
        return ColorImage(new_data, frame=self._frame)