- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试将图像分割成 RGB,但在绘制这些图像时遇到了问题。我使用此功能从某个文件夹获取所有图像:
def get_images(path, image_type):
image_list = []
for filename in glob.glob(path + '/*'+ image_type):
im=misc.imread(filename, mode='RGB')
image_list.append(im)
return image_list
此函数创建 4d 数组 (30, 1536, 2048, 3),我非常确定第一个值代表图像数量,第二和第三个是维度,第三个是 RGB 值。
在获取所有图像后,我将它们存储为一个 numpy 数组
image_list = get_images('C:\HDR\images', '.jpg')
temp = np.array(image_list)
之后我尝试使用简单的切片来从这些图像中获取特定的颜色:
red_images = temp[:,:,:,0]
green_images = temp[:,:,:,1]
blue_images = temp[:,:,:,2]
当我打印出这些值时,一切似乎都很好。
print(temp[11,125,311,:])
print(red_images[11,125,311])
print(green_images[11,125,311])
print(blue_images[11,125,311])
我得到以下信息:
[105 97 76]
105
97
76
到目前为止,一切似乎都很好,但是当我尝试显示图像时出现了问题。我使用 matplotlib.pyplot.imshow
来显示它,我得到的图像如下:
这是合理的,因为我选择红色:
plt.imshow(temp[29,:,:,0])
但是当我将其更改为不同的颜色 channel 时,如下所示:
plt.imshow(temp[29,:,:,2])
我得到的图像是这样的:
我的问题很简单。这里发生了什么?
最佳答案
我认为 matplotlib 只是将每个 channel (即强度)视为“热图”。
像这样将颜色映射传递给 imshow 函数,告诉它您希望它如何为图像着色:
plt.imshow(image_slice, cmap=plt.cm.gray)
@mrGreenBrown 回应您的评论,我假设您使用的 misc.imread
函数来自 scipy,即 scipy.misc.imread
。该函数与 PIL
的函数没有什么不同。参见 scipy.misc.imread docs .感谢@dai 指出这一点。
任何图像的单个 channel 都只是强度。它没有颜色。对于以 RGB 颜色空间表示的图像,颜色是通过“混合”红色、绿色和蓝色的量(由相应 channel 的强度给出)获得的。 单一 channel 无法表达颜色。
默认情况下,Matplotlib 将强度显示为热图,因此显示为“颜色”。
当您将单个 channel 保存为 JPEG 格式的图像时,该函数仅将单个 channel 复制 3 次,以便 R、G 和 B channel 都包含相同的强度。这是典型的行为,除非您将其保存为 PGM 等可以处理单 channel 灰度图像的格式。当您尝试将具有相同 channel 的此图像可视化 3 次时,由于每个像素的红色、绿色和蓝色贡献相同,因此图像显示为灰色。
将 plt.cm.gray
传递给 cmap
参数只是告诉 imshow
不要对强度进行“颜色编码”。因此,较亮的像素(接近白色的像素)意味着在这些位置有“更多”的“颜色”。
如果您想要颜色,您必须复制 3 channel 图像并将其他 channel 的值设置为 0
。
例如,将红色 channel 显示为“红色”:
# Assuming I is numpy array with 3 channels in RGB order
I_red = image.copy() # Duplicate image
I_red[:, :, 1] = 0 # Zero out contribution from green
I_red[:, :, 2] = 0 # Zero out contribution from blue
来自 stackoverflow 的一个相关问题 here .
关于python - 如何使用 misc.imread 将图像切片为红色、绿色和蓝色 channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37431599/
我正在使用 Web 服务在 Android 应用程序和 SOAP Web 服务之间发送数据。此 Web 服务仅接受序列化对象,而执行此操作的唯一方法是使用: import sun.misc.BASE6
我正在将 JDK 版本从 8 更新到 11,以解决某些并发数据结构的问题。 error: type Contended is not a member of package sun.misc [ERR
scipy.misc.logsumexp函数的输入参数有(a, axis=None, b=None, keepdims=False, return_sign=False),具体配置可参见这里,返回的
我刚刚安装了 scipy(通过 easy_install scipy),但由于某些原因 scipy.misc 丢失了。 看看这个: >>> import scipy >>> scipy >>> sc
在 kate(或 QtCreator)中,我有一个名为“Fixed[Misc]”的字体。我想检索字体文件,但没有找到:在我的字体目录中,我有一个“misc”目录,但我无法准确找到我在 kate 中使用
我在 python 脚本中导入 scipy.misc 时遇到问题。现在我知道其他人也提出了与此相关的问题,但他们的解决方案对我不起作用。 我正在编写的程序是这样开始的: import matplotl
我一直在使用 sun.misc 中的 BASE64Encoder 和 BASE64Decoder;我正在使用 Eclipse 并且不得不求助于警告,因为默认情况下访问权限仅限于它。 这些类工作得很好,
前言 unsafe类在jdk 源码的多个类中用到,这个类的提供了一些绕开jvm的更底层功能,基于它的实现可以提高效率。但是,它是一把双刃剑:正如它的名字所预示的那样,它是unsafe的,它所分配的
我正在使用具有以下导入的 scipy 1.3.1 运行旧代码: from scipy.misc import bytescale 出现以下错误: ImportError: cannot import
10 分钟前,我尝试连接 phppgadmin 5.1,但我在 Web 服务器错误日志中发现了此错误消息:“Misc 在/usr/share/phppgadmin/classes/Misc.php 第
我想调整表面法线“图像”(H * W * 3)的大小。问题在于数组中存在可取数字。如何使用scipy.misc.resize或cv2.resize调整大小? 最佳答案 cv2.resize支持负数。
jdk1.8.0_144中的src.zip包含Float.java 这又指的是 sun.misc.FloatingDecimal。我在 src.zip 中找不到它?谁能告诉我它在哪里?我可以找到 gr
我正在开发一个供其他团队使用的库,在库中有一些公开的类/方法,但我不希望其他人使用它们。 像java中的sun.misc包这样的东西,虽然所有的类都是公共(public)的,但编译器在使用它时会抛出“
10 分钟前,我尝试连接 phppgadmin 5.1,但我在 Web 服务器错误日志中发现了此错误消息:“Misc 在/usr/share/phppgadmin/classes/Misc.php 第
虽然也有类似的问题(例如 A 、 B 和 C ),但他们的答案并不能解决我的问题。 我使用的是针对 Android API 18 的 Android Studio 1.5.1(Android KitK
我将在我的代码中使用 sun.misc.BASE64Decoder 的 decodeBuffer(String inputString) 。多个线程将在同一个解码器对象上调用此函数。 这个线程安全吗?
我知道 sun.* 包不是官方 Java API 的一部分。但是,我需要使用一些类似于 Perf 提供的功能:特别是基本 JVM 指标(堆、线程、PermGen 等)、GC 的 JvmStat 计数器
我有一个选项卡设置为使用 API8 max 的 Activity 扩展。在此选项卡中,我正在初始化一些 TextView、EditText 和 SeekBar 对象,如下所示。 我正在寻找一种更通用的
sun.misc.Unsafe 或 theUnsafe 实例线程安全吗? 最佳答案 Unsafe 的方法不是线程安全的。您需要像往常一样同步访问您想要自己操作的数据。然而,访问实例 theUnsafe
我想知道我在这里做错了什么...... 我正在试验一个简单而人为的函数,它对某些 x 值求导: f(x) = x^3,然后计算导数 f'(x) = 3x^2 对于 x 在 1、2、3 处的值 >>>
我是一名优秀的程序员,十分优秀!