【问题标题】:How to draw squares of certain pixel area centered on a centroid如何绘制以质心为中心的某个像素区域的正方形
【发布时间】:2019-09-28 04:32:03
【问题描述】:

我在 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

【问题讨论】:

  • 'ID_counter' undefined near line 24 column 12。应该只是N
  • 啊,是的,我之前在代码中指定了 ID_counter,但 N 也应该可以工作。
  • 你应该编辑你的代码/问题。如果提供的代码不能开箱即用,人们可能会不愿意帮助您。
  • 我现在已经添加了完整的代码。应该一切正常。
  • 好的,谢谢,我已将“ID_counter”更改为“N”。

标签: matlab image-processing centroid


【解决方案1】:

我已经通过以下方式解决了这个问题:

  • 将质心的 (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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-19
    • 2014-03-13
    • 1970-01-01
    • 2018-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多