# Fast z-stack recording

**URL:** <https://openflexure.discourse.group/t/fast-z-stack-recording/1763>\
**Category:** Request Help\
**Created:** [3 October 2024 08:38 UTC](https://openflexure.discourse.group/t/fast-z-stack-recording/1763 "2024-10-03T08:38:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![JojoDevel](https://avatars.discourse-cdn.com/v4/letter/j/a88e4f/32.png) [@JojoDevel](https://openflexure.discourse.group/u/JojoDevel)\
**Post date:** [3 October 2024 08:38 UTC](https://openflexure.discourse.group/t/fast-z-stack-recording/1763/1 "2024-10-03T08:38:50Z")

</div>

Is there a way to perform a fast z-stack (similar to autofocus, so no stops in between) but retrieve the images and relative z-steps?

---

<div class="post-metadata">

**Author:** ![j.stirling](https://yyz2.discourse-cdn.com/free1/user_avatar/openflexure.discourse.group/j.stirling/32/3_2.png) [@j.stirling](https://openflexure.discourse.group/u/j.stirling)\
**Post date:** [3 October 2024 10:39 UTC](https://openflexure.discourse.group/t/fast-z-stack-recording/1763/2 "2024-10-03T10:39:50Z")

</div>

@JohemianKnapsody will know better than me. But I believe that the fast autofocus is just using the MJPEG video stream, and an estimate of the step position based on time. So I don’t think you would get the best quality z-stack. I know Joe is working on a much faster stack and scan that doesn’t autofocus before z-stacks.

---

<div class="post-metadata">

**Author:** ![JojoDevel](https://avatars.discourse-cdn.com/v4/letter/j/a88e4f/32.png) [@JojoDevel](https://openflexure.discourse.group/u/JojoDevel)\
**Post date:** [3 October 2024 12:21 UTC](https://openflexure.discourse.group/t/fast-z-stack-recording/1763/3 "2024-10-03T12:21:25Z")

</div>

Hey @j.stirling, thanks for your answer. I am not that concerned about image quality but about recording data with different focus in a quick way. So I would be really curious to have a good way to jump into the code and write the mjpeg images and estimated z-step to the file system for later use 🙂

---

<div class="post-metadata">

**Author:** ![j.stirling](https://yyz2.discourse-cdn.com/free1/user_avatar/openflexure.discourse.group/j.stirling/32/3_2.png) [@j.stirling](https://openflexure.discourse.group/u/j.stirling)\
**Post date:** [3 October 2024 12:22 UTC](https://openflexure.discourse.group/t/fast-z-stack-recording/1763/4 "2024-10-03T12:22:33Z")

</div>

@JohemianKnapsody How easy is it to do this?

---

<div class="post-metadata">

**Author:** ![WilliamW](https://yyz2.discourse-cdn.com/free1/user_avatar/openflexure.discourse.group/williamw/32/106_2.png) [@WilliamW](https://openflexure.discourse.group/u/WilliamW)\
**Post date:** [3 October 2024 14:58 UTC](https://openflexure.discourse.group/t/fast-z-stack-recording/1763/5 "2024-10-03T14:58:08Z")

</div>

> [@JojoDevel](#):
>
> write the mjpeg images and estimated z-step to the file system

Writing to file is a lot of the slow part, particularly on a Pi.

The stream is available to remote clients, where saving could be a lot quicker, if the web stream does not drop frames. For the z-position in steps you would need to estimate from tagging the start time of the move. I believe everything runs at 1000 steps per second. However, do you want absolute step position, or something more like position relative to focus? The most focussed image your can determine from you recording. Then steps away from that position is estimated by the timestamps.

---

<div class="post-metadata">

**Author:** ![r.w.bowman](https://yyz2.discourse-cdn.com/free1/user_avatar/openflexure.discourse.group/r.w.bowman/32/15_2.png) [@r.w.bowman](https://openflexure.discourse.group/u/r.w.bowman)\
**Post date:** [4 October 2024 18:05 UTC](https://openflexure.discourse.group/t/fast-z-stack-recording/1763/6 "2024-10-04T18:05:22Z")

</div>

Yes, the autofocus estimates z based on timestamps, you should be able to see the code in the “autofocus” extension. I think I added code to the python client that captures from the image stream, but it’s not terribly well documented. That might be the easiest place to try.

V3 uses the newer camera library and does a better job of capturing to RAM, but doing what you describe nicely would need an extension to the server code. Running on the server is mostly helpful because it makes it easy to get consistent times between the stage and camera. If you don’t mind a few milliseconds of error (equating to a few steps, as William says it’s 1000 steps per second by default), making the moves from a python script and capturing from the stream is probably the quickest way to do this.

R

---

<div class="post-metadata">

**Author:** ![JojoDevel](https://avatars.discourse-cdn.com/v4/letter/j/a88e4f/32.png) [@JojoDevel](https://openflexure.discourse.group/u/JojoDevel)\
**Post date:** [7 October 2024 06:36 UTC](https://openflexure.discourse.group/t/fast-z-stack-recording/1763/7 "2024-10-07T06:36:50Z")

</div>

Is using V3 something that your recommend? It’s not yet the recommended release, is it?

So for on server changes, I could adapt the [focus\_rel](https://gitlab.com/openflexure/openflexure-microscope-server/-/blame/master/openflexure_microscope/api/default_extensions/autofocus.py#L75) function and then retrieve the `self.camera.stream.frames` to access the image data? How would you convert these frames into an RGB numpy array?

---

<div class="post-metadata">

**Author:** ![r.w.bowman](https://yyz2.discourse-cdn.com/free1/user_avatar/openflexure.discourse.group/r.w.bowman/32/15_2.png) [@r.w.bowman](https://openflexure.discourse.group/u/r.w.bowman)\
**Post date:** [7 October 2024 07:51 UTC](https://openflexure.discourse.group/t/fast-z-stack-recording/1763/8 "2024-10-07T07:51:14Z")

</div>

The data in the stream is only ever JPEG format (MJPEG is just a sequence of JPEG images, and it comes out of the GPU already compressed). So, you’d need to load each frame using Pillow or similar and convert it. You’re not likely to be able to do that in real time on the Pi, I’d recommend supporting the JPEGs and re inflating them afterwards.

V3 is still very much a development image, but it’s reaching the stage where done early users/testers/guinea pigs would be very useful!

---

<div class="post-metadata">

**Author:** ![JojoDevel](https://avatars.discourse-cdn.com/v4/letter/j/a88e4f/32.png) [@JojoDevel](https://openflexure.discourse.group/u/JojoDevel)\
**Post date:** [7 October 2024 08:47 UTC](https://openflexure.discourse.group/t/fast-z-stack-recording/1763/9 "2024-10-07T08:47:10Z")

</div>

Thanks, then I will try to get into the recording there and try to export the jpeg stream 🙂
