How to use the perception.image.BinaryImage 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
""" Takes AND operation with other binary image.

        Parameters
        ----------
        binary_im : :obj:`BinaryImage`
            binary image for and operation

        Returns
        -------
        :obj:`BinaryImage`
            AND of this binary image and other image
        """
        data = np.copy(self._data)
        ind = np.where(binary_im.data == 0)
        data[ind[0], ind[1], ...] = 0
        return BinaryImage(data, self._frame)
github BerkeleyAutomation / perception / perception / image.py View on Github external
Serves as a mask for invalid pixels.

        Returns
        -------
        :obj:`BinaryImage`
            Binary image where a pixel value greater than zero indicates an invalid pixel.
        """
        # init mask buffer
        mask = np.zeros([self.height, self.width, 1]).astype(np.uint8)

        # update invalid pixels
        zero_pixels = self.zero_pixels()
        nan_pixels = self.nan_pixels()
        mask[zero_pixels[:, 0], zero_pixels[:, 1]] = BINARY_IM_MAX_VAL
        mask[nan_pixels[:, 0], nan_pixels[:, 1]] = BINARY_IM_MAX_VAL
        return BinaryImage(mask, frame=self.frame)
github BerkeleyAutomation / perception / perception / image.py View on Github external
pruned_data = np.zeros([self.height, self.width, 3])
        for contour in pruned_contours:
            cv2.fillPoly(
                pruned_data,
                pts=[contour],
                color=(
                    BINARY_IM_MAX_VAL,
                    BINARY_IM_MAX_VAL,
                    BINARY_IM_MAX_VAL))
        pruned_data = pruned_data[:, :, 0]  # convert back to one channel

        # preserve topology of original image
        if preserve_topology:
            orig_zeros = np.where(self.data == 0)
            pruned_data[orig_zeros[0], orig_zeros[1]] = 0
        return BinaryImage(pruned_data.astype(np.uint8), self._frame)
github BerkeleyAutomation / perception / perception / image.py View on Github external
bgmodel = self.background_model(ignore_black=ignore_black,
                                            use_hsv=use_hsv,
                                            scale=scale)

        # get the bounds
        lower_bound = np.array(
            [bgmodel[i] - tolerance for i in range(self.channels)])
        upper_bound = np.array(
            [bgmodel[i] + tolerance for i in range(self.channels)])
        orig_zero_indices = np.where(np.sum(self._data, axis=2) == 0)

        # threshold
        binary_data = cv2.inRange(self.data, lower_bound, upper_bound)
        binary_data[:, :, ] = (BINARY_IM_MAX_VAL - binary_data[:, :, ])
        binary_data[orig_zero_indices[0], orig_zero_indices[1], ] = 0.0
        binary_im = BinaryImage(binary_data.astype(np.uint8), frame=self.frame)
        return binary_im
github BerkeleyAutomation / perception / perception / image.py View on Github external
""" Takes OR operation with other binary image.

        Parameters
        ----------
        binary_im : :obj:`BinaryImage`
            binary image for and operation

        Returns
        -------
        :obj:`BinaryImage`
            OR of this binary image and other image
        """
        data = np.copy(self._data)
        ind = np.where(binary_im.data > 0)
        data[ind[0], ind[1], ...] = BINARY_IM_MAX_VAL
        return BinaryImage(data, self._frame)
github BerkeleyAutomation / perception / perception / image.py View on Github external
def contour_mask(self, contour):
        """ Generates a binary image with only the given contour filled in. """
        # fill in new data
        new_data = np.zeros(self.data.shape)
        num_boundary = contour.boundary_pixels.shape[0]
        boundary_px_ij_swapped = np.zeros([num_boundary, 1, 2])
        boundary_px_ij_swapped[:, 0, 0] = contour.boundary_pixels[:, 1]
        boundary_px_ij_swapped[:, 0, 1] = contour.boundary_pixels[:, 0]
        cv2.fillPoly(
            new_data, pts=[
                boundary_px_ij_swapped.astype(
                    np.int32)], color=(
                BINARY_IM_MAX_VAL, BINARY_IM_MAX_VAL, BINARY_IM_MAX_VAL))
        orig_zeros = np.where(self.data == 0)
        new_data[orig_zeros[0], orig_zeros[1]] = 0
        return BinaryImage(new_data.astype(np.uint8), frame=self._frame)
github BerkeleyAutomation / perception / perception / image.py View on Github external
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:`BinaryImage`
            The new binary image.
        """
        data = Image.load_data(filename)
        if len(data.shape) > 2 and data.shape[2] > 1:
            data = data[:, :, 0]
        return BinaryImage(data, frame)
github BerkeleyAutomation / perception / perception / image.py View on Github external
def segment_mask(self, segnum):
        """ Returns a binary image of just the segment corresponding to the given number.

        Parameters
        ----------
        segnum : int
            the number of the segment to generate a mask for

        Returns
        -------
        :obj:`BinaryImage`
             binary image data
        """
        binary_data = np.zeros(self.shape)
        binary_data[self.data == segnum] = BINARY_IM_MAX_VAL
        return BinaryImage(binary_data.astype(np.uint8), frame=self.frame)
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:`BinaryImage`
            The resized image.
        """
        resized_data = imresize(self.data.astype(np.float32), size, interp=interp)
        return BinaryImage(resized_data.astype(np.uint8), self._frame)
github BerkeleyAutomation / perception / perception / image.py View on Github external
def inverse(self):
        """ Inverts image (all nonzeros become zeros and vice verse)
        Returns
        -------
        :obj:`BinaryImage`
            inverse of this binary image
        """
        data = np.zeros(self.shape).astype(np.uint8)
        ind = np.where(self.data == 0)
        data[ind[0], ind[1], ...] = BINARY_IM_MAX_VAL
        return BinaryImage(data, self._frame)