Python cv2 模块,IMREAD_ANYCOLOR 实例源码

我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用cv2.IMREAD_ANYCOLOR

项目:GulpIO    作者:TwentyBN    | 项目源码 | 文件源码
def resize_by_short_edge(img, size):
    if isinstance(img, str):
        img_path = img
        img = cv2.imread(img_path, cv2.IMREAD_ANYCOLOR)
        if img is None:
            raise ImageNotFound("Image read None from path ", img_path)
    if size < 1:
        return img
    h, w = img.shape[0], img.shape[1]
    if h < w:
        scale = w / float(h)
        new_width = int(size * scale)
        img = cv2.resize(img, (new_width, size))
    else:
        scale = h / float(w)
        new_height = int(size * scale)
        img = cv2.resize(img, (size, new_height))
    return img
项目:didi-competition    作者:udacity    | 项目源码 | 文件源码
def write_image(self, outdir, msg, fmt='png'):
        results = {}
        image_filename = os.path.join(outdir, str(msg.header.stamp.to_nsec()) + '.' + fmt)
        try:
            if hasattr(msg, 'format') and 'compressed' in msg.format:
                buf = np.ndarray(shape=(1, len(msg.data)), dtype=np.uint8, buffer=msg.data)
                cv_image = cv2.imdecode(buf, cv2.IMREAD_ANYCOLOR)
                if cv_image.shape[2] != 3:
                    print("Invalid image %s" % image_filename)
                    return results
                results['height'] = cv_image.shape[0]
                results['width'] = cv_image.shape[1]
                # Avoid re-encoding if we don't have to
                if check_image_format(msg.data) == fmt:
                    buf.tofile(image_filename)
                else:
                    cv2.imwrite(image_filename, cv_image)
            else:
                cv_image = self.bridge.imgmsg_to_cv2(msg, "bgr8")
                cv2.imwrite(image_filename, cv_image)
        except CvBridgeError as e:
            print(e)
        results['filename'] = image_filename
        return results
项目:async_face_recognition    作者:dpdornseifer    | 项目源码 | 文件源码
def _cascade_detect(self, raw_image):
        ''' use opencv cascades to recognize objects on the incomming images '''
        cascade = cv2.CascadeClassifier(self._cascade)
        image = np.asarray(bytearray(raw_image), dtype="uint8")

        gray_image = cv2.imdecode(image, cv2.IMREAD_GRAYSCALE)
        color_image = cv2.imdecode(image, cv2.IMREAD_ANYCOLOR)

        coordinates = cascade.detectMultiScale(
            gray_image,
            scaleFactor=1.15,
            minNeighbors=5,
            minSize=(30, 30)
        )

        for (x, y, w, h) in coordinates:
            cv2.rectangle(color_image, (x, y), (x + w, y + h), (0, 255, 0), 2)
            self._logger.debug("face recognized at: x: {}, y: {}, w: {}, h: {}".format(x, y, w, h))

        return color_image, self._tojson(coordinates)
项目:pybot    作者:spillai    | 项目源码 | 文件源码
def compressed_imgmsg_to_cv2(cmprs_img_msg, desired_encoding = "passthrough"):
    """
    Convert a sensor_msgs::CompressedImage message to an OpenCV :cpp:type:`cv::Mat`.

    :param cmprs_img_msg:   A :cpp:type:`sensor_msgs::CompressedImage` message
    :param desired_encoding:  The encoding of the image data, one of the following strings:

       * ``"passthrough"``
       * one of the standard strings in sensor_msgs/image_encodings.h

    :rtype: :cpp:type:`cv::Mat`
    :raises CvBridgeError: when conversion is not possible.

    If desired_encoding is ``"passthrough"``, then the returned image has the same format as img_msg.
    Otherwise desired_encoding must be one of the standard image encodings

    This function returns an OpenCV :cpp:type:`cv::Mat` message on success, or raises :exc:`cv_bridge.CvBridgeError` on failure.

    If the image only has one channel, the shape has size 2 (width and height)
    """
    str_msg = cmprs_img_msg.data
    buf = np.ndarray(shape=(1, len(str_msg)),
                      dtype=np.uint8, buffer=cmprs_img_msg.data)
    im = cv2.imdecode(buf, cv2.IMREAD_ANYCOLOR)

    if desired_encoding == "passthrough":
        return im

    try:
        res = cvtColor2(im, "bgr8", desired_encoding)
    except RuntimeError as e:
        raise CvBridgeError(e)

    return res
