⬆️ ⬇️

Let's collect a moss-mite under a microscope or focus-stacking photos from the console





I hope this post will not cause nightmares in very sensitive harabchiteli. In this post I will try to talk about a simple way to increase the depth of field . This is a very urgent problem for those who work with a microscope and are engaged in macrophotography. The essence of the problem is that at large magnifications, blurring objects distant from the focal point becomes a big problem. This is in traditional portrait photography blur background allows you to emphasize the object. In scientific micrographs, this is most often a negative effect. I am glad that there is a focus-stacking technique that allows you to stitch a stack of photos with a different focus point into a single sharp picture. But stop talking about the abstract. Bring tick in the studio!



If it is not visible, it does not mean that he is not eating you right now.



It all started quite trivial. My beloved Fatshedera began to rapidly sink away and in every possible way try to die despite leaving. Since I work in a secret laboratory headcrab project , I have access to microscopes and other interesting pieces of iron. I shook the leaf above the slide and saw whole legions of the sweetest creatures that tried to crawl away from the bright light. I decided to take a photo for memory before the mass genocide, but I ran into the problem of depth of field. Not only did this animal move sluggishly even when it was flooded with alcohol, it was also too voluminous to get into a sharp zone at once. It looked like this:



We had to come up with something to get a sharp picture.



Z-stacking



I turned to the traditional method of stitching a series of photographs, but was unpleasantly surprised by the prices of commercial products. Especially if it is software from the manufacturer of the microscope. There is generally a quiet horror. But since we are not in vain for the Linux users and the Stallman icon to be regularly wiped from dust, we will look for free software. Immediately there were Hugin and Enfuse, which allow you to do everything beautifully and from the console, which is extremely convenient for mass processing of photos.

We have two main stages:



')

A GUI is available for Enfuse , but we will work from the console. Install the necessary packages for Ubuntu (they are available for Windows, the syntax will differ slightly):

sudo apt-get update && sudo apt-get install enfuse hugin hugin-tools -y 




At the first stage we put all our images in a separate directory. If necessary, fine-tune the white balance and exposure using the same RawTherapee. After this, we align it to level the camera shake or, in this case, a slight movement of the patient:



 align_image_stack -v -m --gpu -a aligned -C *.jpg 




Please note that we are trying to use GPU through OpenCL for calculations, which is much faster, but does not always work. If it falls, remove the option --gpu. Next, -a aligned creates new files with the appropriate prefix. * .jpg - mask for selecting files to be aligned.

As a result, the console gives us this conclusion:



