# How to give a scale bar in microscopy image?

I have a Differential interference contrast microscopy (DIC) image

img = Import["https://i.sstatic.net/hfEZx.png"]


I have made some image adjustment using, ImageTrim, ImageResize, and ImageAdjust

ImageTrim[
ImageResize[
ImageAdjust[img, {0.7, 0.75, 0.9}], {250, 266}], {{.08, .30}, {0.55,
0.75}}, DataRange -> {{0, 1}, {0, 1}}]


and get,

This clearly has altered the image scaling.

How can one overlay a scale bar with proper length?

• Welcome to Mathematica StackExchange! What do you mean by "give the scale bar"? Do you mean "get the scale bar"? Also, have you seen this question? Commented May 15, 2023 at 16:01
• Actually, as the size of the micrograph already modified, I need to mention the scale bar with the modified length which maintain the same aspect ratio with the particle size Commented May 15, 2023 at 16:11
• I provided an answer to the question, but I would appreciate if somebody could figure out how to extract the pixelsPerMeter information directly from the image using Mathematica. Commented May 16, 2023 at 8:54

img = Import["https://i.sstatic.net/hfEZx.png", "PNG"];


From the metadata (extracted from here) we can get the scaling of the picture.

pixelsPerMeter = {7710892, 7710892}


(I didn't mange to get the metadata from Mathematica)

We can check by overlaying a 10 x 1 micrometers bar on the original scale bar

Graphics[
{
img
, Red
, Rectangle @@ {#, # + N[pixelsPerMeter {10, 1} 10^-6]} &[{1797, 1369}]
}
]


Now you are scaling with ImageResize from {1920, 1440} to {250, 266}, distorting the image.

scaledImg =
ImageTrim[
ImageResize[
266}], {{.08, .30}, {0.55, 0.75}}
, DataRange -> {{0, 1}, {0, 1}}]


The new scale is

newscale = pixelsPerMeter*{250, 266}/ImageDimensions[img]


and we can place a scale "Square" of 10x10 micrometers

With[
{
lbcorner = {10, 10}, (* In pixels *)
boxsize = newscale *{10, 10} *Power[10, -6](* In pixels *)
},
Graphics[{
scaledImg
, Red
, Rectangle @@ Accumulate[{lbcorner, boxsize}]
}
]
]


Please notice the undesirable distortion on the "square" you have introduced by forcing a change in the aspect ratio.