2
$\begingroup$

I have a Differential interference contrast microscopy (DIC) image

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

Microscopy Image

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,

Scaled and trimmed image

This clearly has altered the image scaling.

How can one overlay a scale bar with proper length?

$\endgroup$
3
  • 2
    $\begingroup$ 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? $\endgroup$
    – Domen
    Commented May 15, 2023 at 16:01
  • $\begingroup$ 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 $\endgroup$
    – R.Saha
    Commented May 15, 2023 at 16:11
  • $\begingroup$ 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. $\endgroup$
    – rhermans
    Commented May 16, 2023 at 8:54

1 Answer 1

2
$\begingroup$

Import your image

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}]
   }
 ]

enter image description here

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

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

The new scale is

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

enter image description here

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}]
   }
  ]
 ]

enter image description here

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.