MATLAB: Creating image mask of 288 by 288 pixel from centroid location, threshold the image mask and out put summed pixel area

croppingimage maskimage processingImage Processing Toolboximcroppixel valuethresholdthresholding

Hi all,
I am very new to the image analyst world. I have an image that has had the centroids calculated ( http://www.mathworks.com/matlabcentral/answers/299323-finding-centroid-of-an-image-with-a-summed-pixel-area-greater-than-120-pixels-squared ) thanks Image Analyst!
So now with these centroid locations (in struct array), I would like to take a 288 pixel by 288 pixel region around the centroid origins. My goal is to find the pixel value of this region, which I believe I can do with Otsu's method (graythresh function).
My attempts to calculate the coordinates for imcrop function for all my centroids have been unsuccessful, so I thought I would reach out to the MATLAB professionals.
Any help is of course appreciated.
Here is my attempt:
binaryImage=imregionalmax(loadedimage)
binaryImage=bwareaopen(binaryImage, 120);
labeledImage=logical(binaryImage);
props=regionprops(labeledImage,'Centroid');
allCentroids=allCentroids[props.Centroid];
xCentroids=allCentroids(1:2:end);
yCentroids=allCentroids(2:2:end);
allCentorids=allCentroids';
[size1,size2]=size(allCentroids);
for k=1:1:size1
xmin=xCentroids(1,k);
ymin=yCentroids(1,k);
width=288;
height=288;
crop=imcrop(binaryImage, [xmin ymin width height]);
pixellevel=graythresh(crop)
end
end

Best Answer

Not sure what "the centroid's origin" means, but it appears that you want the upper left centroid, and to take a box going 288 pixels down from that and 288 pixels over from that, regardless if other centroids are included in that cropping box. So to do that, you'd do (without any loop at all):
xmin = floor(min(xCentroids));
ymin = floor(min(yCentroids));
width = 288;
height = 288;
croppedImage = imcrop(binaryImage, [xmin, ymin, width, height]);
that gives you the sub-image (the pixel values) of this 288x288 square of the binary image. I have no idea what you mean by "find the pixel value of this region" and why you think graythresh() may help you. There is nothing to threshold - it's already a binary image! And besides, you already have the pixel values because they're in the array croppedImage, so I don't know why you think you need any additional operations to get them.