查找图像中满足一个条件的像素索引
Find the index of pixels in an image statisfies one condition
我有一个图像包含 4 个值 {3,-3,1,-1} 如图
我们称其值等于轮廓中的1或-1像素的像素索引。这些像素将创建围绕黄色 (-3) 的轮廓。现在,我想找到轮廓中的所有索引像素,并向内和向外轮廓加上填充位置。作为红色,填充设置为1,因此,这些像素的索引包括轮廓中的像素{1,-1}和作为红色的填充索引。在该任务中,我想找到所有像素索引。如何在 matlab 代码中实现该想法。这是我在轮廓
中查找索引的代码
%% Let define the image I
idx=find(I==1|I==-1);
padding=1;
%%Continue
更新:我的预期结果如上图白色区域。因此,索引如 13,14,15,..21,24,...
更新
首先感谢Andrew 和Rayryeng 的回答。我想延长我的问题。正如上面的描述,轮廓是由{1,-1}创建的。现在,我想忽略 1 和 -1,所以我的图像只有 {3,-3}。而我定义的轮廓是{3,-3}边缘的像素,如图。保持相同的填充和像素索引的想法。如何找到轮廓和近轮廓中的像素索引(称为窄带轮廓)(预期结果为白色)
假设您的图像是 N×M 像素。在 MATLAB 中,数组按列顺序存储(有关详细信息,请参阅 http://www.mathworks.com/help/matlab/math/matrix-indexing.html)。您可以在列格式中使用 I
,如下所示。首先,轮廓像素由
给出
idx=find(I(:)==1|I(:)==-1);
现在,如果你想向下和向上填充很简单:
idx_up=idx - padding;
idx_up = idx_up(idx_up>0);
idx_down=idx + padding;
idx_down = idx_down(idx_down<=N*M);
请注意,idx_up
和 idx_down
也将包含轮廓像素。
同样,您可以填充到 left\right:
idx_left=idx - padding*N;
idx_left = idx_left(idx_left>0);
idx_right=idx + padding*N;
idx_right = idx_right(idx_right<=N*M);
并合并整体像素:
PaddedContour = false(N,M);
PaddedContour(unique([idx;idx_up;idx_down;idx_left;idx_right])) = true;
不太难,你走在正确的轨道上。如果您有 图像处理工具箱 ,我建议您查看 morphological operators. Specifically you want to use imdilate 我的代码包含您需要的所有详细信息。
%rather than using find, we create a binary mask. Its not the indicies of
%the matching elements as find gives. its is 1/true if the value matches the
%criteria, and 0/false otherwise.
mask = (im=1 | im=-1);
%create a 3x3 rectangle structuring element. We use a 3x3 because we want
%to expand the image by one pixel. basically the structring element (Strel)
%is our kernal, if you know image processing this is the same thing.
%a = [0 0 0 0;
% 0 1 1 1;
% 0 1 1 1;
% 0 1 1 1];
%our kernal is center at 2,2 (for this example) which are these elements
% 0 0 0 of a think a(1:3,1:3) now what the dialate operation
% 0 1 1 says is, if the majority of these pixels are ones... they
% 0 1 1 should probabaly all be ones so all those 0s will become ones
%the size of the kernal 3x3 ensures we are only growing our image one
%pixel, hope that makes sense
se = strel('square',3);
%now we dilate, or 'expand' our mask with our structuring element
expanded_mask = imdilate(mask,se);
%if you still want the indicies you can use find on our expanded mask
idx = find(expanded_mask==1);
编辑:没有形态学operations/image处理工具箱
此方法使用大量 for 循环,因此它不是最快的,并且不进行错误检查,但它会起作用。我的扩张函数表示如果大多数像素都是一个,那么它们就全部变成一个。
function expanded_mask=DilateBinaryImage(bin_im, kernal_size)
[max_row,max_col] = size(bin_im);
%since we are opening the mask (only adding 1s), we can start off with the
%same values of the mask, and simply add extra 1's as needed
expanded_mask = bin_im;
%we don't want to go off the edge of our image with this kernal
%so we offset it a bit
kern_padding = floor(kernal_size/2);
%this ignores the edges
for (curr_row=kern_padding+1:1:max_row - kern_padding)
for (curr_col=kern_padding+1:1:max_col - kern_padding)
%we do 2 sums, one for rows, one for columns
num_ones = sum(sum(bin_im(curr_row-kern_padding:curr_row+kern_padding,curr_col-kern_padding:curr_col+kern_padding)));
%if the majority of vlaues are 1, we use floor to help with corner
%cases
if (num_ones >= floor((kernal_size*kernal_size)/2))
%make all the values one
expanded_mask(curr_row-kern_padding:curr_row+kern_padding,curr_col-kern_padding:curr_col+kern_padding) = 1;
end
end
end
end
然后这样称呼它
kernal_size= 3;
mask = (I==1 | I==-1);
expanded_mask = DilateBinaryImage(mask, kernal_size);
idx = find(expanded_mask==1);
我的膨胀函数在二值图像的边缘不起作用。它只是准确地复制它们。
我有一个图像包含 4 个值 {3,-3,1,-1} 如图
我们称其值等于轮廓中的1或-1像素的像素索引。这些像素将创建围绕黄色 (-3) 的轮廓。现在,我想找到轮廓中的所有索引像素,并向内和向外轮廓加上填充位置。作为红色,填充设置为1,因此,这些像素的索引包括轮廓中的像素{1,-1}和作为红色的填充索引。在该任务中,我想找到所有像素索引。如何在 matlab 代码中实现该想法。这是我在轮廓
中查找索引的代码%% Let define the image I
idx=find(I==1|I==-1);
padding=1;
%%Continue
更新:我的预期结果如上图白色区域。因此,索引如 13,14,15,..21,24,...
更新
首先感谢Andrew 和Rayryeng 的回答。我想延长我的问题。正如上面的描述,轮廓是由{1,-1}创建的。现在,我想忽略 1 和 -1,所以我的图像只有 {3,-3}。而我定义的轮廓是{3,-3}边缘的像素,如图。保持相同的填充和像素索引的想法。如何找到轮廓和近轮廓中的像素索引(称为窄带轮廓)(预期结果为白色)
假设您的图像是 N×M 像素。在 MATLAB 中,数组按列顺序存储(有关详细信息,请参阅 http://www.mathworks.com/help/matlab/math/matrix-indexing.html)。您可以在列格式中使用 I
,如下所示。首先,轮廓像素由
idx=find(I(:)==1|I(:)==-1);
现在,如果你想向下和向上填充很简单:
idx_up=idx - padding;
idx_up = idx_up(idx_up>0);
idx_down=idx + padding;
idx_down = idx_down(idx_down<=N*M);
请注意,idx_up
和 idx_down
也将包含轮廓像素。
同样,您可以填充到 left\right:
idx_left=idx - padding*N;
idx_left = idx_left(idx_left>0);
idx_right=idx + padding*N;
idx_right = idx_right(idx_right<=N*M);
并合并整体像素:
PaddedContour = false(N,M);
PaddedContour(unique([idx;idx_up;idx_down;idx_left;idx_right])) = true;
不太难,你走在正确的轨道上。如果您有 图像处理工具箱 ,我建议您查看 morphological operators. Specifically you want to use imdilate 我的代码包含您需要的所有详细信息。
%rather than using find, we create a binary mask. Its not the indicies of
%the matching elements as find gives. its is 1/true if the value matches the
%criteria, and 0/false otherwise.
mask = (im=1 | im=-1);
%create a 3x3 rectangle structuring element. We use a 3x3 because we want
%to expand the image by one pixel. basically the structring element (Strel)
%is our kernal, if you know image processing this is the same thing.
%a = [0 0 0 0;
% 0 1 1 1;
% 0 1 1 1;
% 0 1 1 1];
%our kernal is center at 2,2 (for this example) which are these elements
% 0 0 0 of a think a(1:3,1:3) now what the dialate operation
% 0 1 1 says is, if the majority of these pixels are ones... they
% 0 1 1 should probabaly all be ones so all those 0s will become ones
%the size of the kernal 3x3 ensures we are only growing our image one
%pixel, hope that makes sense
se = strel('square',3);
%now we dilate, or 'expand' our mask with our structuring element
expanded_mask = imdilate(mask,se);
%if you still want the indicies you can use find on our expanded mask
idx = find(expanded_mask==1);
编辑:没有形态学operations/image处理工具箱 此方法使用大量 for 循环,因此它不是最快的,并且不进行错误检查,但它会起作用。我的扩张函数表示如果大多数像素都是一个,那么它们就全部变成一个。
function expanded_mask=DilateBinaryImage(bin_im, kernal_size)
[max_row,max_col] = size(bin_im);
%since we are opening the mask (only adding 1s), we can start off with the
%same values of the mask, and simply add extra 1's as needed
expanded_mask = bin_im;
%we don't want to go off the edge of our image with this kernal
%so we offset it a bit
kern_padding = floor(kernal_size/2);
%this ignores the edges
for (curr_row=kern_padding+1:1:max_row - kern_padding)
for (curr_col=kern_padding+1:1:max_col - kern_padding)
%we do 2 sums, one for rows, one for columns
num_ones = sum(sum(bin_im(curr_row-kern_padding:curr_row+kern_padding,curr_col-kern_padding:curr_col+kern_padding)));
%if the majority of vlaues are 1, we use floor to help with corner
%cases
if (num_ones >= floor((kernal_size*kernal_size)/2))
%make all the values one
expanded_mask(curr_row-kern_padding:curr_row+kern_padding,curr_col-kern_padding:curr_col+kern_padding) = 1;
end
end
end
end
然后这样称呼它
kernal_size= 3;
mask = (I==1 | I==-1);
expanded_mask = DilateBinaryImage(mask, kernal_size);
idx = find(expanded_mask==1);
我的膨胀函数在二值图像的边缘不起作用。它只是准确地复制它们。