Take 3x3 nearest neighbours round centroids of all spots and find the median of their mean

81 vues (au cours des 30 derniers jours)
Hello, I have an image (TIFF) that I calculate the centroids and extract the median intensity of all these centroids.
I have the centroid positons in x1,y1 and loop through them the obtin the intensity of each spot and then take the median
Intensity(i)=IM(y1(i),x1(i)); %Centroid intensity
...
medIntensity=median(Intensity(:));
I now want to take a 3x3 nearest neighbours round each of the x1,y1 points and obtain the median of their mean.
Are there any suggestions how I achieve this.
Thanks
Jason

Réponse acceptée

Matt J
Matt J le 15 Jan 2025
Modifié(e) : Matt J le 15 Jan 2025
sz=size(IM);
centroidIdx=sub2ind(sz,y1,x1);
T=combinations(1:3,1:3);
delta=sub2ind(sz,T{:,2},T{:,1})'-(sz(1)+2);
result=median( mean( IM(centroidIdx(:) + delta),2) )
  7 commentaires
Jason
Jason le 11 Mar 2025 à 15:52
Hi thanks for this, but now this part doesn't work
result=median( mean(IM(centroidIdx(:) + delta),2) )
Matt J
Matt J le 11 Mar 2025 à 16:09
It should work, but be mindfult that your centroids must be deep enough in the interior of the image that the whole of each nxn neighborhood fits within the image boundaries.
IM=rand(24,30);
n=5;
hn=ceil(n/2);
C=combinations(hn:6:height(IM),hn:8:width(IM));
[y1r,x1r]=deal(C{:,1}, C{:,2});
sz=size(IM);
centroidIdx=sub2ind(sz,round(y1r),round(x1r)); %x1r, y1r and not integers so round
T=combinations(1:n,1:n);
delta=sub2ind(sz,T{:,2},T{:,1})'- sub2ind(sz,hn,hn);
result=median( mean(IM(centroidIdx(:) + delta),2) )
result = 0.5203

Connectez-vous pour commenter.

Plus de réponses (2)

Matt J
Matt J le 15 Jan 2025
Modifié(e) : Matt J le 15 Jan 2025
movingMean=conv2(IM,ones(3)/9,'same');
centroidIdx=sub2ind(size(IM),y1,x1);
result=median(movingMean(centroidIdx))

Image Analyst
Image Analyst le 4 Mar 2025 à 22:30
If you've used regionprops to find the centroids or the spots, then why not simply also ask it for the mean intensities?
props = regionprops(mask, 'Centroid', 'MeanIntensity');
xyCentroids = vertcat(props.Centroid);
meanIntensities = [props.MeanIntensity]
medianMeanIntensity = median(meanIntensities)
If you want different diameters around the spots, simply call imdilate to enlarge the spots mask.
  10 commentaires
Jason
Jason le 7 Mar 2025 à 6:59

Thankyou, this is exactly what I want

Jason
Jason le 7 Mar 2025 à 11:12
So here is the version as per your suggestion (thankyou so much)
1: Whats the best way to handle where the 5x5 square extends out of the image (i.e. where the green arrow is). I see there are padding options in conv2, but ideally I'd just like to ignore these ones. Perhaps I just go through all my centroids and remove any within 5 pixels of the edge of the image?
2: Rather than means, I'd prefer the median value within each 5x5 - is there a Kernel for this?
3: Also how woudl the Kernel change for a circular shape rather than square

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by