Clipping input data to the valid range for imshow with RGB data

今天在提取彩色图像RGB通道值合成单通道图像时,出现问题:

Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).

先给出原因:

matplotlib.pyplot.imshow()函数在处理灰度图像时,自动将其值做归一化处理

而在处理彩色图像时则不会,而是将浮点值变换至[0,1],整数值变换到[0, 255]范围

上代码:

def slic_image(image_path, block_number, compactness, sigma):image = cv2.imread(image_path)image_depth = image.shape[2]if image_depth == 3:image_r, image_g, image_b = cv2.split(image)image_r = image_r.astype('float32')image_g = image_g.astype('float32')image_b = image_b.astype('float32')syn_image_g = syn_single_channel_image(sig_channel_image=image_g, channel_name="g")plt.imshow(syn_image_g)plt.show()def syn_single_channel_image(sig_channel_image, channel_name,):image_height = sig_channel_image.shape[0]image_width = sig_channel_image.shape[1]b = np.empty(shape=(image_height, image_width), dtype="float32")g = np.empty(shape=(image_height, image_width), dtype="float32")r = np.empty(shape=(image_height, image_width), dtype="float32")b[:][:] = 0g[:][:] = 0r[:][:] = 0synthesis = [r, g, b]if channel_name == 'r':synthesis = [sig_channel_image, g, b]elif channel_name == 'g':synthesis = [r, sig_channel_image, r]elif channel_name == 'b':synthesis = [r, g, sig_channel_image]synthesis_image = cv2.merge(synthesis)return synthesis_imageslic_image(image_path='test.png', block_number=30, compactness=10, sigma=5)

简单描述大概就是:将一张图像的三个通道信息分别抽取出来,与另外两个大小相等的0数组来合成单色图像。

这里我将抽取出的每个通道的信息都转为float32,原目的的为了更好的保留图像的信息(后来发现使用cv2.imread( )函数读取的图像,其像素值类型本就为uint8,完全没有必要这么做)

最后合成的结果自然是三个通道的数据类型全部是float32,在调用plt.imshow( )函数的时候全被调整到了[0, 1]范围内,最后导致生成了几乎全是绿色的图像:

​ 原图与合成图像的显示对比

解决方法

​ 最终我将数组中的数据类型全部定义为uint8,这样就正常了。

结果:

今天在提取彩色图像RGB通道值合成单通道图像时,出现问题:

Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).

先给出原因:

matplotlib.pyplot.imshow()函数在处理灰度图像时,自动将其值做归一化处理

而在处理彩色图像时则不会,而是将浮点值变换至[0,1],整数值变换到[0, 255]范围

上代码:

def slic_image(image_path, block_number, compactness, sigma):image = cv2.imread(image_path)image_depth = image.shape[2]if image_depth == 3:image_r, image_g, image_b = cv2.split(image)image_r = image_r.astype('float32')image_g = image_g.astype('float32')image_b = image_b.astype('float32')syn_image_g = syn_single_channel_image(sig_channel_image=image_g, channel_name="g")plt.imshow(syn_image_g)plt.show()def syn_single_channel_image(sig_channel_image, channel_name,):image_height = sig_channel_image.shape[0]image_width = sig_channel_image.shape[1]b = np.empty(shape=(image_height, image_width), dtype="float32")g = np.empty(shape=(image_height, image_width), dtype="float32")r = np.empty(shape=(image_height, image_width), dtype="float32")b[:][:] = 0g[:][:] = 0r[:][:] = 0synthesis = [r, g, b]if channel_name == 'r':synthesis = [sig_channel_image, g, b]elif channel_name == 'g':synthesis = [r, sig_channel_image, r]elif channel_name == 'b':synthesis = [r, g, sig_channel_image]synthesis_image = cv2.merge(synthesis)return synthesis_imageslic_image(image_path='test.png', block_number=30, compactness=10, sigma=5)

简单描述大概就是:将一张图像的三个通道信息分别抽取出来,与另外两个大小相等的0数组来合成单色图像。