项目:imgProcessor    作者:radjkarl    | 项目源码 | 文件源码
def imread(img, color=None, dtype=None):
    '''
    dtype = 'noUint', uint8, float, 'float', ...
    '''
    COLOR2CV = {'gray': cv2.IMREAD_GRAYSCALE,
                'all': cv2.IMREAD_COLOR,
                None: cv2.IMREAD_ANYCOLOR
                }
    c = COLOR2CV[color]
    if callable(img):
        img = img()
    elif isinstance(img, string_types):
        #         from_file = True
        #         try:
        #             ftype = img[img.find('.'):]
        #             img = READERS[ftype](img)[0]
        #         except KeyError:
        # open with openCV
        # grey - 8 bit
        if dtype in (None, "noUint") or np.dtype(dtype) != np.uint8:
            c |= cv2.IMREAD_ANYDEPTH
        img2 = cv2.imread(img, c)
        if img2 is None:
            raise IOError("image '%s' is not existing" % img)
        img = img2

    elif color == 'gray' and img.ndim == 3:  # multi channel img like rgb
        # cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #cannot handle float64
        img = toGray(img)
    # transform array to uint8 array due to openCV restriction
    if dtype is not None:
        if isinstance(img, np.ndarray):
            img = _changeArrayDType(img, dtype, cutHigh=False)

    return img
项目:dataArtist    作者:radjkarl    | 项目源码 | 文件源码
def open(self, filename):
        p = self.preferences
        # open in 8 bit?
        if p.p8bit.value():
            col = 0
        else:
            col = cv2.IMREAD_ANYDEPTH
        if p.pGrey.value() and not p.pSplitColors.value():
            col = col | cv2.IMREAD_GRAYSCALE
        else:
            col |= cv2.IMREAD_ANYCOLOR

        # OPEN
        img = cv2.imread(str(filename), col)  # cv2.IMREAD_UNCHANGED)
        if img is None:
            raise Exception("image '%s' doesn't exist" % filename)

        # crop
        if p.pCrop.value():
            r = (p.pCropX0.value(),
                 p.pCropX1.value(),
                 p.pCropY0.value(),
                 p.pCropY1.value())
            img = img[r[0]:r[1], r[2]:r[3]]

        # resize
        if p.pResize.value():
            img = cv2.resize(img, (p.pResizeX.value(), p.pResizeY.value()))

        labels = None
        if img.ndim == 3:
            if p.pSplitColors.value():
                img = np.transpose(img, axes=(2, 0, 1))
                labels = ['blue', 'green', 'red']
            else:
                # rgb convention
                img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

        # change data type to float
        img = self.toFloat(img)
        return img, labels
项目:GulpIO    作者:TwentyBN    | 项目源码 | 文件源码
def read_frames(self, id_, slice_=None):
        """ Read frames for a single item.

        Parameters
        ----------
        id_: (str)
            The ID of the item
        slice_: (slice:
            A slice with which to select frames.
        Returns
        -------
        frames (int), meta(dict)
            The frames of the item as a list of numpy arrays consisting of
            image pixel values. And the metadata.

        """
        frame_infos, meta_data = self._get_frame_infos(id_)
        frames = []
        slice_element = slice_ or slice(0, len(frame_infos))

        def extract_frame(frame_info):
            self.fp.seek(frame_info.loc)
            record = self.fp.read(frame_info.length)
            img_str = record[:len(record)-frame_info.pad]
            nparr = np.fromstring(img_str, np.uint8)
            img = cv2.imdecode(nparr, cv2.IMREAD_ANYCOLOR)
            if img.ndim > 2:
                img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            return img
        frames = [extract_frame(frame_info)
                  for frame_info in frame_infos[slice_element]]
        return frames, meta_data
项目:GulpIO    作者:TwentyBN    | 项目源码 | 文件源码
def resize_images(imgs, img_size=-1):
    for img in imgs:
        img_path = img
        img = cv2.imread(img_path, cv2.IMREAD_ANYCOLOR)
        if img is None:
            raise ImageNotFound("Image is  None from path:{}".format(img_path))
        if img_size > 0:
            img = resize_by_short_edge(img, img_size)
        yield img
项目:diy_driverless_car_ROS    作者:wilselby    | 项目源码 | 文件源码
def write_image(bridge, outdir, msg, fmt='png'):
    results = {}
    image_filename = os.path.join(outdir, str(msg.header.stamp.to_nsec()) + '.' + fmt)
    try:
        if hasattr(msg, 'format') and 'compressed' in msg.format:
            buf = np.ndarray(shape=(1, len(msg.data)), dtype=np.uint8, buffer=msg.data)
            cv_image = cv2.imdecode(buf, cv2.IMREAD_ANYCOLOR)
            if cv_image.shape[2] != 3:
                print("Invalid image %s" % image_filename)
                return results
            results['height'] = cv_image.shape[0]
            results['width'] = cv_image.shape[1]
            # Avoid re-encoding if we don't have to
            if check_format(msg.data) == fmt:
                buf.tofile(image_filename)
            else:
                cv2.imwrite(image_filename, cv_image)
        else:
            cv_image = bridge.imgmsg_to_cv2(msg, "bgr8")
            cv2.imwrite(image_filename, cv_image)
    except CvBridgeError as e:
        print(e)
    results['filename'] = image_filename
    return results

#sensor_msgs/Image