如何使用 VIPS 进行图像归一化?
How can I use VIPS for image normalization?
我想标准化一组图像的曝光度和调色板。对于上下文,这是为了在医学图像的图像分类中训练神经网络。我也在为几十万张图片做这个,所以效率很重要。
到目前为止,我一直在使用 VIPS,特别是 PyVIPS,并且更喜欢使用该库的解决方案。找到 and looking through the documentation 后,我尝试了
x = pyvips.Image.new_from_file('test.ndpi')
x = x.hist_norm()
x.write_to_file('test_normalized.tiff')
但这似乎总是产生纯白色图像。
您需要 hist_equal
进行直方图均衡。
主要文档在这里:
https://libvips.github.io/libvips/API/current/libvips-histogram.html
但是,对于大型幻灯片图像,这将非常慢。它将需要扫描整张幻灯片一次以构建直方图,然后再次扫描以均衡它。找到低分辨率层的直方图,然后用它来均衡高分辨率层会快得多。
例如:
#!/usr/bin/env python3
import sys
import pyvips
# open the slide image and get the number of layers ... we are not fetching
# pixels, so this is quick
x = pyvips.Image.new_from_file(sys.argv[1])
levels = int(x.get("openslide.level-count"))
# find the histogram of the highest level ... again, this should be quick
x = pyvips.Image.new_from_file(sys.argv[1],
level=levels - 1)
hist = x.hist_find()
# from that, compute the transform for histogram equalisation
equalise = hist.hist_cum().hist_norm()
# and use that on the full-res image
x = pyvips.Image.new_from_file(sys.argv[1])
x = x.maplut(equalise)
x.write_to_file(sys.argv[2])
另一个因素是直方图均衡化是非线性的,因此它会扭曲亮度关系。它还会扭曲颜色关系并使噪声和压缩伪影看起来很疯狂。我在此处的图像上尝试了该程序:
$ ~/try/equal.py bild.ndpi[level=7] y.jpg
条纹来自幻灯片扫描仪,丑陋的条纹来自压缩。
我想我会从低分辨率级别找到图像的最大值和最小值,然后使用它们对像素值进行简单的线性拉伸。
类似于:
x = pyvips.Image.new_from_file(sys.argv[1])
levels = int(x.get("openslide.level-count"))
x = pyvips.Image.new_from_file(sys.argv[1],
level=levels - 1)
mn = x.min()
mx = x.max()
x = pyvips.Image.new_from_file(sys.argv[1])
x = (x - mn) * (256 / (mx - mn))
x.write_to_file(sys.argv[2])
你在 pyvips 中找到新的 Region
功能了吗?它可以更快地生成用于训练的补丁,在某些情况下可以快 100 倍:
https://github.com/libvips/pyvips/issues/100#issuecomment-493960943
我想标准化一组图像的曝光度和调色板。对于上下文,这是为了在医学图像的图像分类中训练神经网络。我也在为几十万张图片做这个,所以效率很重要。
到目前为止,我一直在使用 VIPS,特别是 PyVIPS,并且更喜欢使用该库的解决方案。找到
x = pyvips.Image.new_from_file('test.ndpi')
x = x.hist_norm()
x.write_to_file('test_normalized.tiff')
但这似乎总是产生纯白色图像。
您需要 hist_equal
进行直方图均衡。
主要文档在这里:
https://libvips.github.io/libvips/API/current/libvips-histogram.html
但是,对于大型幻灯片图像,这将非常慢。它将需要扫描整张幻灯片一次以构建直方图,然后再次扫描以均衡它。找到低分辨率层的直方图,然后用它来均衡高分辨率层会快得多。
例如:
#!/usr/bin/env python3
import sys
import pyvips
# open the slide image and get the number of layers ... we are not fetching
# pixels, so this is quick
x = pyvips.Image.new_from_file(sys.argv[1])
levels = int(x.get("openslide.level-count"))
# find the histogram of the highest level ... again, this should be quick
x = pyvips.Image.new_from_file(sys.argv[1],
level=levels - 1)
hist = x.hist_find()
# from that, compute the transform for histogram equalisation
equalise = hist.hist_cum().hist_norm()
# and use that on the full-res image
x = pyvips.Image.new_from_file(sys.argv[1])
x = x.maplut(equalise)
x.write_to_file(sys.argv[2])
另一个因素是直方图均衡化是非线性的,因此它会扭曲亮度关系。它还会扭曲颜色关系并使噪声和压缩伪影看起来很疯狂。我在此处的图像上尝试了该程序:
$ ~/try/equal.py bild.ndpi[level=7] y.jpg
条纹来自幻灯片扫描仪,丑陋的条纹来自压缩。
我想我会从低分辨率级别找到图像的最大值和最小值,然后使用它们对像素值进行简单的线性拉伸。
类似于:
x = pyvips.Image.new_from_file(sys.argv[1])
levels = int(x.get("openslide.level-count"))
x = pyvips.Image.new_from_file(sys.argv[1],
level=levels - 1)
mn = x.min()
mx = x.max()
x = pyvips.Image.new_from_file(sys.argv[1])
x = (x - mn) * (256 / (mx - mn))
x.write_to_file(sys.argv[2])
你在 pyvips 中找到新的 Region
功能了吗?它可以更快地生成用于训练的补丁,在某些情况下可以快 100 倍:
https://github.com/libvips/pyvips/issues/100#issuecomment-493960943