这里我将抽取出的每个通道的信息都转为float32,原目的的为了更好的保留图像的信息(后来发现使用cv2.imread( )函数读取的图像,其像素值类型本就为uint8,完全没有必要这么做)

最后合成的结果自然是三个通道的数据类型全部是float32,在调用plt.imshow( )函数的时候全被调整到了[0, 1]范围内,最后导致生成了几乎全是绿色的图像:

​ 原图与合成图像的显示对比

解决方法

​ 最终我将数组中的数据类型全部定义为uint8,这样就正常了。

结果:

解决:Clipping input data to the valid range for imshow with RGB data相关推荐

  1. python Clipping input data to the valid range for imshow with RGB data解决方法

    文章目录 遇到的问题 全部代码 参考 遇到的问题 第一点,遇到的问题是 Clipping input data to the valid range for imshow with RGB data ...

  2. Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255]解决方法

    Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] ❓报错原因 ...

  3. 错误解决:Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255]

    今天又是快乐改错误的经历: 在做k-means进行图片压缩的实战项目中,我遇到了这样一个pyplot显示图像报错问题:Clipping input data to the valid range fo ...

  4. 解决matplotlib无法imshow/imsave真彩色图像或提示Clipping input data to the valid range for imshow with RGB data (

    问题描述及解决方案 问题描述 解决matplotlib无法imshow/imsave真彩色图像或提示Clipping input data to the valid range for imshow ...

  5. Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for i

    keras  imshow显示图片显示不出来,报错 Clipping input data to the valid range for imshow with RGB data ([0..1] fo ...

  6. 【笔记】input data to the valid range for imshow with RGB data [0..1] for floats or [0.255] for integers

    Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for i ...

  7. Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255]

  8. Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] ...

    警告的位置在matplotlib.image中: 从源码可知如果图像(np.array)数值范围小于0或者大于1(或255),则会用np.clip将其截断

  9. 应用matplotlib的imshow函数显示彩色图像(RGB data)报错的解决方法

    何时出现错误提示 :"Clipping input data to the valid range for imshow with RGB data ([0..1] for floats o ...

最新文章

  1. CF724G Xor-matic Number of the Graph(线性基+组合数)
  2. 世界上第一个程序员竟然是女性,难以置信......
  3. 新一代蓝牙5标准开启 会成为物联网的最佳选择吗
  4. HTML5儿童玩具游戏商店网站模板
  5. Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---模板方法模式之CoffeineBeverageWithHook[转]...
  6. 换行符‘/n’和回车符‘/r’ 区别
  7. vue-json-excel前端导出excel教程
  8. Spring Cloud构建微服务架构—创建“服务注册中心”
  9. UVA10946 You want what filled?【DFS】
  10. 回调函数及其应用案例
  11. Android Studio设计用户登录界面
  12. 3D摇杆控制器一种简单实现!Cocos Creator 3D!
  13. [BZOJ2144]跳跳棋
  14. ubuntu安装photoshop
  15. 标准正态分布正反函数
  16. irc php,IRC / 实时聊天系统
  17. 带你掌握Visual Studio Code的格式化程序
  18. MMA安装及使用优化
  19. YOLOv5-7.0解决No module named ‘utils.datasets‘和cannot import name ‘scale_coords‘ from ‘utils.general‘
  20. 评价数据离散度方法(转)

热门文章

  1. 【Android】实现九宫格展示图片+视频(仿QQ空间、微信朋友圈)
  2. win10设备管理器闪退
  3. (dfs/dp)P2327 [SCOI2005]扫雷
  4. 甲减、甲状腺相关疾病最新研究、治疗进展(2021年下半年)
  5. Haxe FD 开发学习
  6. 周易Java_关于维度、计算机、周易的漫思
  7. RecyclerView图片错乱复用问题
  8. 【手把手】JavaWeb 入门级项目实战 -- 文章发布系统 (第六节)
  9. windows中的一些小技巧
  10. Qualcomm 处理器 Krait架构