日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

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

發(fā)布時間:2025/4/5 56 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python Clipping input data to the valid range for imshow with RGB data解决方法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

      • 遇到的問題
      • 全部代碼
      • 參考

遇到的問題

第一點,遇到的問題是

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

導(dǎo)致結(jié)果圖片顯示不出來

這其實是python的imshow函數(shù)的一個bug,imshow函數(shù)不會對彩色圖像進(jìn)行歸一化。

michalkahle commented on Oct 13, 2017
imshow doesn’t normalize the color range in RGB images
Bug summary
When single channel float image is passed to imshow it gets normalized to range [0,1] before display. This does not happen for RGB images.

這里的解決方法是:

axs[1].imshow(new_img.astype('uint8'))


來源:stackoverflow

全部代碼

import numpy as np from scipy import signal from skimage import data from skimage.io import imread from matplotlib import pyplot as plt import mathdef correl2d(img, window):"""二維灰度圖像的空間濾波函數(shù)"""# 使用濾波器實現(xiàn)圖像的空間相關(guān) # mode = 'same'表示輸出尺寸等于輸入尺寸 # boundary = 'fill' 表示濾波前,用常量值填充原始圖像的邊緣,默認(rèn)常量值為0s = signal.correlate2d(img, window, mode='same', boundary='fill')return s.astype(np.uint8)def gauss(i, j, sigma):"""定義二維高斯函數(shù)"""return 1 / (2 * math.pi * sigma ** 2) * math.exp(-(i ** 2 + j ** 2) / (2 * sigma ** 2))def gauss_window(radius, sigma):"""定義radius * radius 的高斯平滑模板"""window = np.zeros((radius * 2 + 1, radius * 2 + 1))for i in range(-radius, radius + 1):for j in range(-radius, radius + 1):window[i + radius][j + radius] = gauss(i, j, sigma)return window / np.sum(window)# img為原始圖像 img = imread("4.1.04.tiff") new_img = np.zeros(img.shape) # 3*3 高斯平滑濾波模板 window = gauss_window(3, 1.0) for i in range(3):new_img[:,:,i]= correl2d(img[:, :, i],window) # 顯示圖像 plt.rcParams['font.sans-serif'] = ['SimHei'] # 中文 fig, axs = plt.subplots(1, 2) axs[0].imshow(img,) axs[0].set_title("原圖") axs[1].imshow(new_img.astype('uint8')) axs[1].set_title("3*3 高斯平滑濾波模板")plt.tight_layout() plt.show()

參考

[1]https://stackoverflow.com/questions/49643907/clipping-input-data-to-the-valid-range-for-imshow-with-rgb-data-0-1-for-floa
[2]https://github.com/matplotlib/matplotlib/issues/9391/

總結(jié)

以上是生活随笔為你收集整理的python Clipping input data to the valid range for imshow with RGB data解决方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。