简体   繁体   English

在python中减去16位tiff图像

[英]Subtract 16 bit tiff image in python

I have two 16 bit tiff image, of which one is a background and I have to remove it from all the image.我有两个 16 位 tiff 图像,其中一个是背景,我必须从所有图像中删除它。 I use the following code, however I get the error saying我使用以下代码,但是我收到错误消息

return image1._new(image1.im.chop_difference(image2.im))返回 image1._new(image1.im.chop_difference(image2.im))
ValueError: image has wrong mode ValueError:图像的模式错误

from PIL import Image, ImageChops
    im1 = Image.open("main.tif")
    im2 = Image.open("background.tif")
    
    diff = ImageChops.difference(im2, im1)
    diff.show()

when I check the mode using print(im1.mode) I get当我使用print(im1.mode)检查模式时,我得到

I,16我,16

I do not understand this error.我不明白这个错误。 Also, I don't know if Pillow is able to subtract 16 bit tiff images or not.另外,我不知道 Pillow 是否能够减去 16 位 tiff 图像。 I need help to resolve this error and get a subtracted image.我需要帮助来解决此错误并获得减影图像。

The two images are main: main image这两个图像是主要的:主图像

background image: background背景图片: 背景

I think I would do it like this:我想我会这样做:

#!/usr/bin/env python3

from PIL import Image
import numpy as np

# Open both images and make into Numpy arrays of signed 32-bit integers
main = np.array(Image.open('main.tif')).astype('int32')
back = np.array(Image.open('background.tif')).astype('int32')

# Calculate difference with saturation
diff = np.clip(main - back, 0, main.max())

# Revert to PIL Image and save
Image.fromarray(diff.astype(np.uint16)).save('result.tif')

If you stretch the contrast, you get:如果你拉伸对比度,你会得到:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM