如何绘制以质心为中心的某些像素区域的正方形
How to draw squares of certain pixel area centered on a 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 等组成的簇代表一颗星。我使用 link: How to find all connected components in a binary image in Matlab? 中提供的答案来标记像素。在此之后,代码会找到每个像素簇的面积和质心。
我现在想要包含一些代码,这些代码将自动绘制以每个质心为中心的特定像素区域的框。例如,质心的位置为 [41, 290],像素簇的面积为 6 个像素,我想绘制一个面积为 n x 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
我通过执行以下操作设法解决了这个问题:
- 将质心的 (x,y) 坐标分成 x 和 y
- 对每个x和y坐标减去或加上一定量的'pixels',计算出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
我在 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 等组成的簇代表一颗星。我使用 link: How to find all connected components in a binary image in Matlab? 中提供的答案来标记像素。在此之后,代码会找到每个像素簇的面积和质心。
我现在想要包含一些代码,这些代码将自动绘制以每个质心为中心的特定像素区域的框。例如,质心的位置为 [41, 290],像素簇的面积为 6 个像素,我想绘制一个面积为 n x 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
我通过执行以下操作设法解决了这个问题:
- 将质心的 (x,y) 坐标分成 x 和 y
- 对每个x和y坐标减去或加上一定量的'pixels',计算出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