Intro
I wanted to build a CNC microscope in order to attempt image fairly large objects such leaves, CD-ROMs etc. to reveal detail that can’t be seen with the naked eye.
This required a metallurgical style microscope, which shines light through a microscope objective, onto a sample.
Initial attempt
I initially attempted to fix the microscope in place using a rather large amount of plasticine. This did hold it in place for a little while, until it began to creep and fall through!
To fit the microscope through the tool holder I had to first unscrew the mirror section of the microscope and re-install after.
![](https://www.anfractuosity.com/wp-content/uploads/2024/06/plasticine-683x1024.jpg)
I switched from plasticine to neoprene tape, which held the microscope in place much more effectively!
To install the microscope I wrapped it with the neoprene tape and unscrewed the CNCs tool holder and gently prised it wide open to install the scope and re-install the screw.
The AmScope imaging sensor was then attached to the C-Mount thread on top of the microscope. It was necessary to rotate the image sensor, to do this I gently unscrewed the screw labelled in the following image and turned the sensor and re-tightened the screws.
![](https://www.anfractuosity.com/wp-content/uploads/2024/06/unscrew-769x1024.jpg)
An adapter provided by Lapsun was then attached to the RMS thread of the 20x objective and then the objective was screwed into the bottom of the microscope.
The LED light source provided by Lapsun was attached to the light port on the side of the microscope.
Parts and approx. costs
- Genmitsu 3018-PROVer V2 – CNC machine – £216
- Lapsun Microscope – Chose Color: 40X-400X Lens – £240
- Metallurgical Objective – Chose Color: Only 20X Lens – £108
- AmScope MU1803 18MP USB 3.0 – Used – £120
- Black neoprene tape – 0.5mm thick, 50mm height – £15
- Sorbothane discs – used for feet for the CNC machine – £20
Initial imaging
In the following image, you can see the microscope attached to the moveable head of the CNC machine using neoprene tape.
I placed a large cast plastic block onto the CNC stage, so that samples were closer to the microscope (the Z-axis couldn’t reach down far enough for the microscope to image without this).
![](https://www.anfractuosity.com/wp-content/uploads/2024/07/cncscope-683x1024.jpg)
The Z-axis was at -11.798mm to take the first picture from the microscope of a CD-ROM. I then moved the CNC 11cm to the left.
![](https://www.anfractuosity.com/wp-content/uploads/2024/07/a-1024x768.jpg)
To get from the image below, to an in-focus image, I had to adjust the Z-axis upwards to -11.488mm. A difference of 0.31mm.
![](https://www.anfractuosity.com/wp-content/uploads/2024/07/b-1024x768.jpg)
Magnification
The following image is of white paper with 20x magnification objective and using 4x magnification on the microscope dial.
![](https://www.anfractuosity.com/wp-content/uploads/2024/07/paper-1024x768.jpg)
The following image is of white paper with 20x magnification objective and using 1x magnification on the microscope dial – you can see significant vignetting due to poor illumination (I since found this assumption regarding illumination was incorrect, see below for more information).
![](https://www.anfractuosity.com/wp-content/uploads/2024/07/paper2-1024x768.jpg)
Turns out I was wrong with initial thoughts on illumination being the issue for the vignetting.
![](https://www.anfractuosity.com/wp-content/uploads/2024/07/1-1024x768.jpg)
After tightening the screws, which you can see in the image below, more properly. The image looks a lot better!
![](https://www.anfractuosity.com/wp-content/uploads/2024/07/mirror-1024x769.jpg)
![](https://www.anfractuosity.com/wp-content/uploads/2024/07/2-1024x768.jpg)
Sharpest regions from a single microscope image
onilink_ (see their website here – http://ic.onidev.fr/en/index.html) suggested to me the great idea of splitting a single microscope image into multiple ’tiles’ and measuring the sharpness of each tile, using the Laplacian of Gaussian – this works since a CD-ROM has a repeating pattern.
I used the Laplacian function I found from https://github.com/ArduCAM/RaspberryPi/blob/master/Motorized_Focus_Camera/python/Autofocus.py, iterated over tiles and generated a heat-map, which you can see below.
The original image has a shape of (3684, 4912) and the number of tiles is (13, 17).
You can see in the heat-map the far right of the image is the sharpest. I’m just hoping this means the CD-ROM is tilted rather than the microscope!
Using a higher quality 10x CD-ROM image from ZeptoBars (see their site https://zeptobars.com/) and the same code, with a tile size of 150. You can see the area of sharpness is in the centre of the image, which makes more sense.
I made a few adjustments – rotating the light source to try to improve light distribution as well as re-tightening the mirror section.
The following shows experiments with CD-ROM images with a 10x objective, with different magnifications on the microscope dial.
The following shows experiments with CD-ROM images with a 20x objective, with different magnifications on the microscope dial.
Software
I created a very simplistic script using Python which you can find at – https://github.com/anfractuosity/cncmicroscopy to automatically capture images from the AmScope camera from x0,y0, to x1,y1 with arbitrary steps.
You can see in the follow image, the pattern that the software generates for the movement of the CNC microscope whilst moving across a sample and capturing images.
![](https://www.anfractuosity.com/wp-content/uploads/2024/07/scan-1024x529.png)
I then made use of Hugin to stitch the resulting images. following the procedure from – https://hugin.sourceforge.io/tutorials/scans/en.shtml
The first image consists of microscope images of a book cover. You can see how the image becomes more out of focus nearer the bottom. I’m going to investigate focus stacking as one means of fixing this.
![](https://www.anfractuosity.com/wp-content/uploads/2024/07/scan-1024x848.jpg)
Mean Square Error (MSE)
I made use of mean square error to compare the last two frames in the queue for similarity.
last = list(self.q)[-2:]
mse = np.mean((np.array(last[0].buf) - np.array(last[1].buf))**2)
If the frames are similar enough then the python script takes a photo and then moves the microscope to the next tile.
I found currently the MSE seems fairly high, due to vibrations being transferred to the table with the microscope on. To lower these I plan to make use of a thick block of granite underneath the microscope as well as removing the CNC machine’s rubber feet and placing sorbothane discs underneath it instead.
In the following experiments an autofocus procedure is first run. The microscope X axis is then moved 1mm to the right and back 1mm to the left. Then MSE values of pairs of images are created over time and graphed. You can see the MSE value is very high at first at 90, due to the vibrations from the CNC machine itself.
The following graph shows MSE values graphed across time, when external hard disk drives where powered on, on the same desk as the CNC microscope, upstairs.
![](https://www.anfractuosity.com/wp-content/uploads/2024/08/mse_analysis-1024x768.png)
The following graph shows MSE values graphed across time, when external hard disk drives where powered off, on the same desk as the CNC microscope, upstairs.
![](https://www.anfractuosity.com/wp-content/uploads/2024/08/mse_analysis-1-1024x768.png)
The following shows the same experiment with the CNC machine on the carpet, upstairs.
![](https://www.anfractuosity.com/wp-content/uploads/2024/08/mse_analysis-3-1024x768.png)
The following shows the same experiment with the CNC machine on the carpet, downstairs.
![](https://www.anfractuosity.com/wp-content/uploads/2024/08/mse_analysis-2-1024x768.png)
The following shows the same experiment with the CNC machine on the carpet, downstairs and no rubber feet.
![](https://www.anfractuosity.com/wp-content/uploads/2024/08/mse_analysis-4-1024x768.png)
The following shows the same experiment with the CNC machine on the carpet, downstairs and no rubber feet, but adding sorbothane discs.
![](https://www.anfractuosity.com/wp-content/uploads/2024/08/mse_analysis-5-1024x768.png)
The following shows the same experiment with the CNC machine downstairs on stone fireplace with no feet.
![](https://www.anfractuosity.com/wp-content/uploads/2024/08/mse_analysis-6-1024x768.png)
The following shows the same experiment with the CNC machine downstairs on stone fireplace with no feet but sorbothane discs.
![](https://www.anfractuosity.com/wp-content/uploads/2024/08/mse_analysis-7-1024x768.png)
Exposure and framerate
Initially I wasn’t setting the exposure duration manually, but using automatic exposures. I found I was getting only around 3 fps from the camera.
This was significantly improved by dropping the resolution from 4912×3684 to 2456×1842 as well as using manual exposures instead by doing the following:
ctx.hcam.put_AutoExpoEnable(False)
ctx.hcam.put_ExpoTime(ctx.exposure)
ctx.hcam.put_ExpoAGain(100)
A high framerate is necessary so that MSE can be quickly calculated.
Autofocus
You can see in the following animation, the Z-axis of the CNC machine is adjusted, to adjust focus. It iterates from -10.000 to -12.000mm in steps of 0.01mm. The Laplacian function is used as a method of measuring sharpness.
We find the sharpest image is at -11.390mm. To go to this ‘best’ height, we can’t simply go from -12mm to -11.390mm because of backlash. If you do attempt to go from -12mm to -11.39mm, the image looks very blurry.
So we first go back to -10mm then to -11.39mm.
The video below was generated using the following command –
ffmpeg -framerate 10 -pattern_type glob -i '*.tif' -c:v libx264 out.mp4
To Do
- See if kinematic table means I can move 11cm across CD-ROM without having to adjust Z-axis
- Try out focus stacking
- Stitch more images using Hugin
Commercial solutions
The following is a list of some commercial solutions:
- https://www.labsmore.com/ – Lots of cool looking microscopes as well as graphical open source software – https://github.com/Labsmore/pyuscope
- https://meijitechno.com/product/mt8500-m-motorized-metallurgical-microscope/ – Rather pricey (around $44k)
DIY solutions
- https://openflexure.org/ – Cool looking DIY 3D printed motorised microscope
- https://github.com/TadPath/PUMA – Another awesome 3D printed microscope
Leave Comment
Error