fabio icon indicating copy to clipboard operation
fabio copied to clipboard

Flipped masks

Open CJ-Wright opened this issue 8 years ago • 7 comments

It seems that fabio should flip the .msk data on the way out. Currently if you mask an image loaded with tifffile and you write the mask down to .msk when it is loaded in fit2d the mask is upside down.

CJ-Wright avatar Sep 15 '17 18:09 CJ-Wright

img-4518

CJ-Wright avatar Sep 15 '17 18:09 CJ-Wright

I think that np.fipud(mask) would work.

CJ-Wright avatar Sep 15 '17 18:09 CJ-Wright

Could you give an example or mention the specific use case? I just tried it naively on a tiff and I don't see the tiff. Maybe it's filetype specific?

Here's what I tried. I compared to the PIL and the outputs look the same:

import numpy as np
import fabio
from PIL import Image

# use your own tiff
filename = "test.tiff"
img_fabio = fabio.open(filename)
data_fabio = img_fabio.data

img_PIL = Image.open(filename)
data_PIL = np.array(img_PIL)

import matplotlib.pyplot as plt
plt.ion() #because Im using ipython
plt.figure(2);
plt.imshow(data_fabio)
plt.figure(3);
plt.imshow(data_PIL)

# testing out writing and reading back
img_fabio.write("test2.tiff")
img_fabio2 = fabio.open("test2.tiff")
data_fabio2 = img_fabio.data
img_PIL2 = Image.open("test2.tiff")
data_PIL2 = np.array(img_PIL)

plt.figure(4);plt.clf()
plt.imshow(data_fabio2)
plt.figure(5);plt.clf()
plt.imshow(data_PIL2)

In the plots, they look the same for me. I understand TIFF formats can vary. I can post the TIFF metadata here if your issue was also with a TIFF. How is the image flipped? Is it from the saving process?

jrmlhermitte avatar Sep 16 '17 03:09 jrmlhermitte

This is about the .msk file saving not the Tiff. The problem is that the mask when loaded by fit2d is upside down from that in Python.

CJ-Wright avatar Sep 16 '17 05:09 CJ-Wright

On Fri, 15 Sep 2017 22:17:10 -0700 "Christopher J. Wright" [email protected] wrote:

This is about the .msk file saving not the Tiff. The problem is that the mask when loaded by fit2d is upside down from that in Python.

Hi Christopher,

Could you give us some more information about the history of the tiff and the msk file ?

The tiff could have been acquired with a CCD camera + taper, but how was the msk generated ?

Most image format do not specify an origin in the lab space, hence most scientists consider natural to have the origin at the bottom-left of the image (this does not resolve all ambiguities, as the side of the detector is not defined).

Did you know Fit2d was flipping all tiff-images upside-down ? probably because tiff defines explicitly the origin at the top of the camera and Fit2D wants it at the bottom. FabIO does nothing like this. It only address (hopefully properly) the endianess of the format. The "msk" format support has been obtained by reverse engineering, so I have no warranty about the correctness of what is done. There is no room for metadata, but maybe Fit2d behaves differently with masks on tif compared to others ?

Could you try converting all those files to a common format (use fabio-convert) where the behaviour is less ambiguous than tiff ?

Cheers,

-- Jérôme Kieffer

kif avatar Sep 18 '17 06:09 kif

The tiff was created via a Perkin Elmer detector using EPICs. The tiff was loaded with tifffile. The mask was created in python using a statistical masking algorithm via numpy. The .msk file was generated using https://github.com/scikit-beam/scikit-beam/blob/master/skbeam/io/fit2d.py#L8

CJ-Wright avatar Sep 18 '17 12:09 CJ-Wright

On Mon, 18 Sep 2017 05:51:23 -0700 "Christopher J. Wright" [email protected] wrote:

The tiff was created via a Perkin Elmer detector using EPICs. The tiff was loaded with tifffile. The mask was created in python using a statistical masking algorithm via numpy. The .msk file was generated using https://github.com/scikit-beam/scikit-beam/blob/master/skbeam/io/fit2d.py#L8

Then don't search further ... it is Fit2D which has flipped the image.

Try another format and it won't be flipped.

-- Jérôme Kieffer tel +33 476 882 445

kif avatar Sep 18 '17 13:09 kif