You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
4.4 KiB

  1. # -*- coding: utf-8 -*-
  2. import array
  3. import numpy as np
  4. import struct
  5. import six
  6. from nd2reader.model.image import Image
  7. class V3ImageReader(object):
  8. def __init__(self, metadata):
  9. self._metadata = metadata
  10. def _calculate_field_of_view(self, frame_number):
  11. images_per_cycle = len(self._metadata.z_levels) * len(self._metadata.channels)
  12. return int((frame_number - (frame_number % images_per_cycle)) / images_per_cycle) % len(self._metadata.fields_of_view)
  13. def _calculate_channel(self, frame_number):
  14. return self._metadata.channels[frame_number % len(self._metadata.channels)]
  15. def _calculate_z_level(self, frame_number):
  16. return self._metadata.z_levels[int(((frame_number - (frame_number % len(self._metadata.channels))) / len(self._metadata.channels)) % len(self._metadata.z_levels))]
  17. def _calculate_image_group_number(self, time_index, fov, z_level):
  18. """
  19. Images are grouped together if they share the same time index, field of view, and z-level.
  20. :type time_index: int
  21. :type fov: int
  22. :type z_level: int
  23. :rtype: int
  24. """
  25. return time_index * len(self._metadata.fields_of_view) * len(self._metadata.z_levels) + (fov * len(self._metadata.z_levels) + z_level)
  26. def _calculate_frame_number(self, image_group_number, fov, z_level):
  27. return (image_group_number - (fov * len(self._metadata.z_levels) + z_level)) / (len(self._metadata.fields_of_view) * len(self._metadata.z_levels))
  28. def get_image(self, index):
  29. channel_offset = index % len(self._metadata.channels)
  30. fov = self._calculate_field_of_view(index)
  31. channel = self._calculate_channel(index)
  32. z_level = self._calculate_z_level(index)
  33. image_group_number = int(index / len(self._metadata.channels))
  34. frame_number = self._calculate_frame_number(image_group_number, fov, z_level)
  35. timestamp, image = self._get_raw_image_data(image_group_number, channel_offset, self._metadata.height, self._metadata.width)
  36. image.add_params(timestamp, frame_number, fov, channel, z_level)
  37. @property
  38. def _channel_offset(self):
  39. """
  40. Image data is interleaved for each image set. That is, if there are four images in a set, the first image
  41. will consist of pixels 1, 5, 9, etc, the second will be pixels 2, 6, 10, and so forth.
  42. :rtype: dict
  43. """
  44. channel_offset = {}
  45. for n, channel in enumerate(self._metadata.channels):
  46. channel_offset[channel] = n
  47. return channel_offset
  48. def _get_raw_image_data(self, image_group_number, channel_offset, height, width):
  49. """
  50. Reads the raw bytes and the timestamp of an image.
  51. :param image_group_number: groups are made of images with the same time index, field of view and z-level.
  52. :type image_group_number: int
  53. :param channel_offset: the offset in the array where the bytes for this image are found.
  54. :type channel_offset: int
  55. :return: (int, array.array()) or None
  56. """
  57. chunk = self._label_map[six.b("ImageDataSeq|%d!" % image_group_number)]
  58. data = self._read_chunk(chunk)
  59. # All images in the same image group share the same timestamp! So if you have complicated image data,
  60. # your timestamps may not be entirely accurate. Practically speaking though, they'll only be off by a few
  61. # seconds unless you're doing something super weird.
  62. timestamp = struct.unpack("d", data[:8])[0]
  63. image_group_data = array.array("H", data)
  64. image_data_start = 4 + channel_offset
  65. # The images for the various channels are interleaved within the same array. For example, the second image
  66. # of a four image group will be composed of bytes 2, 6, 10, etc. If you understand why someone would design
  67. # a data structure that way, please send the author of this library a message.
  68. image_data = np.reshape(image_group_data[image_data_start::len(self._metadata.channels)], (height, width))
  69. # Skip images that are all zeros! This is important, since NIS Elements creates blank "gap" images if you
  70. # don't have the same number of images each cycle. We discovered this because we only took GFP images every
  71. # other cycle to reduce phototoxicity, but NIS Elements still allocated memory as if we were going to take
  72. # them every cycle.
  73. if np.any(image_data):
  74. return timestamp, Image(image_data)
  75. return None