Counting algae cells with granulated background

cellprofiler

#1

Hello,

I have trouble counting small algae cells on CellProfiles on a background that is quite noisy and does not contrast so well with the cells. Image attached. In my pipeline I have used, imagemath/ invert, then smooth/smooth keeping edges based on cell diameter, then IdentifyPrimaryObjects with robust background and distinguish between clumped objects and lines based on intensity.

But at the end I get just a black image that nothing is identified.

Any advice, pleeease
thanks!
Sofie


#2

Hello Sofie,
I did not use cell profiler to segment this image, I used Fiji instead because it is faster. If the image suits you I will write up the whole procedure (not difficult just hard to explain) shortly and send it back

Good luck
Bob


1st Results.csv (28.3 KB)
1st Summary.csv (114 Bytes)


#3

yes please! I dont know how to thank you for this!!!


#4

Hello again!,
O.K. I am assuming (I shouldn’t do that) that the attached image is as you aquired it with no preprocessing yet. So, the following is what I did.
Since any analysis in the future will vary in characteristics this first part was done by hand. Obtain the Polynomial Shading Corrector and Polynomial Fit plug-ins (available from the ImageJ/Fuji plug-in site) and firstly shade correct the image to flatten out the background. You can even do this twice if there’s any doubt and it’s quick, Then create a Poly fit image. Now the following is for the image and is completly variable to any images you may have in the future.

  1. Use Process > Math > subtract(150) all pixels from here down are not providing any data so it’s useless. Then Math > Divide(16) the highest value of pixels remaining, and then Multiply >(100) this gives you an extreamly good contrast. Better then the Macro which would be v=((v-150)/16*100)

  2. This you can do and then undo if it does not improve the image, but subtract the Fit image from the Prime image.

Your done and can color threshold and analyze particles.

This I done just because it didn’t seem that you needed the Blue channel so I split the channels and then merged only the Red and Green channels.

Hope this helps now and in the future , any questions just ask.
Bob


#5

Bob, thank you so much for getting into all this trouble! You might have saved us hundreds of hours of processing…

I will give this a try the next few days. Hopefully it will work

I will make sure I acknowledge you in our paper!


#6

Hi Sofie1,
No need for that, but before you do anything with the image select the whole image then plot the profile and enable the LIVE button at the bottom so you can watch the changes as they happen. I get a kick out of it. Take care.
Bob


#8

Hi Bob,

Trying to follow your steps and just have a couple of questions.
What degree of polynomial should I use for the surface fit? For subtracting the images I need to be saving themnunder different names or is there a simpler way?
Finally, I wanted to ask whether you think this procedure could be automated using eg a macro, cause I have hundreds of pictures like this one

A huge thanks for your help!

Sofie


#9

Hi Sofie,
O.K. now for the interesting stuff. All numbers used in both plug-ins should be kept between 1 and 10. The Degree in the Shading plug_in determines how much “spread” there is in the shading range and should be equal in both the x and y axis. The Regularization determines how close you want the image to the maximum intensity of the image. I very seldom use a value higher than 5. The lower this value, the closer you get to maximum.
For the Fit plug-in, the higher the value the closer the fit is to the overall intensity of the image BUT there will be more variation to the fit.
The reason for all of this is that you will be manipulating how the PHOTONs of light interact with the image. and it all has to be balanced out which will become easier with practice.
As for the automation of the process Yes it can be. IF all of the images are very consistant as to size,brightness,focus and distance. You will understand as you work with it. And it is relatively simple with Fiji/Image macro recording accessed by Plugins > Macro > Record. You simply start it at the beginning of the process and it records every move you make so you can save it for later use.

Good Luck, have fun with it- you can’t break it and I’ll always be around if any further questions.
Bob


#10

Hi again Bob,

Ok I managed to follow all steps up until the color threshold. After I merge the red and yellow channels I get a new tiff but when I try to do color threshold on it, all options are disactivated. What am I doing wrong? I feel I’m very close…

Thank you!
Sofie


#11

When you merge the images you need to use three images to color threshold, so while the channels are separated highlight the Green and Edit > selection > select all then Edit > clear image and then merge with all three channels.

I’m proud for you guys. Keep up the spirits and drive on.

Bob

:ok_hand::grin:


#12

Sorry Sophie!
I misunderstood the question. You need to convert the tiff image to a rgb image in Image > type >RGB to get all three channels. I’m so very sorry–Just old age I guess
Bob


#14

i did it Bob! I managed to count them accuratelly with your method! you are a star


#15

Hi again Bob,

so I’ve been trying to convert the whole process into a macro for batch analysis (Processes>Batch>Macro). This is what I’ve got so far, but my problem is (a) that i don;t get past splitting the channels, and (b) the thresholding is quite challenging. If you have any advice on the coding it would be so helpful to get me unstuck. I do plan to share it with the world once it is finished!

run("Polynomial Shading Corrector", "degree_x=2 degree_y=2 regularization=2");
run("Macro...", "code=v=((v-150)/16*100)");

