从rgb图像中,另一个只通过添加填充零的边框来制作:
光密度测量曲线显示两个图像上非零域的像素值相同
subplot(141)
imshow(rgb_chromosomes[0])
subplot(142)
plot(rgb_chromosomes[0][13,:,:])
subplot(143)
imshow(resized_rgb[0][10:40,11:40,:])
subplot(144)
plot(resized_rgb[0][25,17:38,:])
show()
调整大小的图像看起来是蓝色,而原始图像看起来是棕色,这很奇怪。 怎么办第二张图像看起来像第一张?
由于
答案 0 :(得分:2)
假设你有一个rgb图像列表,它是一个由3个ninty数组组成的堆栈,你需要添加边框,以便所有图像具有相同的形状。以下功能完成了这项工作:
def ResizeImages(ImList):
'''Find the largest width and height of images belonging to a list.
Return a list of images of same width/height
'''
maxwidth=0
maxheight=0
components = np.shape(ImList[0])[2]
imtype = ImList[0].dtype
for i in range(len(ImList)):
width=np.shape(ImList[i])[1]#width=column
height=np.shape(ImList[i])[0]#height=line
#print "width:height",width,":",height
if width>maxwidth:maxwidth=width
if height>maxheight:maxheight=height
#print "maxwidth:maxheight",maxwidth,":",maxheight
NewList=[]
for i in range(0,len(ImList)):
width=np.shape(ImList[i])[1]
height=np.shape(ImList[i])[0]
diffw=maxwidth-width
startw=round(diffw/2)
diffh=maxheight-height
starth=int(round(diffh/2))
startw=int(round(diffw/2))
newIm=np.zeros((maxheight,maxwidth,components), dtype=imtype)
newIm[starth:starth+height,startw:startw+width,:]=ImList[i][:,:,:]
NewList.append(newIm)
return NewList
每个要调整大小的图像都粘贴在首先填充零类型dtype = uint8的图像中(因为uint8数组是在函数中发送的):
newIm=np.zeros((maxheight,maxwidth,components), dtype=imtype)
newIm[starth:starth+height,startw:startw+width,:]=ImList[i][:,:,:]
错误发生在这里:
newIm=np.zeros((maxheight,maxwidth,components))
由于没有指定类型,默认情况下类型为float64,解释了为什么调整大小的图像的颜色看起来很奇怪。
答案 1 :(得分:1)
请参阅Imshow subplots with the same colorbar和Set Colorbar Range in matplotlib。
如果您在所有调用imshow中使用相同的vmin
/ vmax
和cmap
,则颜色
应该反映完全相同的强度,无论整体范围如何
图像。
编辑:评论后添加了示例。我可能误解了问题所在,但代码/图像说明了如何设置v限制以匹配颜色(顶行)或如果每个图像自动标准化会发生什么(底行)。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
offset = 5.5
x1 = np.random.rand(12,16) + offset
x2 = np.array(x1)
x2 = np.zeros((15, 20))
x2[2:2+x1.shape[0], 3:3+x1.shape[1]] = x1
vmin = x1.min()
vmax = x1.max()
cmap = cm.hot
plt.figure(1); plt.clf()
f, ax = plt.subplots(2,2, num=1)
im0 = ax[0,0].imshow(x1, interpolation='nearest', cmap=cmap, vmin=vmin, vmax=vmax)
f.colorbar(im0, ax=ax[0,0])
im1 = ax[0,1].imshow(x2, interpolation='nearest', cmap=cmap, vmin=vmin, vmax=vmax)
f.colorbar(im1, ax=ax[0,1])
ax[0,1].set_title('image with border, sharing v lims')
im2 = ax[1,0].imshow(x1, interpolation='nearest', cmap=cmap)
f.colorbar(im2, ax=ax[1,0])
im3 = ax[1,1].imshow(x2, interpolation='nearest', cmap=cmap)
f.colorbar(im3, ax=ax[1,1])
ax[1,1].set_title('image with border, own v lims')
f.show()
注意:仅当非零颜色范围不包括零时,问题才会出现。这与上面的offset
变量一起显示。