Console output
 meklon@RegenLab-LinuxDesktop-1:~/ownCloud/Temp/2016.04  /converted$ align_image_stack -v -m -a aligned -C *.jpg Creating control points between Image_299.jpg and Image_300.jpg Trying to find 8 corners... Number of good matches: 0, bad matches: 40 Number of good matches: 1, bad matches: 39 Number of good matches: 2, bad matches: 38 Number of good matches: 4, bad matches: 36 Number of good matches: 5, bad matches: 35 Number of good matches: 1, bad matches: 39 Number of good matches: 2, bad matches: 38 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 6, bad matches: 34 Number of good matches: 7, bad matches: 33 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 1, bad matches: 39 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 3, bad matches: 37 Creating control points between Image_300.jpg and Image_301.jpg Trying to find 8 corners... Number of good matches: 0, bad matches: 40 Number of good matches: 0, bad matches: 40 Number of good matches: 3, bad matches: 37 Number of good matches: 8, bad matches: 32 Number of good matches: 7, bad matches: 33 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 0, bad matches: 40 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 7, bad matches: 33 Number of good matches: 1, bad matches: 39 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 7, bad matches: 33 Number of good matches: 8, bad matches: 32 Number of good matches: 6, bad matches: 34 Creating control points between Image_301.jpg and Image_302.jpg Trying to find 8 corners... Number of good matches: 0, bad matches: 40 Number of good matches: 6, bad matches: 34 Number of good matches: 8, bad matches: 32 Number of good matches: 4, bad matches: 36 Number of good matches: 7, bad matches: 33 Number of good matches: 1, bad matches: 39 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 3, bad matches: 37 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Creating control points between Image_302.jpg and Image_303.jpg Trying to find 8 corners... Number of good matches: 3, bad matches: 37 Number of good matches: 0, bad matches: 40 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 5, bad matches: 35 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 1, bad matches: 39 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 2, bad matches: 38 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Creating control points between Image_303.jpg and Image_304.jpg Trying to find 8 corners... Number of good matches: 3, bad matches: 37 Number of good matches: 0, bad matches: 40 Number of good matches: 3, bad matches: 37 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 1, bad matches: 39 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 3, bad matches: 37 Number of good matches: 1, bad matches: 39 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Creating control points between Image_304.jpg and Image_305.jpg Trying to find 8 corners... Number of good matches: 0, bad matches: 40 Number of good matches: 0, bad matches: 40 Number of good matches: 8, bad matches: 32 Number of good matches: 2, bad matches: 38 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 0, bad matches: 40 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 4, bad matches: 36 Number of good matches: 1, bad matches: 39 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 6, bad matches: 34 Creating control points between Image_305.jpg and Image_306.jpg Trying to find 8 corners... Number of good matches: 2, bad matches: 38 Number of good matches: 0, bad matches: 40 Number of good matches: 8, bad matches: 32 Number of good matches: 2, bad matches: 38 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 0, bad matches: 40 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 1, bad matches: 39 Number of good matches: 8, bad matches: 32 Number of good matches: 2, bad matches: 38 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Creating control points between Image_306.jpg and Image_307.jpg Trying to find 8 corners... Number of good matches: 0, bad matches: 40 Number of good matches: 0, bad matches: 40 Number of good matches: 0, bad matches: 40 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 0, bad matches: 40 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 6, bad matches: 34 Number of good matches: 6, bad matches: 34 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 1, bad matches: 39 Number of good matches: 8, bad matches: 32 Number of good matches: 1, bad matches: 39 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 8, bad matches: 32 Number of good matches: 6, bad matches: 34 Optimizing Variables Strategy 1 Average (rms) distance between Controlpoints after 0 iteration(s): 13.1811221241261 units Strategy 1 Average (rms) distance between Controlpoints after 1 iteration(s): 13.1566720340408 units Strategy 1 Average (rms) distance between Controlpoints after 2 iteration(s): 13.0662076945298 units Optimizing Variables Strategy 2 Average (rms) distance between Controlpoints after 0 iteration(s): 4.22711068896714 units Strategy 2 Average (rms) distance between Controlpoints after 1 iteration(s): 3.90465178857069 units Strategy 2 Average (rms) distance between Controlpoints after 2 iteration(s): 3.88979141960262 units Strategy 2 Average (rms) distance between Controlpoints after 3 iteration(s): 3.88741946042386 units Strategy 2 Average (rms) distance between Controlpoints after 4 iteration(s): 3.88714333274445 units Strategy 2 Average (rms) distance between Controlpoints after 5 iteration(s): 3.88711661173386 units Strategy 2 Average (rms) distance between Controlpoints after 6 iteration(s): 3.88711414454988 units Strategy 2 Average (rms) distance between Controlpoints after 7 iteration(s): 3.88711391437541 units Ctrl points before pruning: 1274, after: 1051 Optimizing Variables Strategy 1 Average (rms) distance between Controlpoints after 0 iteration(s): 1.11869477363138 units Strategy 1 Average (rms) distance between Controlpoints after 1 iteration(s): 1.10458154981741 units Strategy 1 Average (rms) distance between Controlpoints after 2 iteration(s): 1.10324670986012 units Strategy 1 Average (rms) distance between Controlpoints after 3 iteration(s): 1.09274711349145 units Strategy 1 Average (rms) distance between Controlpoints after 4 iteration(s): 1.09274711349145 units Optimizing Variables Strategy 2 Average (rms) distance between Controlpoints after 0 iteration(s): 1.08772087726722 units Strategy 2 Average (rms) distance between Controlpoints after 1 iteration(s): 1.07924785202492 units Strategy 2 Average (rms) distance between Controlpoints after 2 iteration(s): 1.07923206520615 units Strategy 2 Average (rms) distance between Controlpoints after 3 iteration(s): 1.07923200575396 units Run called Down to Algorithm Original Image: 2080x1544 Inner 256 2349312: 128 1952 - 128 1416 Starting 256: 128 1952 - 128 1416 Starting 128: 128 1952 - 128 1416 Starting 64: 0 1952 - 0 1416 Starting 32: 0 2016 - 0 1480 Starting 16: 0 2048 - 0 1512 Starting 8: 0 2064 - 0 1528 Starting 4: 0 2072 - 0 1536 Starting 2: 0 2076 - 0 1540 Starting 1: 0 2078 - 0 1542 Found Solution: 0 0 2079 1543 Crop 0x0 - 2079x1543 Crop Size 2079x1543 Set crop size to 0,0,2079,1543 Multiple images output loading Image_299.jpg remapping Image_299.jpg saving aligned0000.tif loading Image_300.jpg remapping Image_300.jpg saving aligned0001.tif loading Image_301.jpg remapping Image_301.jpg saving aligned0002.tif loading Image_302.jpg remapping Image_302.jpg saving aligned0003.tif loading Image_303.jpg remapping Image_303.jpg saving aligned0004.tif loading Image_304.jpg remapping Image_304.jpg saving aligned0005.tif loading Image_305.jpg remapping Image_305.jpg saving aligned0006.tif loading Image_306.jpg remapping Image_306.jpg saving aligned0007.tif loading Image_307.jpg remapping Image_307.jpg saving aligned0008.tif Written aligned images to files with prefix "aligned" 






If you are photographing a macro, the proportions of objects on different focusings are often distorted. In such cases, it may happen that extrication can take forever. Try without it, although the result will be worse.



image

Now we sew it all together with Enfuse. The basic principle is the selection of the most contrasting and sharp details in each picture for subsequent stitching. A small halo still remains.

Run:



 enfuse -o result.jpg -v --exposure-weight=0 --saturation-weight=0 --contrast-weight=1 --hard-mask *.tif 




-o result.jpg - the final file for output, * .tif - mask for input. tif is the result of the output of the previous utility, but if you are stitching sources with a different extension, replace them accordingly.



Stitching Examples



I will not give all the photos of the series, just a few for example.

Jam jar:









Special laboratory tissue under the microscope:





Source: https://habr.com/ru/post/283492/



All Articles