0

I've written some code in MATLAB that converts an image (of stars) into a binary image using a set threshold and then labels each cluster of pixels (stars) that is above this threshold. The labelling produces an output: e.g.

[1 1 1 0 0 0 0 0 0
 1 1 0 0 0 2 2 2 0
 0 0 0 3 3 0 2 0 0
 0 0 0 3 3 0 0 0 0]

So each cluster of 1's, 2's, 3's etc. represents a star. I used the answer provided at this link: How to find all connected components in a binary image in Matlab? to label the pixels. After this the code then finds the area and centroids of each pixel cluster.

I now want to include some code that will automatically draw boxes of a certain pixel area centered on each centroid. For example, a centroid has a location of [41, 290] and the pixel cluster has an area of 6 pixels, I want to draw a box with an area of n x 6 pixels with the centre of the box being [41, 290]. And I need this to loop through every centroid and do the same.

How would I go about doing this?

The centroid and area code is shown below.

%% Calculate centroids of each labelled pixel cluster within binary image

N = max(B(:));    % total number of pixel labels generated in output array B
sum_v = zeros(N,1);    % create N x 1 array of 0's
sum_iv = zeros(N,1);    % "
sum_jv = zeros(N,1);    % "
for jj=1:size(B,2)    % search through y positions
   for ii=1:size(B,1)    % search through x positions
      index = B(ii,jj);
      if index>0     
         sum_v(index) = sum_v(index) + 1;
         sum_iv(index) = sum_iv(index) + ii;
         sum_jv(index) = sum_jv(index) + jj;
      end
   end
end
centroids = [sum_jv, sum_iv] ./ sum_v    % calculates centroids for each cluster

for pp = 1:N
    id_index = find(B == pp);
    pixels = numel(id_index);    %  counts number of pixels in each cluster    
    area(pp) = pixels;    % area = no. of pixels per cluster
end

hold on
for i=1:size(centroids,1)
    plot(centroids(i,1),centroids(i,2),'rx','MarkerSize',10)
end
hold off
  • `'ID_counter' undefined near line 24 column 12`. Should be just `N`? – HansHirse May 10 '19 at 09:33
  • Ah yeah I've specified ID_counter earlier in my code but N should work as well. –  May 10 '19 at 09:36
  • 1
    You should edit your code/question then. People might feel discouraged to help you, if the provided code doesn't work out-of-the-box. – HansHirse May 10 '19 at 09:40
  • I've added the complete code now. Should all work. –  May 10 '19 at 09:44
  • Ah, no! Your previous code was - apart from that minor issue - ok! Now, you provided a lot of unnecessary code, which is superfluous. Just edit your original code in that way, that it runs as it is. (I rolled back your original code.) – HansHirse May 10 '19 at 09:46
  • 1
    Ok thanks I've changed 'ID_counter' to 'N'. –  May 10 '19 at 09:54

1 Answers1

0

I've managed to work this out by doing the following:

  • Separate the (x,y) coordinates of the centroids into x and y
  • Subtract or add a certain amount of 'pixels' to each x and y coordinate to calculate xmin,xmax,ymin,ymax
  • Limit the 4 coordinates (boundingbox) to fit on the image
  • Find the width and height of each boundingbox by subtracting xmin from xmax, ymax from ymin etc
  • Then use a for loop and rectangle function to go through the list of centroids and apply the boundingbox coordinates to the image.

The code is below.

N = max(B(:));    % total number of pixel labels generated in output array
sum_total = zeros(N,1);    % create N x 1 array of 0's
sum_yv = zeros(N,1);    % "
sum_xv = zeros(N,1);    % "
for xx=1:size(B,2)    % search through y positions
   for yy=1:size(B,1)    % search through x positions
      index = B(yy,xx);
      if index>0
          sum_total(index) = sum_total(index) + 1;
          sum_yv(index) = sum_yv(index) + yy;
          sum_xv(index) = sum_xv(index) + xx;
      end
   end
end
centroids = [sum_xv, sum_yv] ./ sum_total    % calculates centroids for each cluster


x_lower_limits = centroids(:,1)-4;
y_lower_limits = centroids(:,2)+4;    % lower on image means larger y coord number
x_upper_limits = centroids(:,1)+4;
y_upper_limits = centroids(:,2)-4;    % higher on image means lower y coord number

x_lower_limits(x_lower_limits<1)=1;    % limit smallest x coord to image axis (1,y)
y_lower_limits(y_lower_limits>size(binary_image,1))=size(binary_image,1);    % limit largest y coord to image axis (x,517)
x_upper_limits(x_upper_limits>size(binary_image,2))=size(binary_image,2);    % limit largest x coord to image axis (508,y)
y_upper_limits(y_upper_limits<1)=1;    % limit smallest y coord to image axis (x,1)


width = x_upper_limits(:,1) - x_lower_limits(:,1);    % width of bounding box
height = y_lower_limits(:,1) - y_upper_limits(:,1);    % height of bounding box

% for pp = 1:ID_counter
%     id_index = find(B == pp);
%     pixels = numel(id_index);    %  counts number of pixels in each cluster    
%     area(pp) = pixels;    % area = no. of pixels per cluster
%     gray_area = area*2;
% end


hold on
for xl=1:size(x_lower_limits,1)
                rectangle('Position',[x_lower_limits(xl,1) y_upper_limits(xl,1) width(xl,1) height(xl,1)],'EdgeColor','r')
end
for i=1:size(centroids,1)
    plot(centroids(i,1),centroids(i,2),'rx','MarkerSize',10)
end
hold off