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

时间:2018-05-14 20:58:24

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

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

我想将OpenCV-python cv2.adaptiveThreshold应用于整个图像堆栈。以下代码适用于2D图像(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()

谢谢!

1 个答案:

答案 0 :(得分:0)

使用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)