python - 将 openCV-python 自适应阈值应用于 3D tiff

标签 python opencv opencv-python adaptive-threshold image-thresholding

我使用共聚焦显微镜生成的 3D 体积。这些图像的 x、y、z 尺寸约为 1024,1024,50,并存储在 .tif 文件中。

我想将 OpenCV-python cv2.adaptiveThreshold 应用于整个图像堆栈。以下代码适用于二维图像 (1024,1024,1)。如何将其展开为整个卷并保存输出的 .tif 文件?

img = cv2.imread("1024x1024x40.tif")
gimg = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
th = cv2.adaptiveThreshold(gimg, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 7, -20)
cv2.imshow('original',img)
cv2.imshow('Adaptive threshold',th)
cv2.waitKey(0)
cv2.destroyAllWindows()

谢谢!

最佳答案

使用bioformats包:

我没有测试数据但使用 this answer作为指南,您可以尝试以下操作:

import javabridge
import bioformats

javabridge.start_vm(class_path=bioformats.JARS)

path_to_data = '/path/to/data/file_name.tif'

xml_string = bioformats.get_omexml_metadata(path_to_data)
ome = bioformats.OMEXML(xml_string) # be sure everything is ascii
iome = ome.image(0) # e.g. first image

reader = bioformats.ImageReader(path_to_data)
raw_data = []
for z in range(iome.Pixels.get_SizeZ()):
    img = reader.read(z=z, series=0, rescale=False)
    gimg = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    th = cv2.adaptiveThreshold(gimg, 255,
            cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 7, -20) 
    img = cv2.bitwise_and(img, img, mask = th)
    raw_data.append(img)

bioformats.write_image("/path/to/data/file_name_OUTPUT.tif", raw_data)

关于python - 将 openCV-python 自适应阈值应用于 3D tiff,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50338907/

相关文章:

c++ - Python OpenCv 构建未在命令行中检测到 visual studio 2019

python - 如何使矩形透明?

opencv - 属性错误 : module 'cv2' has no attribute 'face' despite using "opencv_extras" recipe

python - rSimple python 安排两个定时事件

opencv - 使用C++和opencv的第一个项目,什么都找不到

c++ - OpenCV imwrite 浮点图像,使用哪种转换?

opencv - brew 链接 jpeg 问题

python - 检查 Tic Tac Toe 中的平局 [Python]

python - tornado web http请求阻塞其他请求,如何不阻塞其他请求

python - Numpy - 多个 3d 数组与 2d 数组