如何绘制以质心为中心的特定像素区域的正方形

时间:2019-05-10 09:29:13

标签: matlab image-processing centroid

我已经在MATLAB中编写了一些代码,该代码使用设置的阈值将(恒星)图像转换为二进制图像,然后标记高于此阈值的每个像素(恒星)簇。标签产生输出: 例如

[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]

因此1、2、3等的每个簇代表一个星星。我使用以下链接提供的答案:How to find all connected components in a binary image in Matlab?标记像素。然后,代码将找到每个像素簇的面积和质心。

我现在想包含一些代码,这些代码将自动绘制以每个质心为中心的某个像素区域的框。例如,质心的位置为[41,290],像素簇的面积为6像素,我想绘制一个面积为nx 6像素的框,框的中心为[41,290] 。我需要它遍历每个质心并执行相同的操作。

我将如何去做?

质心和区号如下所示。

%% 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

1 个答案:

答案 0 :(得分:0)

通过以下操作,我设法解决了这个问题:

  • 将质心的(x,y)坐标分为x和y
  • 在每个x和y坐标上减去或添加一定数量的“像素”以计算xmin,xmax,ymin,ymax
  • 限制4个坐标(边界框)以适合图像
  • 通过从xmax减去xmin,从ymin等减去ymax来找到每个边界框的宽度和高度
  • 然后使用for循环和矩形函数浏览形心列表,并将边界框坐标应用于图像。

代码在下面。

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