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?

조회 수: 1 (최근 30일)
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?

채택된 답변

Image Analyst
Image Analyst 2016년 8월 13일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by