Count number of identified cells in a region of interest?

9 views (last 30 days)
For a biological image in which certain cells have been stained, I need to count the number of stained cells in a given region of the image. I have a cell identification algorithm (which I did not write) that contains the x/y coordinates of all cells identified in the image. I can scatter those coordinates on top of the image as green dots with this command.
p = findfiles(input_path, '*.tif');
q = findfiles(results_path, '*.mat');
k = 1;
image = imread(p{k});
imshow(image)
hold on
load(q{k});
x = data(:, 2);
y = data(:, 1);
scatter(x, y, '.g', 'SizeData', 100);
What I need to do is 1) Custom draw multiple ROIs on the scattered image, 2) receive a report of how many cells (green dots) were identified in each ROI, and 3) save the scattered image with the custom ROIs as a separate file that can be recalled later.
I have had limited success using impoly to generate a custom ROI, but cannot figure out how to count the number of points within that ROI.
Any help would be greatly appreciated!
Mike

Answers (1)

Image Analyst
Image Analyst on 28 Feb 2013
If the ROIs were rectangles, it should be trivial. What shape are you ROIs? Even if they have some weird shape, you can use inpolygon() to determine if the point is inside your ROI.
  3 Comments
Michael
Michael on 28 Feb 2013
Success!! (Kind of...)
Okay, I have added the following two lines of code that successfully count the number of points in my roi...
[ROI1, xi, yi] = roipoly();
InROI = inpolygon(x, y, xi, yi);
This still leaves me with two problems though. 1) The ROI disappears as soon as I create the mask. I need to be able to label the ROI and have it stay on the figure for evidence of where it was drawn. 2) InROI is a large one-cell matrix matching my "data" matrix only with true false values. I need to generate a report of the number of "true" values for each ROI in a simple format. I can copy the data for each output matrix into excel and filter for true, but clearly this seems the wrong approach with this program. I will keep working on learning the solution, unless there is another trivial MATLAB command I am missing.
Thank you for your assistance!
Mike
Image Analyst
Image Analyst on 1 Mar 2013
You can call plot(xi, yi, 'ro-') to plot the polygon over the image. I don't know what "data matrix" you're talking about. Is that InROI, which might be an array of N rows (for the n points) by M columns (for M possible ROIs)? Why do you need to do anything with Excel? I see no reason for that. Also, I'm not sure what "filter for true" means.
For you step 3 where you say "save the scattered image with the custom ROIs", I have little idea what you want, so let's go over some of the possibilities. Let's say you have 30 points and 3 ROIs, and let's say 15 points are in ROI1 and 5 points are in ROI2, and there are no points in ROI3, and 10 points outside of any ROI. Now, do you want to save the full size image with the pixels outside ROI1 and ROI2 (the only ROIs with points inside of them) blackened? Or do you want to crop ROI1 and ROI2 out of the original full size image, thus making smaller rectangular images defined by the bounding box of each region, and save those two rectangular bounding box images as two separate image files? Please try to be as explicit and detailed as you can to avoid uncertainty, because I'm not going to give code on multiple possible situations when you only need one situation.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!