• BGR 顺序中的 OpenCV-color


    1. import os
    2. import numpy as np
    3. import argparse
    4. import cv2
    5. import matplotlib.pyplot as plt

    原始图片如下所示:

    34bb745187414ddca9463ace08cd445c.png

    OpenCV 假设图像是 BGR 通道顺序。OpenCV 的 imread、imwrite 和 imshow 都使用 BGR 顺序,所以如果我们使用 cv2.imshow 显示图像,图像不会改变。但它不适用于 matplotlib。

    大多数图像处理库都使用 RGB 排序,例如 matplotlib,因此如果使用plt.imshow,logo的颜色会发生变化。

    1. img = cv2.imread("logo.png")
    2. # show the image by cv2
    3. # The cv2.imshow() and cv.imshow() functions from the opencv-
    4. python package are incompatible with Jupyter notebook; 
    5. # see https://github.com/jupyter/notebook/issues/3935. 
    6. # As a replacement, you can use the following function:
    7. from google.colab.patches import cv2_imshow
    8. cv2_imshow(img)
    09259c07851500534dadfc92743f999b.png
    1. # show the image by matplotlib
    2. plt.subplot(111)
    3. plt.imshow(img)
    4. plt.title("Original")
    711a25770e70f7be50d8e1dbc78dd3ae.png

    如果我们想在 OpenCV 中以 RGB 顺序读取图像,我们可以使用:

    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    1. # read images in RGB order in OpenCV
    2. img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    3. from google.colab.patches import cv2_imshow
    4. cv2_imshow(img_rgb)
    5. plt.imshow(img_rgb)
    13b987ccac19c127f76514ab9b1b3aff.png

    cv2.imread(FILENAME, FLAG)

    FLAG有一些值:

    cv2.IMREAD_UNCHANGED:按原样从源读取图像(带 alpha 通道)。如果源图像是 RGB,它将图像加载到具有红色、绿色和蓝色通道的数组中。

    cv2.IMREAD_COLOR:将图像转换为3通道BGR彩色图像,但没有透明通道

    cv2.IMREAD_GRAYSCALE:将图像转换为单通道灰度图像

    在此处查看更多标志:https://docs.opencv.org/3.4/d8/d6a/group__imgcodecs__flags.html#ga61d9b0126a3e57d9277ac48327799c80

    1. img = cv2.imread('logo.png', cv2.IMREAD_UNCHANGED)
    2. cv2_imshow(img)
    3. plt.imshow(img)
    e15eaa1e8c3cfb77fda675f30aad0546.png
    1. img = cv2.imread('logo.png', cv2.IMREAD_COLOR)
    2. cv2_imshow(img)
    3. plt.imshow(img)
    8e369ed88c2aed0012d01a20c489deb7.png
    1. img = cv2.imread('logo.png', cv2.IMREAD_GRAYSCALE)
    2. cv2_imshow(img)
    459589305cc4e74db8cba55a4d84bf30.png

    让我们看一下BGR频道。如果我们想打印整个数组,那么设置np.set_printoptions(threshold=np.inf),并在打印数组后将其设置为默认值。np.set_printoptions(threshold=1000

    1. img = cv2.imread('logo.png', cv2.IMREAD_COLOR)
    2. cv2_imshow(img)
    3. b = img[:,:,0] # get blue channel
    4. g = img[:,:,1] # get green channel
    5. r = img[:,:,2] # get red channel
    6. print(b)
    outside_default.png
    1. np.set_printoptions(threshold=np.inf)
    2. print(b)
    3. np.set_printoptions(threshold=1000)
    4. # the output is really long so I won't print it here.

    现在让我们将其转换为RGB通道。即使我们以 RGB 顺序读取图像,但如果我们用cv2.imshow显示图像,logo颜色也会发生变化,因为它适用于BGR顺序。plt.imshow显示图像的原始颜色,因为它也适用于RGB顺序。

    1. # read images in RGB order in OpenCV
    2. img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    3. from google.colab.patches import cv2_imshow
    4. cv2_imshow(img_rgb)
    5. plt.imshow(img_rgb)
    9567a62a720de3ecdb52154ee98f5116.png
    1. r1 = img_rgb[:,:,0] # get blue channel
    2. g1 = img_rgb[:,:,1] # get green channel
    3. b1 = img_rgb[:,:,2] # get red channel
    4. (img[:,:,0] == img_rgb[:,:,2]).all()
    Output: True

    当你尝试在 OpenCV 中绘制一个矩形框时,请注意矩形的颜色也是按BGR顺序排列的。

    1. # read the image
    2. image = cv2.imread('logo.png')
    3. # represents the top left corner of rectangle
    4. start_point = (55)
    5. # represents the bottom right corner of rectangle
    6. end_point = (2020)
    7. # choose the rectangle color in BGR
    8. color = (00255) # red
    9. # thickness of lines that make up the rectangle is 2 px
    10. thickness = 2
    11. # draw a rectangle with red line borders of thickness of 2 px
    12. image = cv2.rectangle(image, start_point, end_point, color, thickness)
    13. # Displaying the image 
    14. cv2_imshow(image)
    fd2fd691aaa4b176ef9091abef9156e6.png

    ☆ END ☆

    如果看到这里,说明你喜欢这篇文章,请转发、点赞。微信搜索「uncle_pn」,欢迎添加小编微信「 woshicver」,每日朋友圈更新一篇高质量博文。

    扫描二维码添加小编↓

    983b22646d2d76e0e4cc6b56197c408c.jpeg

  • 相关阅读:
    Linux网络:HTTP协议
    Redis学习记录------Redis6持久化操作(十)
    RabbitMQ的Confirm机制
    侧链到底是什么
    java系列之 页面打印出 [object Object],[object Object]
    Go语言各种扩容机制(防止混淆)
    Vue项目预览
    一文教会你如何用 Python 分割合并大文件
    力扣(LeetCode)54. 螺旋矩阵(C++)
    《Head First HTML5 javascript》第9章 认识对象
  • 原文地址:https://blog.csdn.net/woshicver/article/details/127711643