Counting cells in a sliding window
2 views (last 30 days)
Show older comments
I have a series of binary images (already background corrected + filtered for identification of cells), each of them consisting of circular cells. I would like to count the number of cells in a sliding 128 x 128 pixels in each image and do this for all images. As a result, I would like to plot (pcolor) cells distribution in each image. I would be glad if you could direct me.
base_dir = 'K:\images\';
cd(base_dir);
imagefiles = dir('*.jpg');
nfiles = length(imagefiles);
for ii=1:nfiles
currentfilename = imagefiles(ii).name;
currentimage = imread(currentfilename);
I(:,:, ii) = currentimage;
end
[M,N,~] = size(I);
rr = 128; cc = 128;
xx = 10; yy = 10; % if overlapping needed
numBlocksYY = numel(1:rr-xx:(M-(rr-1)));
numBlocksXX = numel(1:cc-yy:(N-(cc-1)));
counter = 1;
for ii=1:rr-xx:(M-(rr-1))
for jj=1:cc-yy:(N-(cc-1))
fprintf('[%d:%d, %d:%d]\n',ii,ii+rr-1,jj,jj+cc-1);
NN{counter} = bwconncomp(I(ii:(ii+rr-1), jj:(jj+cc-1), : ));
counter = counter + 1;
end
end
Here is one image:

2 Comments
Image Analyst
on 31 Mar 2016
At least attach an image or two so people can run your code. http://www.mathworks.com/matlabcentral/answers/6200-tutorial-how-to-ask-a-question-on-answers-and-get-a-fast-answer
Accepted Answer
Image Analyst
on 1 Apr 2016
Edited: Image Analyst
on 1 Apr 2016
Call bwulterode() to erode each blob down to a single point. Then use conv2() to count them in an overlapping window:
binaryImage = bwulterode(binaryImage);
countImage = conv2(double(binaryImage), ones(128));
No for loops needed. This has the window sliding along in steps of 1 pixel. If you want more than that, simply subsample the image or (much less simply) use blockproc().
0 Comments
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!