如何使用VIPS进行图像归一化? [英] How can I use VIPS for image normalization?

查看:212
本文介绍了如何使用VIPS进行图像归一化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要归一化一组图像的曝光和调色板.就上下文而言,这是为了在医学图像的图像分类中训练神经网络.我也在处理数十万张图片,因此效率非常重要.

I want to normalize the exposure and color palettes of a set of images. For context, this is for training a neural net in image classification on medical images. I'm also doing this for hundreds of thousands of images, so efficiency is very important.

到目前为止,我一直在使用VIPS,特别是PyVIPS,并且更喜欢使用该库的解决方案.找到此答案并浏览

So far I've been using VIPS, specifically PyVIPS, and would prefer a solution using that library. After finding this answer and looking through the documentation, I tried

x = pyvips.Image.new_from_file('test.ndpi')
x = x.hist_norm()
x.write_to_file('test_normalized.tiff')

但这似乎总是产生纯白色的图像.

but that seems to always produce a pure-white image.

推荐答案

您需要hist_equal进行直方图均衡.

You need hist_equal for histogram equalisation.

主要文档在这里:

https://libvips.github.io/libvips/API /current/libvips-histogram.html

但是,对于大型幻灯片图像,这将非常慢.它将需要扫描整个幻灯片一次以构建直方图,然后再次扫描以使其均衡.找到低分辨率图层的直方图,然后用它来均衡高分辨率图层的速度会快得多.

However, that will be extremely slow for large slide images. It will need to scan the whole slide once to build the histogram, then scan again to equalise it. It would be much faster to find the histogram of a low-res layer, then use that to equalise the high-res one.

例如:

#!/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])

另一个因素是直方图均衡是非线性的,因此会扭曲亮度关系.它还会扭曲颜色关系,并使噪点和压缩伪像看起来很疯狂.我在这里的图片上尝试过该程序:

Another factor is that histogram equalisation is non-linear, so it will distort lightness relationships. It can also distort colour relationships and make noise and compression artifacts look crazy. I tried that program on an image I have here:

$ ~/try/equal.py bild.ndpi[level=7] y.jpg

条纹来自幻灯片扫描仪,压缩带来的丑陋条纹.

The stripes are from the slide scanner and the ugly fringes from compression.

我想我会从低分辨率级别中找到最大和最小图像,然后使用它们对像素值进行简单的线性拉伸.

I think I would instead find image max and min from the low-res level, then use them to do a simple linear stretch of pixel values.

类似的东西:

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功能?它可以更快地生成用于训练MUCH的补丁,在某些情况下可以快100倍:

Did you find the new Region feature in pyvips? It makes generating patches for training MUCH faster, up to 100x faster in some cases:

https://github.com/libvips/pyvips/issues/100 #issuecomment-493960943

这篇关于如何使用VIPS进行图像归一化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