pyplot学习笔记(一):使用scatter画彩色图像三维散点图,点坐标为RGB值,点颜色为RGB值决定的颜色(彩色图像色彩分布图)

您所在的位置:网站首页 颜色构成图 pyplot学习笔记(一):使用scatter画彩色图像三维散点图,点坐标为RGB值,点颜色为RGB值决定的颜色(彩色图像色彩分布图)

pyplot学习笔记(一):使用scatter画彩色图像三维散点图,点坐标为RGB值,点颜色为RGB值决定的颜色(彩色图像色彩分布图)

2024-07-17 14:13| 来源: 网络整理| 查看: 265

前言

  前段时间朋友叫我帮他画一个彩色图像色彩分布图。也就是以彩色图像的RGB值为坐标值,然后用该RGB值对应的颜色来标点,以此形成一幅色彩分布图。pyplot是一个很丰富的可视化工具,笔者趁机学习了一些相关的简单操作,坐此笔记供以后查阅。

subplot()一瞥 某些参数

  *args :确定子图位置的参数,如(2, 2, 3)=223,表示子图将出现在被划分成两行两列的窗口的第3个网格的位置。   projection:投影方式,坐标类型,如二维直角坐标系:'rectilinear'(=默认None),极坐标系:'polar’,三维直角坐标系:'3d’,地球投影:'aitoff', 'hammer', 'lambert', 'mollweide'等.

ax2 = plt.subplot(221, projection='rectilinear') ax2.set_title('rectilinear') ax2 = plt.subplot(222, projection='polar') ax2.set_title('polar') ax2 = plt.subplot(223, projection='3d') ax2.set_title('3d') ax2 = plt.subplot(224, projection='mollweide') ax2.set_title('mollweide') ax2.grid(True) plt.show()

在这里插入图片描述

图1.projection style

  facecolor :该关键字也可以用fc代替,颜色表示有多种方式。   1. RGB 或者RGBA元组,经检验RGB的np数组也可以,数字要限制在[0,1]之间,其中A表示透明度。如fc = (.18, .31, .31),fc = (0.1, 0.2, 0.5, 0.3),facecolorc=np.array([.18, .31, .31])   2. 十六进制RGB或者RGBA字符串,与第一种类似。如fc ='#0F0F0F' 、'facecolor=#0F0F0F0F'   3. 简单字符,一般是用颜色单词首字母来表示,能表示的颜色有限,有:'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w',其中C = Cyan(青色)、M = Magenta(洋红或品红、 Y = Yellow(黄色)、K = blacK(黑色、W=white (白色)。   4. tab:颜色单词,有: 'tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple', 'tab:brown', 'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan'.   注:purple,紫色;olive,黄褐色;   其他参数(部分):title(标题) xlim(坐标范围) xlabel (轴标识)xticklabels(刻度标识) xtick(刻度)

彩色图像色彩分布图 代码 import numpy as np import matplotlib.pyplot as plt import cv2 as cv img = cv.imread('lena.jpg') b, g, r = cv.split(img) img = cv.merge([r, g, b]) #为了适应fc中元组顺序为RGB,故调整 draw = np.reshape(img, (-1, 3)) lists = list(set([tuple(t) for t in draw])) #去除列表中重复行 print(np.array(lists)) draw = tuple(draw/255) ax = plt.subplot(1, 1, 1, projection='3d', fc=(.18, .31, .31)) # 创建一个三维绘图,并设置颜色 ax.scatter(r, g, b, s=1, color=draw) # 以RGB值为坐标绘制数据点,并以RGB值为点颜色 ax.set_zlim(0, 255) ax.set_ylim(0, 255) ax.set_xlim(0, 255) ax.set_zlabel('B', c='b') # 坐标轴 ax.set_ylabel('G', c='g') ax.set_xlabel('R', c='r') ax.set_title('clolored image distribution', color='w') ax.tick_params(labelcolor='w') plt.show() 测试结果

  纯红色测试图片 在这里插入图片描述

图2.纯红色测试图片

  去掉重复行之后的RGB值:[[254 0 0]]   绘制颜色分布结果 在这里插入图片描述

图3.纯红色测试图片颜色分布

  纯红色纯绿色相间测试图片 在这里插入图片描述

图4.纯红色纯绿色相间测试图片

去掉重复行之后的RGB值:可见混合之后有些地方的颜色就不那么纯净了 [[ 1 254 3] [253 0 0] [ 0 255 1] [ 4 255 0] [255 1 3] [253 2 0] [ 1 255 2] [254 0 0] [ 1 252 0] [255 1 1] [ 1 255 1] [255 1 0] [254 0 2] [ 1 255 0] [252 0 0 [ 0 254 0] [253 1 0] [ 0 255 4] [254 1 0] [ 0 254 2] [254 2 1] [254 2 0] [ 0 255 2] [ 0 255 3] [253 0 2] [ 0 255 0]] 在这里插入图片描述

图5.纯红色纯绿色相间测试图片色彩分布

  计算机视觉经典图像lena

在这里插入图片描述

图6.lena

  计算机视觉经典图像lena色彩分布

在这里插入图片描述

图6.lena色彩分布 参考

   https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplot.html#matplotlib.pyplot.subplot



【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3