简体   繁体   English

如何标准化 tiff 图像

[英]How to normalize a tiff image

I am trying to set some specific pixels inside a image to black, those images are in tiff format, which requires me to decompose them in their respective frames, therefore my tiff image has 50 different frames.我试图将图像中的一些特定像素设置为黑色,这些图像是 tiff 格式,这需要我在它们各自的帧中分解它们,因此我的 tiff 图像有 50 个不同的帧。 For such task, I am using simple values by accessing the pixel index at their given position and simply setting their values to 0. For instance:对于这样的任务,我通过访问给定 position 处的像素索引并简单地将它们的值设置为 0 来使用简单值。例如:

img[10, 50] = 0

every time I try setting their pixels the image goes yellow instantly.每次我尝试设置他们的像素时,图像都会立即变黄。

在此处输入图像描述

However, if I remove every line that changes/sets the pixel values to black, the image goes back to normal.但是,如果我删除将像素值更改/设置为黑色的每一行,图像就会恢复正常。

Here's my code:这是我的代码:

from PIL import Image
%pylab inline
import matplotlib.pyplot as plt
import matplotlib.image as mpimg


image = "myimage.tif"
path = "C:/Dataset/Face1" + image

 plt.imshow(img)
img=mpimg.imread(path)
img[15, 60] = 0
img[15, 85] = 0
img[15, 105] = 0
img[35, 60] = 0
img[35, 85] = 0
img[35, 105] = 0
img[45, 60] = 0
img[43, 75] = 0
img[43, 92] = 0
img[43, 105] = 0
img[58, 55] = 0
img[65, 83] = 0
img[58, 110] = 0
img[75, 83] = 0
img[85, 75] = 0
img[85, 90] = 0
img[90 ,83] = 0
img[95, 60] = 0
img[99, 83] = 0
img[99, 103] = 0

I tried normalizing my image the easy way using opencv2:我尝试使用 opencv2 以简单的方式标准化我的图像:

img1 = cv2.imread('image.tif', cv2.IMREAD_GRAYSCALE)
final_img = cv2.normalize(img1,  img1, 0, 255, cv2.NORM_MINMAX)

Got this instead:得到了这个:

在此处输入图像描述

How i am decomposing the images我如何分解图像

from PIL import Image
import matplotlib.pyplot as plt
imagepath = "face1.tif"
path = "C:/Users/images/" + imagepath
img = Image.open(path)

for i in range(50):
    try:
        img.seek(i)
        img.save('C:/Users/images/face1/%s.tif'%(i,))
    except EOFError:
        break

What i want to do is normalize the image, when i print the values of one of the lightest pixel, the output is something around 8353. Also, convert it to 8bit image, so i can view it on matplotlib.我想要做的是标准化图像,当我打印最亮像素之一的值时,output 大约是 8353。此外,将其转换为 8 位图像,以便我可以在 matplotlib 上查看它。

You can do proper normalization using Scipy exposure.rescale_intensity() with Python/OpenCV.您可以使用带有 Python/OpenCV 的 Scipy exposure.rescale_intensity() 进行适当的标准化。

In the following, I use OpenCV to read the multi-page TIFF and process the frames in a loop as follows:在下文中,我使用 OpenCV 读取多页 TIFF 并循环处理帧,如下所示:

import cv2
import numpy as np
import skimage.exposure as exposure

# read images
imgs = cv2.imreadmulti("face_1.tif", flags = cv2.IMREAD_GRAYSCALE + cv2.IMREAD_ANYDEPTH)[1]

for i,img in enumerate(imgs):
    filename = f"face_1_frame-{i}.png"
    print(f"Processing frame {i} into file {filename}")
    # normalize image to 8-bit range
    img_norm = exposure.rescale_intensity(img, in_range='image', out_range=(0,255)).astype(np.uint8)
    cv2.imwrite(filename, img_norm)

    # display normalized image
    cv2.imshow('normalized',img_norm)
    cv2.waitKey(0)

Here is the first normalized frame: 这是第一个标准化帧:

在此处输入图像描述

Most probably your images use some non-standard encoding scheme.很可能您的图像使用了一些非标准的编码方案。 Normally, the pixel values (for a single channel) are bounded to [0..255].通常,像素值(对于单个通道)限制为 [0..255]。 In your case pixel values lie in the range [8162..8383].在您的情况下,像素值位于 [8162..8383] 范围内。 matplotlib normalizes that range for you automatically. matplotlib会自动为您标准化该范围。 But when you set one of the pixel values to 0, your range becomes [0..8383] and this is the reason why it struggles to display it.但是,当您将其中一个像素值设置为 0 时,您的范围将变为 [0..8383],这就是它难以显示它的原因。 Just normalize the data:只需标准化数据:

from matplotlib import pyplot as plt
img = plt.imread(r'C:\temp\face_1.tif')
img -= img.min() # you can use more sofisticated img = 255*(img - img.min())/(img.max() - img.min())
img[90 ,83] = 0
img[95, 60] = 0
img[99, 83] = 0
img[99, 103] = 0
plt.imshow(img, cmap='gray')
plt.show()

And this will get you:这会让你: 在此处输入图像描述

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

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