
As many people remember, I work in a laboratory where we work with living and not so much organisms. Science is moving, in short. Usually forward. Sometimes we take dead monkeys as samples, the tissues of which then go to experimental tasks. It usually looks extremely cheerful. The bell rang at 11 o'clock in the evening, and you are told that the monkey was killed in the nursery. Almost undamaged, the neighbors only ate the heart. We sigh, climb into the flight schedule and go to the airport. On the spot, they give you the necessary spare parts of the slain and put them in a transparent container with a preservation solution. At the airport with this can no longer be dragged, as the transportation of liquids is limited. We go to the railway station by express to Krasnodar. Pretty girls on the control usually get a delightful lime shade when they see organs slowly circling in a soft pink solution.
In general, they brought, cut everything that was needed into slices, dyed it ... But it turns out that the received sources need to be processed and calculated automatically ... I just want to clarify that I am a research doctor, not a professional programmer or a mathematician. Therefore, if something seems erroneous, I will be glad to be edited.
Formulate the task

So, there is a series of sections stained with two dyes. Bluer is Mayer's hematoxylin, brown is DAB chromagen. In this illustration, the monkey esophagus, and brown painted markers for pankeratin. In principle, it does not matter. The main thing is that we have two dyes that can simultaneously paint the same structures. It is necessary to paint both at the same time, otherwise the structure of the slice is unclear. The trouble is that often in order to count objects or determine the area of structures with a positive reaction to an antibody, we need to have an image where only one of the pigments is present. That is, the task can be formulated roughly as "
split back the mixture of ketchup with mayonnaise ." And here the
color deconvolution algorithm comes into play.
')
Color Deconvolution
I will try not to get into the deep jungle of mathematics, since I myself hardly understand it. The color of the pixel in the image of the microdrug is determined by the absorption spectrum of light at this point. The absorption spectrum accordingly depends on which pigment absorbs light. However, we have an unpleasant situation when a conditional blue and brown dye are present at one point
at the same time . That is, their spectra are superimposed on each other.

I - intensity value for a given wavelength. c is the concentration of the dye, beta is the proportionality coefficient, which depends on the physical properties of the substance. That is, in fact, we need to evaluate the contribution that each of the dyes makes to the red, green and blue channels of the pixel. For our task, we used the AC Ruifrok algorithm in G. Landini modification, which wrote a plugin for the open source software ImageJ. ImageJ - software for batch image processing, written in Java. Extremely flexible, allows you to coat it with a variety of plug-ins and write scripts for it. We are lazy, let the machine grind tons of images for us. But first you need to give her samples for which you want to make the separation.
Samples are obtained using mono-staining sections. Thus we obtain the RGB color of the reference standard. Naturally, all manipulations are performed with the same exposure and white balance. Feed the plugin and get the values we need:
if (myStain.equals("Custom DAB")){
Theoretically, you can take ready-made pre-laboratory samples. In practice, it turns out that in each laboratory the dyes differ slightly in hue, which makes calibration on each lot extremely desirable. In order for this profile to appear in the list of options, you have to shove it into the source java-file. Instructions
on the developer page . The better defined the dye vectors, the closer the complementary image to the white - what remains after picking out our pigments. So, drum roll and the result of dark magic:



Batch processing
Now we need all this disgrace to work in a batch version. We do not want to poke manually in each file? ImageJ allows you to write custom scripts, which is convenient for such tasks. Actually, it creates a regular file in txt format, which describes the manipulations with your source code. I did something like this:
dir1 = getDirectory("Choose Source Directory "); dir2 = getDirectory("Choose Destination Directory "); list = getFileList(dir1); setBatchMode(true); for (i=0; i<list.length; i++) { showProgress(i+1, list.length); open(dir1+list[i]); imgName=getTitle(); run("Colour Deconvolution", "vectors=[Custom DAB]");
Now you can feed the resulting images of software, sharpened for recognition of objects in biological preparations. But this is already in the next article)
UPD. The author of the algorithm gives quite interesting applications - the identification of inscriptions filled with ink of a different color (Beinecke Rare Book and Manuscript Library, Yale University)



UPD2. These cute baboons love to kill each other fucking in hierarchical clashes.

