Microscope scale bar
There are two common ways to generate a scale bar:


Measure it.  You can take a picture of an object with known spacing such as a grid target and then measure the size of each pixel.
Compute it.  If you know the magnification of your objective, and the magnification of any relay optics after the object but before your camera, you can compute the total optical magnification.  Then using the pixel size on your camera, you can get the actual pixel size.  For example, suppose you have a camera with 7.4 micron pixels, a 20x objective, and a 0.5x adapter lens before your camera, then you would have a pixel size in your image of 7.4/20/0.5 = 0.74 microns.