OpenCV 各通道分析

OpenCV 各通道分析

问题:

  • 对于 RGB 三通道图像,当颜色为纯白色时其各通道颜色分布;
  • 当颜色为纯红/纯绿/纯蓝时其各通道颜色分布情况;

工具:PyCharmAnaconda


对于 RGB 三通道图像,当颜色为纯白色时其各通道颜色分布

对于 RGB 三通道图像,当颜色为纯白色时,其他通道颜色应该均为最大值(255),验证代码如下:

思路:生成三个 300x300 大小的值均为 255 的单通道图像,合并 3 个单通道图像,查看是否显示为纯白色图像

# -*- coding: utf-8 -*-

import cv2
import numpy as np


def merge_image(img_list):
    """
    合并单通道图像
    :param img_list:单通道图像列表
    :return: RGB彩色图像
    """
    dst = cv2.merge(img_list)

    print dst.shape
    print type(dst)

    cv2.imshow("img", dst)
    cv2.waitKey(0)

    return dst


def create_single_image(w, h):
    """
    生成单通道图像
    :param w:宽
    :param h:高
    :return:
    """
    s = (w, h)
    return np.ones(s, dtype=np.uint8) * 255


if __name__ == '__main__':
    li = []
    for i in range(3):
        li.append(create_single_image(300, 300))

    merge_image(li)

显示结果:

这里写图片描述

由生成图片可知,单通道图像均为 255 时,合并成 RGB 三通道图像确实为纯白色

关键函数介绍:

  1. cv2.merge

    Help on built-in function merge in module cv2:
    
    merge(...)
        merge(mv[, dst]) -> dst
    

    函数功能:合并多个单通道图像为一个图像,比如合并 cv2.merge([b, g, r]) -> bgr

  2. np.ones

    Help on function ones in module numpy.core.numeric:
    
    ones(shape, dtype=None, order='C')
        Return a new array of given shape and type, filled with ones.
    
        Parameters
        ----------
        shape : int or sequence of ints
            Shape of the new array, e.g., ``(2, 3)`` or ``2``.
        dtype : data-type, optional
            The desired data-type for the array, e.g., `numpy.int8`.  Default is
            `numpy.float64`.
        order : {'C', 'F'}, optional
            Whether to store multidimensional data in C- or Fortran-contiguous
            (row- or column-wise) order in memory.
    
        Returns
        -------
        out : ndarray
            Array of ones with the given shape, dtype, and order.
    
        See Also
        --------
        zeros, ones_like
    
        Examples
        --------
        >>> np.ones(5)
        array([ 1.,  1.,  1.,  1.,  1.])
    
        >>> np.ones((5,), dtype=np.int)
        array([1, 1, 1, 1, 1])
    
        >>> np.ones((2, 1))
        array([[ 1.],
               [ 1.]])
    
        >>> s = (2,2)
        >>> np.ones(s)
        array([[ 1.,  1.],
               [ 1.,  1.]])
    

    函数功能:返回给予宽高并赋值为 1 的数组

note:同样的,可以测试是否当单通道颜色为 0 时,其 RGB 图像为黑色

# -*- coding: utf-8 -*-

import cv2
import numpy as np


def merge_image(img_list):
    """
    合并单通道图像
    :param img_list:单通道图像列表
    :return: RGB彩色图像
    """
    dst = cv2.merge(img_list)

    print dst.shape
    print type(dst)

    cv2.imshow("img", dst)
    cv2.waitKey(0)

    return dst


def create_single_image(w, h):
    """
    生成单通道图像
    :param w:宽
    :param h:高
    :return:
    """
    s = (w, h)
    # return np.ones(s, dtype=np.uint8) * 255
    return np.zeros(s, dtype=np.uint8)


if __name__ == '__main__':
    li = []
    for i in range(3):
        li.append(create_single_image(300, 300))

    merge_image(li)

结果:

这里写图片描述


当颜色为纯红/纯绿/纯蓝时其各通道颜色分布情况

OpenCV 中,RGB 彩色图像的通道分布是 BGR

当图像为纯红,其 R 通道值应该为 255 ,其余通道值为 0,以此类推。验证代码如下:

# -*- coding: utf-8 -*-

import cv2
import numpy as np


def merge_image(img_name, img_list):
    """
    合并单通道图像
    :param img_name:图像名
    :param img_list:单通道图像列表
    :return: RGB彩色图像
    """
    dst = cv2.merge(img_list)

    print dst.shape
    print type(dst)

    cv2.imshow(img_name, dst)
    cv2.waitKey(0)

    return dst


def create_single_image(w, h):
    """
    生成单通道图像
    :param w:宽
    :param h:高
    :return:
    """
    s = (w, h)
    return np.ones(s, dtype=np.uint8) * 255


def create_single_image2(w, h):
    """
    生成单通道图像
    :param w:宽
    :param h:高
    :return:
    """
    s = (w, h)
    return np.zeros(s, dtype=np.uint8)


if __name__ == '__main__':
    img1 = create_single_image(300, 300)  # 值为255
    img2 = create_single_image2(300, 300)  # 值为0
    img3 = create_single_image2(300, 300)  # 值为0

    merge_image('纯蓝'.decode('utf-8').encode('gbk'), [img1, img2, img3])  # 纯蓝
    merge_image('纯红'.decode('utf-8').encode('gbk'), [img2, img3, img1])  # 纯红
    merge_image('纯绿'.decode('utf-8').encode('gbk'), [img3, img1, img2])  # 纯绿

实验结果:

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值