MATLAB: I have an original image that has not been converted to binary. I have found the centroid for multiple areas, now I want to use them crop 288 pixel areas around centroid. I then want to calculate total summed pixel area. Can I use a for loop

centroidscropfor loopimage analysisimage processingImage Processing Toolboximcrop

I have centroid of multiple areas of an image. I want to use those centroids to crop 288 by 288 regions around the centroid and then calculate the summed pixel area of the ORIGINAL IMAGE(non-binary image). I want to do this with a for loop so that I can have a vector or some output of summed pixel areas of my cropped image from ALL of my centroids.
Here is my the code I have written so far, but I am only getting the summed pixel area of one centroid still.
if true
loadedimage=originalimage;
xCentroids=allCentroids(1:2:end); %already calculated x and y centroid locations from function regionprops
yCentroids=allCentroids(2:2:end);
[size1,size2]=size(xCentroids);
for k=1:1:size1
xmin=xCentroids(1,k);
ymin=yCentroids(1,k);
croppedimages=imcrop(loadedimage, [xmin ymin 288 288]);
pixellevel=graythresh(croppedimages);
end
end
I am not sure if it is even possible to use the function "imcrop" in a for loop in this way? Do I need to keep the original in binary form to use it in a loop in this way?

Best Answer

Well, yeah. Did you check the size of xCentroids? It's a 1-D row vector, so size1 is 1 and thus your for loop executes only once. Then, your loop is basically not saving any sums at all. You'd have to add this line
theSums(k) = sum(double(pixellevel));
And you don't need to do
xCentroids(1,k);
you can just do
xCentroids(k);
No rows index of 1 is needed because there is only one row.