python - 如何以完整的位深度保存多个子图

标签 python opencv matplotlib

我正在使用Python 2.7,OpenCV,matplotlib创建图像转换的“联系表”。我的输入文件是〜1920x1200 16位灰度PNG。我想保存完整的联系表(如果我正确理解的话,在代码中由“fig”对象表示)。

当我运行以下命令时,我得到一个〜200kb的输出PNG,对于图像查看器来说是空白的。我不确定我是否正确使用了savefig()(或在正确的对象上),还是问题是16位数据混淆了savefig

抱歉,骇客代码!

编辑:这里样本http://www.filedropper.com/sample_83

fig,axs = plt.subplots(5,3)
for clip in enumerate(range(1,6)):
    for exp in enumerate(range(2,5)):
        gridsize = (pow(2,exp[1]),pow(2,exp[1]))
        cliplim = clip[1]*3 

        clahe = cv.createCLAHE(clipLimit=cliplim,tileGridSize=gridsize)
        cl = cv.normalize(clahe.apply(img16),None,0,65535,cv.NORM_MINMAX)

        axs[clip[0],exp[0]].imshow(cv.bitwise_not(cl),cmap='Greys')

        axs[clip[0],exp[0]].set_title('clip=%s grid=%s'%(cliplim,gridsize[0]))
        axs[clip[0],exp[0]].get_xaxis().set_visible(False)
        axs[clip[0],exp[0]].get_yaxis().set_visible(False)
fig.savefig('out.png')
plt.show()

最佳答案

您的图像确实是16位的,但是对比度较低,这就是为什么它看起来“纯灰色”的原因。您可以像这样通过 ImageMagick identify运行它。我在右侧添加了指向突出部分的箭头。

magick identify -verbose sample.png               # omit "magick" if still using v6

示例输出
Image: sample.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: DirectClass
  Geometry: 1920x1080+0+0
  Units: Undefined
  Colorspace: Gray
  Type: Grayscale
  Base type: Undefined
  Endianess: Undefined
  Depth: 16-bit                               <--- 16-bit
  Channel depth:
    Gray: 16-bit
  Channel statistics:
    Pixels: 2073600
    Gray:
      min: 27916  (0.425971)                  <--- min 27,916 of 65,535
      max: 44722 (0.682414)                   <--- max 44,722 of 65,535
      mean: 30629.2 (0.467372)
      standard deviation: 297.459 (0.00453893)
      kurtosis: 219.144
      skewness: 6.43389
      entropy: 0.811496
  Rendering intent: Undefined
  Gamma: 0.454545
  Matte color: grey74
  Background color: white
  Border color: srgb(223,223,223)
  Transparent color: none
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 1920x1080+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: Zip
  Orientation: Undefined
  Properties:
    date:create: 2020-02-28T08:17:02+00:00
    date:modify: 2020-02-28T08:17:01+00:00
    png:IHDR.bit-depth-orig: 16
    png:IHDR.bit_depth: 16
    png:IHDR.color-type-orig: 0
    png:IHDR.color_type: 0 (Grayscale)
    png:IHDR.interlace_method: 0 (Not interlaced)
    png:IHDR.width,height: 1920, 1080
    signature: bfd36a4055d8bb31695bbed19738063efd9e842d4beec01c5d5123d32f1df42f
  Artifacts:
    verbose: true
  Tainted: False
  Filesize: 3.96262MiB
  Number pixels: 2073600
  Pixels per second: 75.0272MP
  User time: 0.020u
  Elapsed time: 0:01.027
  Version: ImageMagick 7.0.9-6 Q16 x86_64 2019-11-27 https://imagemagick.org

如您所见,亮度范围从65,535的27,916到44,722,因此您在16位图像中仅使用了可用范围的26%。

因此,如果您希望图像显示得更好,则可能需要先将其规格化为全范围图像。

关于python - 如何以完整的位深度保存多个子图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60440859/

相关文章:

python - 按内容的升序合并文本文件

python - 如何实时平滑阈值处理?

opencv - 处理中的OpenCV Canny Edge检测器(实时视频)

java - 使用命令行和外部库在 ide 之外运行我的 jar 时出错

python - 我们可以用一条数据线创建散点图吗

Python 将 2d 数据绘制到 3d 轴上

arrays - python - Matplotlib 3D 输入格式化 Z(相关)值

python - 图像的 RGB 矩阵

python - 我收到 "AttributeError: module ' Discord.ext.commands' has no attribute 'bot"错误

python - PyCharm 3.2 问题 : the packages are listed in External Libraries but unsolved in editor