11

Anyone know if OpenCV is capable of loading a multi-frame TIFF stack? I'm using OpenCV 2.2.0 with python 2.6.

6 Answers 6

14

OpenCV is now capable of reading a multi-page TIFF using the imreadmulti function. See this page from the OpenCV 3.4 documentation:

https://docs.opencv.org/3.4/d4/da8/group__imgcodecs.html#ga4dd47c9ae3d55cc42286cff005825e31

Sign up to request clarification or add additional context in comments.

1 Comment

This should be the accepted answer. I'd just add that it's important to set the right flags. If you want the separate channels/pages to be read as is, use for example flags=cv2.IMREAD_UNCHANGED.
10

While OpenCV can't open multi-frame TIFF files, you can open the image using PIL and then pass the data on to OpenCV. I haven't yet been able to get it working with the new "cv2" namespace

tiff = Image.open('sample.tif') try: while 1: # Convert PIL image to OpenCV image = cv.CreateImageHeader(tiff.size, cv.IPL_DEPTH_8U, 1) cv.SetData(image, tiff.tostring()) # It's "tostring" and not "toString()"! # Do whatever you're going to do with OpenCV data tiff.seek(tiff.tell()+1) except EOFError: pass 

5 Comments

Thanks for this. In case anyone else cares, I got it working in cv2. Further, I made a class that emulates the behavior of a cv2 capture object, you can treat multi-frame TIFF files just like a movie file. See the class PseudoCapture: github.com/danielballan/mr/blob/master/mr/video/tiff.py
@Dan Allan I'm very interested but this link is broken now, could you provide the script please?
I split the image-reading stuff into a separate repository that I am calling pims. It lives on this organization page now: github.com/soft-matter/pims
P.S. If you get some use out of this, get in touch! I'd like to see more people using it, giving feedback, contributing changes. Cheers!
@Corey Cole, apparently there is no support for the function CreateImageHeader in OpenCV 3.3, is there any alternative to that?
7

Unfortunately OpenCV does not support TIFF directories and is able to read only the first frame from multi-frame TIFF files.

1 Comment

OpenCV has added this functionality, as it is present in version 3.4. See: docs.opencv.org/3.4/d4/da8/…
5

You can load multi-frame tiff files in OpenCV using file read function called imreadmulti. Here is the example

 ret, images = cv2.imreadmulti('<path_of_tiff_files>.tiff', [], cv2.IMREAD_ANYCOLOR) 

Images will be a list of frames in the tiff file. Suppose you want to see 2nd image, you can access as

img = images[1] # note 0 based indexing 

Comments

0

You can also load and access tiff stacks in OpenCV after pip install TiffCapture. It implements all the VideoCapture methods you'd expect in OpenCV for tiffs using PIL.

Comments

0
#cv2.__version__ == '4.5.1' _, frame_data = cv2.imreadmulti('my.tiff', [], cv2.IMREAD_ANYDEPTH) #or _, frame_data = cv2.imreadmulti('my.tiff', [], -1) 

1 Comment

since you chose to add an answer that is very similar to an existing answer, you really should point out how your answer differs, how does it behave differently, and why that matters.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.