title: How to calculate weighted (grayscale) centroids of pixel clusters contained within bounding boxes on an image
body: <p>I've written some code in MATLAB that converts an image (of stars) into a grayscale image and then 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.</p>
  
  <pre class="lang-matlab prettyprint-override"><code>[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]
  </code></pre>
  
  <p>So each cluster of 1's, 2's, 3's etc. represents a star. After this the code then finds the centroids of each pixel cluster and draws a bounding box around each centroid (centered on the centroid) with an 8 x 8 pixel area. The bounding box limits are made by finding xmax, xmin, ymax, ymin of each calculated centroid, which involves either adding or subtracting 4 (pixels) from the x and y coordinates of each centroid.</p>
  
  <p>The weighted centroid is calculated like so:</p>
  
  <p>x_coordinate_centroid = sum(x_coordinate .* pixel_values) / sum_pixel_values
  y_coordinate_centroid = sum(y_coordinate .* pixel_values) / sum_pixel_values</p>
  
  <p>x/y_coordinate and pixel values are for the pixels contained within each bounding box.</p>
  
  <p>The bounding box would surround an 8 x 8 pixel area (with the given intensities) on the grayscale image such as: </p>
  
  <pre class="lang-matlab prettyprint-override"><code>[100 100 100 90  20  20  0   0
   80  90  100 90  20  30  0   0
   50  70  100 70  30  0   20  0
   50  0   0   60  30  30  0   0
   0   0   0   0   0   0   0   0
   0   50  0   0   0   0   0   0
   0   40  0   0   0   0   0   0
   0   20  0   0   0   0   0   0]
  </code></pre>
  
  <p>The top left value ([xmin, ymax]), for example, could have image coordinates [41, 14] and an intensity of 100.</p>
  
  <p>The output from my code, for example, could give 5 bounding boxes across the grayscale image. I now need to write code that automatically calculates the weighted centroid of each bounding box region. I'm not sure how to go about this, does anyone have any ideas how this can be achieved?</p>
  
  <p>My code for calculating the centroids and their bounding boxes is shown below.</p>
  
  <pre class="lang-matlab prettyprint-override"><code>
  %% Calculate centroids of each labelled pixel cluster within binary image
  
  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&gt;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&lt;1)=1;    % limit smallest x coord to image axis (1,y)
  y_lower_limits(y_lower_limits&gt;size(binary_image,1))=size(binary_image,1);    % limit largest y coord to image axis (x,517)
  x_upper_limits(x_upper_limits&gt;size(binary_image,2))=size(binary_image,2);    % limit largest x coord to image axis (508,y)
  y_upper_limits(y_upper_limits&lt;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
  
  
  hold on
  
  for xl=1:size(x_lower_limits,1)
                 r(xl)=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
  
  
  %% 
  
  
  </code></pre>