Help integrating particle intensity

I'm trying to integrate the intensity from each of the particles in the following image to determine if there are any outliers that need to be discarded:
From what I understand, this is done by summing all the pixels in the PSF by summing the value of the all the pixels contained within a box surrounding each particle defined my the size of the maximum FWHM value.
So right now I have x and y locations for each particle and sigma values for the PSF. Here's the code I have now:
numParticles = numel(sigma);
fwhm = 2.355*sigma;
fwhm = max(fwhm);
fwhm = round(fwhm);
x0 = round(position(:,1)); y0 = round(position(:,2));
for ii=1:numParticles
x = x0(ii)+(-fwhm:fwhm); y = y0(ii)+(-fwhm:fwhm);
x = x(x>0); y = y(y>0);
intensity(ii) = sum(sum(im(x,y),2),1);
end
The problem I'm having is that I'm getting a lot of negative values, which doesn't make sense to me. Can someone point out if I'm doing something wrong?

Answers (1)

If you have the x and y coordinates of the particles, then just turn them into a binary image and compute the mean intensity and multiply by the areas:
mask = false(rows, columns);
for k = 1 : length(x)
mask(y(k), x(k)) = true;
end
props = regionprops(mask, grayImage, 'MeanIntensity', 'Area')
allAreas = [props.Area]
allIntensities = [props.MeanIntensity]
% Find integrated Gray Value, IGV
igv = allAreas .* allIntensities

5 Comments

Hi, thank you for your response and sorry for getting back so late.
I have a couple questions (sorry, I'm still pretty new to matlab).
What exactly does the mask = false(rows, columns); do? Should "rows" and "columns" already be defined? when I try this, it's asking if I mean mask = false(rose, columns);, which is kind of funny.
Also, I'm sure your way will work, but do you think you could help with the way I'm trying to do it? I basically want the sum of the intensities in the areas in each of these boxes, and I'm not sure how to do that.
I hope I've explained that well. Thanks a ton.
So I've been messing around a bit and came up with the following:
width = 2*fwhm; % fwhm = 5
height = 2*fwhm;
xCenter = position(:,1);
yCenter = position(:,2);
xLeft = xCenter - width/2;
yBottom = yCenter - height/2;
figure;
imshow(img);
axis equal;
axis off;
hold on;
for ii=1:numParticles
r(ii) = rectangle('Position', [xLeft(ii), yBottom(ii), width, height], 'EdgeColor', 'r', 'LineWidth', 2);
intensity(ii) = sum([xLeft(ii), yBottom(ii), width, height]);
end
and getting the follwing values:
intensity =
Columns 1 through 17
56.8851 179.1524 117.0116 169.1353 147.6620 132.1198 120.3778 132.3350 22.0791 107.3466 163.9513 136.0578 193.1910 166.2642 158.2211 106.9701 148.9788
Column 18
222.5909
Which seems reasonable to me.
Does this make sense/am I doing the right thing for what I'm trying to do?
Thanks again.
Looks like you spelled rows as rose instead of rows -- that's why it's asking you if you mean ""mask = false(rose, columns);"
You're summing the rectangular coordinates, not the gray scale image. You need to get the rows and columns for each rect
allBB = vertcat(props.BoundingBox);
column1 = ceil(allBB(:, 1));
row1 = (allBB(:, 2));
widths = allBB(:, 3);
heights = allBB(:, 4);
row2 = row1 + heights;
column2 = column1 + widths;
for k = 1 : numParticles
subImage = grayImage(row1(k):row2(k), column1(k):column2(k));
intensity(k) = sum(subImage(:)); % Add up all gray levels in this rectangle.
end
Hmm, well I didn't type anything and just copy-pasted what you gave me. Is that something that I should already have defined?
I tried the other code you gave and have some more questions. I just want to make sure I'm understanding everything.
For the bounding box, I used:
props = regionprops(img, 'BoundingBox');
Is this the right way to do this?
Is it correct to assume that this is identifying particles in the image and putting a box around them, and then the lines for the rows, columns, etc. are for the coordinates of the box? To be clear, "row" is referring to the bottom and top boundries of the box, and column is referring to the left and right boundaries, correct?
One of the problems I'm having is that the bounding box is only around 6 particles, which isn't all of them. So I tried using the coordinates that I already have with your code:
width = 2*fwhm; % fwhm = 5
height = 2*fwhm;
xCenter = position(:,1);
yCenter = position(:,2);
xLeft = xCenter - width/2;
yBottom = yCenter - height/2;
row1 = ceil(xLeft);
column1 = ceil(yBottom);
row2 = row1 + heights;
column2 = column1 + widths;
for k = 1 : numParticles
subImage = img(row1(k):row2(k), column1(k):column2(k));
intensity(k) = sum(subImage(:));
end
But I'm still getting a lot of negative values similar to the original ones, which is wrong. Any suggestions for what I'm doing wrong?
Sorry for all the noob questions. Thank you for all the help.
You should know how many rows and columns your image has. If you don't, get them this way:
[rows, columns, numberOfColorChannels] = size(grayImage);

Sign in to comment.

Asked:

J
J
on 26 Jul 2020

Commented:

on 4 Aug 2020

Community Treasure Hunt

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

Start Hunting!