title=getTitle();
run("Split Channels");
selectWindow("title + " (blue)"");
run("Select All");
setBackgroundColor(0, 0, 0);
run("Clear", "slice");
red= title + " (red)";
green= title + " (green)";
blue= title + " (blue)";
run("Merge Channels...", "c1=["+blue+"] c2=["+red+"] c3=["+green+"] create");
run("RGB Color");
selectWindow(Composite (RGB))
min=newArray(3);
max=newArray(3);
filter=newArray(3);
a=getTitle();
run("HSB Stack");
run("Convert Stack to Images");
selectWindow("Hue");
rename("0");
selectWindow("Saturation");
rename("1");
selectWindow("Brightness");
rename("2");
min[0]=0;
max[0]=255;
filter[0]="pass";
min[1]=0;
max[1]=255;
filter[1]="pass";
min[2]=3;
max[2]=206;
filter[2]="pass";
for (i=0;i<3;i++){
  selectWindow(""+i);
  setThreshold(min[i], max[i]);
  run("Convert to Mask");
  if (filter[i]=="stop")  run("Invert");
}
imageCalculator("AND create", "0","1");
imageCalculator("AND create", "Result of 0","2");
for (i=0;i<3;i++){
  selectWindow(""+i);
  close();
}
selectWindow("Result of 0");
close();
selectWindow("Result of Result of 0");
rename(title);

close(title)

#16

Good day Sofie,

you may try the following ImageJ-macro that works reasonably well for your sample image:

// imagej-macro "cellCount" (Herbie G., 19. Dec. 2018)
requires( "1.52i" );
setOption("BlackBackground", true);
orig=getTitle();
setBatchMode(true);
run("Duplicate...", "title=temp");
run("Lab Stack");
run("Make Substack...", "channels=3");
close("temp");
run("Bandpass Filter...", "filter_large=40 filter_small=1 suppress=None tolerance=0");
run("Median...", "radius=4");
setAutoThreshold("Yen dark");
run("Convert to Mask");
run("Watershed");
run("Analyze Particles...", "size=20-Infinity exclude add");
print("Cells recognized: "+roiManager("count"));
close("temp-1");
run("From ROI Manager");
run("Overlay Options...", "stroke=blue width=1 fill=none apply");
setBatchMode(false);
exit();
// imagej-macro "cellCount" (Herbie G., 19. Dec. 2018)

Of course some of the cells are left out and some false counts occur but the achieved count of 548 cells isn’t too bad.
(The macro isn’t suited to determine cell sizes.)

Please report about your experience.

Regards

Herbie


#17

Works like magic! For some reason i can’t get it to work in batch mode though. Neither with the loop as a macro script nor in the batch>macro window. So before I spend another couple of weeks trying to figure this out, I thought I might ask if there’s an easy fix? :slight_smile:

Thank you!

Sofie


#18

Sofie,

please tell me what exactly you want to do.

Do you have your image files (what image format do they have? TIF, JPG etc.) in one folder and if not how is the structure of your data and should the images be saved with the overlay after the processing in TIF-format.

Please explain in detail, otherwise things become unnecessarily lengthy.

Regards

Herbie


#19

So I have 15 jpeg images stored within a folder. What I would like is to process all 15 of them all in one go and get a table out which would give the cell count for each image name (so a table with 15 rows and two columns).
I have dosens of these folders that is why I want to automate this procedure, to be able to do each folder in one go.

Thank you!


#20

OK.

Please be aware of the fact that JPG-compressed images are unsuited for scientific work because JPG-compression is lossy and introduces artifacts that can’t be removed. Always use original images in a lossless format such as TIF or PNG.
Converting a JPG-compressed image to TIFF- or PNG-format doesn’t make sense.

Please stay tuned

Herbie


#21

Sofie,

according to your order, here is a macro that works for me:

// imagej-macro "cellCount_batch" (Herbie G., 19. Dec. 2018)
requires( "1.52i" );
fileSuffix=".jpg";
setOption("BlackBackground", true);
dir=getDirectory("Choose a Folder");
list=getFileList(dir);
setBatchMode(true);
for (i=0; i<list.length; i++) {
   if (endsWith(list[i],fileSuffix)) {
         open(dir+list[i]);
         cellCount(list[i]);
   }
}
setBatchMode(false);
exit();
function cellCount(name) {
   updateResults();
   run("Lab Stack");
   run("Make Substack...", "channels=3");
   rename("temp");
   close(name);
   run("Bandpass Filter...", "filter_large=40 filter_small=1 suppress=None tolerance=0");
   run("Median...", "radius=4");
   setAutoThreshold("Yen dark");
   run("Convert to Mask");
   run("Watershed");
   run("Analyze Particles...", "size=20-Infinity exclude add");
   setResult("Label", nResults, name);
   setResult("Cell Count", nResults-1, roiManager("count"));
   close("temp");
   roiManager("delete");
}
// imagej-macro "cellCount_batch" (Herbie G., 19. Dec. 2018)

After starting this ImageJ-macro you are asked to choose the folder containing the images you like to analyze.
The counts are written to a Results table together with the file names.

Please report if the macro works for you.

If you need further changes, then you need to learn coding ImageJ-macros which is really easy.

If your work is published in a report, thesis or article you are to mention that you received help.

Good luck

Herbie


#22

It does work! Of course I’d like to acknowledge your help in future published work! Please let me know of details if you wish me to write something more personalised. You really saved us!

Sofie