New Thresholding Method


I have an idea that I would like to implement within a CellProfiler (CP) module and I was wondering if anybody could give me guidance, but first I will give you some background so you can understand the problem. Over the last few months I have been trying to develop a solid pipeline to identify foci within the nucleus of all types of cells. Although I have developed pipelines with settings that work well for some cell lines (ie human cancer cells that have bright foci and little background), these same settings do not work well with other cell lines [most notably mouse embryonic fibroblasts (MEFs) which have weaker foci signal and much more background]. Among other things MEFs need stricter thresholding correction values etc to account for all of the background. I have arranged the settings countless different ways and I cannot develop a single pipeline with middle ground powerful enough to use on different kinds of cells. I have come to this conclusion by comparing the object (foci) count CP gives for each nucleus versus a manual count repeated in triplicates and I find too much variability in the statistical results. I appreciate the per object analysis available within CP but from my experience it is still not precise enough for the needs of the laboratory I work for. So I went in a different direction and developed a thresholding method/program in Matlab and I am at the point where I want to implement and test it within CP. I’m a biologist and I am not a computer programmer, but I have familiarized myself by reading Python programming books and I can understand some code and the order that it follows. I downloaded the developer’s version and I am able to access the individual modules but I was wondering if you could give me advice to embed my code. What’s the best way to implement my idea and call Matab commands within Python? I would appreciate any help.


We do not have any way to directly call MATLAB code from within CellProfiler. That ability used to exist when CP was written in MATLAB, but was abandoned when CP was re-written in Python.

That being the case, we have pointers to contributing Python code on our wiki, in particular the “Writing a CellProfiler module” section. We also have tutorial with examples here. If it ends up being too daunting anyway, you can also contribute the code to us directly for our review.

Lastly, have you tried using EnhanceOrSupressFeatures with “Speckles” as the feature, in order to remove background intensities prior to foci detection? Per-object thresholding is fine, but we have found that the most common problem is the background florescence which often varies greatly not just object to object but within the same object as well. Using EnhanceOrSupressFeatures often does a good job removing this confounding factor, provided the foci are generally within a certain size or smaller.


Hi Mark,

I’m glad you asked for the MATLAB code I was actually in the process of sending it to you. Real quickly I want to explain the thresholding method and how to use it. I started this project by plotting a histogram of the pixel intensities and I noticed that there seemed to be two populations within the distribution. A large bell-shaped curve with low to mid range pixel intensity values that we believe to be background, followed by a tail of higher intensity values which probably belong to foci. We asume the first curve follows a Gaussian distribution, but if we try to determine a cutoff by fitting a normal distribution it would be skewed by the brighter intensities values on the right side of the graph. So instead, we modeled a truncated normal distribution which estimates a normal distribution based on the median (or mode) and sigma (estimated from the distribution ). If these values are plugged into the truncated normal equation we can determine the 99% confidence interval on the left side of the graph and set the cutoff at this point. Everything to the right we consider to be foci.

To analyze images and determine the cut off I use the following MATLAB commands.

I = imread (‘imageName’)
b = double (I(:))
c = (b(b>0))
% I use this variable to exclude the dark region around the cropped nucleus; if it is not exclude all pixel values with 0 will be counted and the fit will not be accurate

% Place both m files into your MATLAB directory

ci99 = getCI99( c ) – %this command will calculate the threshold and give the value in the command window.

% The previous command will also generate a histogram of the pixel intensity distribution, along with a fitted truncated normal model and the 99 % CI cutoff. The file is saved as a pdf in the MATLAB directory as “testFigure”

% I then use the threshold value that is calculated in the command window and apply it to the image.

%For example, if the calculated value is “ ci99 =1.3876e+04”, I would use the following command to apply the threshold to the nucleus

imshow (I, [13876, inf])

%This displays the nucleus only with pixels above the threshold value.

I will email you the files because the forum does not allow me to upload some of the file extensions
testFigure.pdf (5.61 KB)