{"id":1479,"date":"2024-06-01T19:58:33","date_gmt":"2024-06-01T23:58:33","guid":{"rendered":"http:\/\/wordpress.forensicpath.us\/?p=1479"},"modified":"2024-06-01T19:59:46","modified_gmt":"2024-06-01T23:59:46","slug":"taking-and-processing-photomicrographs-part-4-fixing-anisotropic-illumination","status":"publish","type":"post","link":"https:\/\/wordpress.forensicpath.us\/index.php\/2024\/06\/01\/taking-and-processing-photomicrographs-part-4-fixing-anisotropic-illumination\/","title":{"rendered":"Taking and processing photomicrographs &#8212; part 4: fixing anisotropic illumination."},"content":{"rendered":"<p>As I mentioned before, anisotropic illumination occurs when the bulb does not make the background uniformly white (or whatever color it makes it).\u00a0 Traditionally with light microscopes, it makes the center brighter than the edges.\u00a0 In addition, when dust gets into the light path, it can cause grey blobs.\u00a0 For the rest of these posts, I&#8217;ll call the image of the tissue you took the &#8220;target image.&#8221;\u00a0 After you&#8217;ve processed the image, the resulting image becomes the new &#8220;target\u00a0 image&#8221; of the next step.<\/p>\n<p>The order in which you do things *does* matter, at least sometimes.\u00a0 I&#8217;m going to give you the order in which I do things.<\/p>\n<p><em><strong>Subtract darkfield image:<\/strong><\/em><\/p>\n<p>The first thing I do is get rid of hot spots if I took a darkfield image.\u00a0 It&#8217;s simple. I just subtract the dark image from the target image.\u00a0 Image arithmetic (adding, subtracting, multiplying, dividing images) is a common task and is included in all sorts of image processing software.\u00a0 Good open source packages for this include <a href=\"https:\/\/imagej.nih.gov\/ij\/index.html\">ImageJ <\/a>and <a href=\"https:\/\/imagej.net\/software\/fiji\/\">Fiji<\/a>, <a href=\"https:\/\/gnu-octave.github.io\/packages\/image\/\">Gnu Octave<\/a> with the &#8220;image&#8221; package, scilab with the <a href=\"https:\/\/atoms.scilab.org\/toolboxes\/IPCV\/1.1\">&#8220;image processing and computer vision toolbox&#8221;<\/a>, and numerous libraries if you want to code it yourself.\u00a0 These are all linux packages, but they all come in flavors for Windows and Macs I think.<\/p>\n<p>I find ImageJ\/Fiji very convenient.\u00a0 Open the two images, choose &#8220;Image Arithmetic&#8221; under the &#8220;Process&#8221; menus, choose &#8220;Subtract&#8221; and you&#8217;re done. ImageJ also allows you to put commands in macros and scripts so you can process a large number of images automatically.<\/p>\n<p>But, mostly, I don&#8217;t bother with darkfield images.\u00a0 If you do, you need to do this before you do the more important thing &#8212; correcting for the anisotropic illumination in the brightfield image.<\/p>\n<p><em><strong>Divide by the brightfield image:<\/strong><\/em><\/p>\n<p>The easy solution is to *divide* your regular image by the brightfield image you took.\u00a0 Again, it&#8217;s a simple image arithmetic function.\u00a0 Instead of choosing &#8220;subtract&#8221; in ImageJ, choose &#8220;divide.&#8221;\u00a0 Easy peasy.<\/p>\n<p>Sort of.\u00a0 Here is where it matters what image processing package you use.\u00a0 The reason is that there&#8217;s some assumptions made on how to regenerate the image once you divide it.\u00a0 Think of it this way.\u00a0 Remember that your pixel is defined by (usually) three values &#8212; red green and blue.\u00a0 There are other conventions, e.g. LUV, HSV, Lab, etc. but we can ignore them for now.\u00a0 For now, it&#8217;s RGB.\u00a0 Most of the time, these red, green, and blue values are encoded by integers (whole numbers) with a value between 0 (dark) and 255 (bright).\u00a0 Thus, white is r255,g255,b255 where r is red, g is green, and b is blue.\u00a0 Here&#8217;s a screenshot of a color picker in GIMP showing a bright red with r=174.3, g=30.4, b=30.4.<\/p>\n<p><a href=\"https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/Screenshot_20240601_163938.png\"><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone size-full wp-image-1843\" src=\"https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/Screenshot_20240601_163938.png\" alt=\"\" width=\"452\" height=\"491\" srcset=\"https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/Screenshot_20240601_163938.png 452w, https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/Screenshot_20240601_163938-276x300.png 276w\" sizes=\"(max-width: 452px) 100vw, 452px\" \/><\/a><\/p>\n<p>Dealing with integers like this is fine if you are adding or subtracting, but what about dividing?\u00a0 Let&#8217;s say my original pixel is 100,120,110, and my brightfield pixel is 100,100,100?\u00a0 That means that the results of division will be 1, 1.2, 1.1. which is very dark.\u00a0 Worse, since you can&#8217;t represent real numbers (e.g. 1.1) as an integer, if you did all this in integer arithmetic, the result would be 1,1,1.<\/p>\n<p>So, in order to do the division, you really have to:<\/p>\n<ol>\n<li>Get the original two images.<\/li>\n<li>Convert the pixel values from integers to real (floating point) numbers.<\/li>\n<li>Do the division.<\/li>\n<li>Scale everything back to between 0 and 255<\/li>\n<li>Convert back to integers.<\/li>\n<\/ol>\n<p>There are more ways to do this than you&#8217;d think and more decisions to make than is comfortable.\u00a0 For instance, do you process the red, green, and blue channels completely separately, and then combine them, or do you combine them early and treat them all the same.\u00a0 For instance, let&#8217;s say that the red channel goes from 10 to 200 down to 1 to 6, and the blue channel goes from 20 to 210 to 1 to 5.\u00a0 Now, let&#8217;s take a pixel that has a resulting value of r=1.5, g=2.1 after division.\u00a0 If I process the channels independently, that r of 1.5 becomes 30.\u00a0 If I combine the two channels, that 1.5 becomes 37.5.\u00a0 Plus, what happens when you have a zero or very dark value in the brightfield image?\u00a0 You can&#8217;t divide by zero.\u00a0 So, your program has to decide how to handle that.<\/p>\n<p>You can get those dark spots on the brightfield image by having dust or specks in the light path.\u00a0 For some programs they can be a problem\u00a0 In that case, you will have to manually edit the brightfield image to remove them.\u00a0 The easiest way I&#8217;ve found to do this is to use the &#8220;clone&#8221; function in a graphics program and cover them up with a patch of a nearby clean space that has similar illumination.\u00a0 It&#8217;s a standard tool in commercial programs like Photoshop.\u00a0 Again, there are multiple free tools for linux.\u00a0 I use<a href=\"https:\/\/www.gimp.org\/\"> &#8220;GIMP&#8221;<\/a> the Gnu Image Manipulation Program.<\/p>\n<p>For instance, ImageJ (or it&#8217;s related Fiji) does image division by first breaking the image into separate colors, doing the division, and recombining.\u00a0 Here&#8217;s an image I took of a tumor:<\/p>\n<p><a href=\"https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/DSC_6416.cut_.scaled.webp\"><img decoding=\"async\" class=\"alignnone size-full wp-image-1844\" src=\"https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/DSC_6416.cut_.scaled.webp\" alt=\"\" width=\"1024\" height=\"694\" srcset=\"https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/DSC_6416.cut_.scaled.webp 1024w, https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/DSC_6416.cut_.scaled-300x203.webp 300w, https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/DSC_6416.cut_.scaled-768x521.webp 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/p>\n<p>Here&#8217;s the brightfield image:<\/p>\n<p><a href=\"https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/DSC_6410.cut_.scaled.webp\"><img decoding=\"async\" class=\"alignnone size-full wp-image-1845\" src=\"https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/DSC_6410.cut_.scaled.webp\" alt=\"\" width=\"1024\" height=\"694\" srcset=\"https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/DSC_6410.cut_.scaled.webp 1024w, https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/DSC_6410.cut_.scaled-300x203.webp 300w, https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/DSC_6410.cut_.scaled-768x521.webp 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/p>\n<p>Note all the dust in the light path. Yuck.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Here&#8217;s the result of the division:<\/p>\n<p><a href=\"https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/Composite.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1846\" src=\"https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/Composite.webp\" alt=\"\" width=\"1024\" height=\"694\" srcset=\"https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/Composite.webp 1024w, https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/Composite-300x203.webp 300w, https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/Composite-768x521.webp 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/p>\n<p>The problem is that all of the dynamic range is eaten up by the result of dividing by a very small number.\u00a0 Note the bright spots in the lower left and left margin (arrows):<\/p>\n<p><a href=\"https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/Composit_annotated.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-1847\" src=\"https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/Composit_annotated-1024x688.webp\" alt=\"\" width=\"688\" height=\"462\" srcset=\"https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/Composit_annotated-1024x688.webp 1024w, https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/Composit_annotated-300x202.webp 300w, https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/Composit_annotated-768x516.webp 768w, https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/Composit_annotated-1536x1032.webp 1536w, https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/Composit_annotated.webp 1575w\" sizes=\"(max-width: 688px) 100vw, 688px\" \/><\/a><\/p>\n<p>Those dots are nice and bright, but everything else is scaled down.\u00a0 If one is tied to ImageJ, you can deal with this in part by manually editing out the dark spots and then adjusting the white balance.<\/p>\n<p>Here&#8217;s the brigthfield image with the darkest spots (though not all of the spots) manually removed using a clone tool in GIMP:<\/p>\n<p><a href=\"https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/DSC_6410.cut_.a.scaled.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1851\" src=\"https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/DSC_6410.cut_.a.scaled.jpg\" alt=\"\" width=\"1024\" height=\"694\" srcset=\"https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/DSC_6410.cut_.a.scaled.jpg 1024w, https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/DSC_6410.cut_.a.scaled-300x203.jpg 300w, https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/DSC_6410.cut_.a.scaled-768x521.jpg 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/p>\n<p>Now here&#8217;s the result of the division:<\/p>\n<p><a href=\"https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/Composite_new.scaled.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1852\" src=\"https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/Composite_new.scaled.png\" alt=\"\" width=\"1024\" height=\"694\" srcset=\"https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/Composite_new.scaled.png 1024w, https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/Composite_new.scaled-300x203.png 300w, https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/Composite_new.scaled-768x521.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/p>\n<p>It&#8217;s much more flat, and doesn&#8217;t have the white spikes.\u00a0 The color balance is still off. I&#8217;ll discuss that later, but here it is fixed a little:\u00a0 Note that it&#8217;s still a bit blue.\u00a0 It turns out that most pathologists like it that way, probably because we all use blue filters:<\/p>\n<p><a href=\"https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/Composite_new.scaled.color_balanced.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1853\" src=\"https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/Composite_new.scaled.color_balanced.png\" alt=\"\" width=\"1024\" height=\"694\" srcset=\"https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/Composite_new.scaled.color_balanced.png 1024w, https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/Composite_new.scaled.color_balanced-300x203.png 300w, https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/Composite_new.scaled.color_balanced-768x521.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Or&#8230; you can use a different piece of software.<\/p>\n<p>There is a very nice llibrary of image processing tools called the &#8220;<a href=\"https:\/\/www.libvips.org\/\">vips<\/a>&#8221; library (https:\/\/www.libvips.org\/).\u00a0 Learning to use vips is a bit more of a challenge because it doesn&#8217;t have a gui.\u00a0 In linux, you use it through the shell.\u00a0 In Windows, I assume there is some terminal-based way to do it, though I don&#8217;t use Windows so I don&#8217;t know.<\/p>\n<p>Here&#8217;s a shell script for doing the division.\u00a0 Most of the code is involved in casting the image from its native formate (jpeg) into the format used by the library (.v) and back:<\/p>\n<p>!\/bin\/sh<\/p>\n<p>#bring in the names of the files, two input and one output<br \/>\nfirst=$1<br \/>\nsecond=$2<br \/>\nout=$3<\/p>\n<p>#change the input image into .v format<br \/>\nvips cast $first first.v float<br \/>\nvips cast $second second.v float<\/p>\n<p># do the division<br \/>\nvips divide first.v second.v out.v<\/p>\n<p>#make the output image a .tif file<br \/>\n#it turns out that vips does better moving from<br \/>\n#.v to a floating point tif file and *then* to<br \/>\n# integer jpg format<br \/>\nvips cast out.v out.tif float<\/p>\n<p>#then jpg<br \/>\nconvert out.tif $out.jpg<\/p>\n<p>#delete all the intermediate files<br \/>\nrm out.tif<\/p>\n<p>&nbsp;<\/p>\n<p>Here&#8217;s the output from this division, using the very same brightfield image:<\/p>\n<p><a href=\"https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/DSC_6416.divide_scaled.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1849\" src=\"https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/DSC_6416.divide_scaled.webp\" alt=\"\" width=\"1024\" height=\"694\" srcset=\"https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/DSC_6416.divide_scaled.webp 1024w, https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/DSC_6416.divide_scaled-300x203.webp 300w, https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/DSC_6416.divide_scaled-768x521.webp 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/p>\n<p>And here it is with a simple histogram stretch, using a &#8220;curves&#8221; tool in GIMP:<\/p>\n<p><a href=\"https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/DSC_6416.divide_scaled-1.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1850\" src=\"https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/DSC_6416.divide_scaled-1.webp\" alt=\"\" width=\"1024\" height=\"694\" srcset=\"https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/DSC_6416.divide_scaled-1.webp 1024w, https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/DSC_6416.divide_scaled-1-300x203.webp 300w, https:\/\/wordpress.forensicpath.us\/wp-content\/uploads\/2024\/06\/DSC_6416.divide_scaled-1-768x521.webp 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/p>\n<p>The white balance isn&#8217;t perfect, but everything has a much more uniform background, and sprucing it up a little is trivial.\u00a0 It&#8217;s a lot faster than using ImageJ.<\/p>\n<p>I&#8217;ve also gotten some good results with OpenCV image division, though I can&#8217;t run it at the moment because of some fedora configuration issue I don&#8217;t want to deal with right now.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As I mentioned before, anisotropic illumination occurs when the bulb does not make the background uniformly white (or whatever color&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[99,60,1],"tags":[56,27,25,26],"class_list":["post-1479","post","type-post","status-publish","format-standard","hentry","category-forensic-pathology","category-photomicrographs","category-uncategorized","tag-histology","tag-image-processing","tag-microscopy","tag-photomicrography"],"_links":{"self":[{"href":"https:\/\/wordpress.forensicpath.us\/index.php\/wp-json\/wp\/v2\/posts\/1479","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.forensicpath.us\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wordpress.forensicpath.us\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wordpress.forensicpath.us\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wordpress.forensicpath.us\/index.php\/wp-json\/wp\/v2\/comments?post=1479"}],"version-history":[{"count":2,"href":"https:\/\/wordpress.forensicpath.us\/index.php\/wp-json\/wp\/v2\/posts\/1479\/revisions"}],"predecessor-version":[{"id":1854,"href":"https:\/\/wordpress.forensicpath.us\/index.php\/wp-json\/wp\/v2\/posts\/1479\/revisions\/1854"}],"wp:attachment":[{"href":"https:\/\/wordpress.forensicpath.us\/index.php\/wp-json\/wp\/v2\/media?parent=1479"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wordpress.forensicpath.us\/index.php\/wp-json\/wp\/v2\/categories?post=1479"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wordpress.forensicpath.us\/index.php\/wp-json\/wp\/v2\/tags?post=1479"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}