[英]How to normalize a tiff image
我试图将图像中的一些特定像素设置为黑色,这些图像是 tiff 格式,这需要我在它们各自的帧中分解它们,因此我的 tiff 图像有 50 个不同的帧。 对于这样的任务,我通过访问给定 position 处的像素索引并简单地将它们的值设置为 0 来使用简单值。例如:
img[10, 50] = 0
每次我尝试设置他们的像素时,图像都会立即变黄。
但是,如果我删除将像素值更改/设置为黑色的每一行,图像就会恢复正常。
这是我的代码:
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
我尝试使用 opencv2 以简单的方式标准化我的图像:
img1 = cv2.imread('image.tif', cv2.IMREAD_GRAYSCALE)
final_img = cv2.normalize(img1, img1, 0, 255, cv2.NORM_MINMAX)
得到了这个:
我如何分解图像
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
我想要做的是标准化图像,当我打印最亮像素之一的值时,output 大约是 8353。此外,将其转换为 8 位图像,以便我可以在 matplotlib 上查看它。
您可以使用带有 Python/OpenCV 的 Scipy exposure.rescale_intensity() 进行适当的标准化。
在下文中,我使用 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)
很可能您的图像使用了一些非标准的编码方案。 通常,像素值(对于单个通道)限制为 [0..255]。 在您的情况下,像素值位于 [8162..8383] 范围内。 matplotlib
会自动为您标准化该范围。 但是,当您将其中一个像素值设置为 0 时,您的范围将变为 [0..8383],这就是它难以显示它的原因。 只需标准化数据:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.