Is there a way to preallocate the struct array formed when using regionprops-centroid so that i can use the array in a loop with images with different numbers of centroids?
1 view (last 30 days)
Show older comments
I have ~600 images to process. In each image there are ~600 light dots (centroids). I'm trying to the track the pixel locations of the centroids in each image. The issue is sometimes the dots go dark for an image or two. I have regionprops in a loop to store all the centroids in a struct array, however, when a dot goes dark the loop/program fails. I tried preallocating the struct to no avail.
Is there a way to have regionprops either: fill the struct with [0,0] points if some of the dots disappear?; fill a preallocated struct (with all [0,0]) with as many centroids as it can find?; or have it use the pixel location from the previous image when a centroid disappears? Or is there an easy way to get past this dimension mismatch?
field = 'centroid';
value(1:425,1) = {zeros(2,1)};
s(:,:,numframes) = struct(field,value);
rect = [574 710 900 643]; %[X,Y,dX,dY]
for j = 1:numframes
z(:,:,j) = imcrop(f(:,:,j), rect);
r(:,:,j) = z(:,:,j) > 8;
u(:,:,j) = bwareaopen(r(:,:,j),10,8);
%y(:,:,j) = im2bw(z(:,:,j),graythresh(z(:,:,j))); %Create b&w image
b(:,:,j) = imclearborder(u(:,:,j),8); %Clear edge sensors from image
d(:,:,j) = padarray(b(:,:,j),[6 6]); %Pad image with black border (0's)
s(:,:,j) = regionprops(d(:,:,j),'centroid'); %Locate centroids
centroids(:,:,j) = cat(1,s(:,:,j).Centroid); %Array of [x,y] loc's
g(:,:,j) = padarray(z(:,:,j),[6 6]);
end
I attached an example image.
Let me know if you have any ideas...Thanks.
0 Comments
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!