- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我一直试图在不使用“bwlabel”函数的情况下使用二值图像中的 8 个邻居来查找所有连通分量。
比如我的输入矩阵是:
a =
1 1 0 0 0 0 0
1 1 0 0 1 1 0
1 1 0 0 0 1 0
1 1 0 0 0 0 0
0 0 0 0 0 1 0
0 0 0 0 0 0 0
我想要这样的东西:
a =
1 1 0 0 0 0 0
1 1 0 0 2 2 0
1 1 0 0 0 2 0
1 1 0 0 0 0 0
0 0 0 0 0 3 0
0 0 0 0 0 0 0
此图像中有 3 个连接的对象。
最佳答案
这是图像处理中的常见问题。有许多变体,例如对图像中的区域进行洪水填充,或查找哪些像素属于同一区域。一种常见的方法是使用 depth first search .这个想法是你从左到右,从上到下遍历你的图像,对于遇到的任何等于 1 的像素,你将它们添加到堆栈中。对于堆栈中的每个像素,您从堆栈中弹出,然后查看该像素周围的相邻像素。将任何为 1 的像素添加到堆栈中。您需要在任何您已经访问过 的像素处保留一个额外的变量,不要将这些添加到堆栈中。当堆栈为空时,我们已经找到了那些是整个区域的像素,因此您使用唯一 ID 标记这些像素。然后重复此过程,直到用完图像中的所有区域。
因此,鉴于您的矩阵存储在 A
中,这是基本算法:
初始化一个与A
大小相同的逻辑
数组。这将记录我们检查或访问过的像素。还将输出数组 B
初始化为全零,从而为您提供您正在寻找的所有连接组件。最后为零的任何位置都不属于任何连接的组件。同时初始化一个 ID 计数器,用于跟踪每个连接组件的标签。
对于矩阵中的每个位置:
一个。如果位置为 0
,则将此位置标记为已访问并继续。
如果我们已经访问过该位置,则继续。
如果我们还没有访问过这个位置...转到步骤 #3。
将这个未访问的位置添加到堆栈中。
一个。虽然这个堆栈不为空......
从堆栈中弹出这个位置
如果我们访问过此位置,则继续。
否则,将此位置标记为已访问并使用连接的组件 ID 标记此位置。
给定此位置,查看 8 个相邻像素。
移除这个列表中那些已经被访问过,不等于1或者超出矩阵边界的像素
无论剩余的位置是什么,都将它们添加到堆栈中。
一旦堆栈为空,递增计数器,然后返回步骤 #2。
继续前进,直到我们访问了数组中的所有位置。
不用多说,这是代码。
%// Step #1
visited = false(size(A));
[rows,cols] = size(A);
B = zeros(rows,cols);
ID_counter = 1;
%// Step 2
%// For each location in your matrix...
for row = 1 : rows
for col = 1 : cols
%// Step 2a
%// If this location is not 1, mark as visited and continue
if A(row,col) == 0
visited(row,col) = true;
%// Step 2b
%// If we have visited, then continue
elseif visited(row,col)
continue;
%// Step 2c
%// Else...
else
%// Step 3
%// Initialize your stack with this location
stack = [row col];
%// Step 3a
%// While your stack isn't empty...
while ~isempty(stack)
%// Step 3b
%// Pop off the stack
loc = stack(1,:);
stack(1,:) = [];
%// Step 3c
%// If we have visited this location, continue
if visited(loc(1),loc(2))
continue;
end
%// Step 3d
%// Mark location as true and mark this location to be
%// its unique ID
visited(loc(1),loc(2)) = true;
B(loc(1),loc(2)) = ID_counter;
%// Step 3e
%// Look at the 8 neighbouring locations
[locs_y, locs_x] = meshgrid(loc(2)-1:loc(2)+1, loc(1)-1:loc(1)+1);
locs_y = locs_y(:);
locs_x = locs_x(:);
%%%% USE BELOW IF YOU WANT 4-CONNECTEDNESS
% See bottom of answer for explanation
%// Look at the 4 neighbouring locations
% locs_y = [loc(2)-1; loc(2)+1; loc(2); loc(2)];
% locs_x = [loc(1); loc(1); loc(1)-1; loc(1)+1];
%// Get rid of those locations out of bounds
out_of_bounds = locs_x < 1 | locs_x > rows | locs_y < 1 | locs_y > cols;
locs_y(out_of_bounds) = [];
locs_x(out_of_bounds) = [];
%// Step 3f
%// Get rid of those locations already visited
is_visited = visited(sub2ind([rows cols], locs_x, locs_y));
locs_y(is_visited) = [];
locs_x(is_visited) = [];
%// Get rid of those locations that are zero.
is_1 = A(sub2ind([rows cols], locs_x, locs_y));
locs_y(~is_1) = [];
locs_x(~is_1) = [];
%// Step 3g
%// Add remaining locations to the stack
stack = [stack; [locs_x locs_y]];
end
%// Step 4
%// Increment counter once complete region has been examined
ID_counter = ID_counter + 1;
end
end %// Step 5
end
根据您的示例矩阵,这就是我得到的 B
:
B =
1 1 0 0 0 0 0
1 1 0 0 2 2 0
1 1 0 0 0 2 0
1 1 0 0 0 0 0
0 0 0 0 0 3 0
0 0 0 0 0 0 0
要修改代码以在 4-connected 区域中搜索,即仅北、东、西和南,您看到的部分 %//Look at the 8 neighboring locations
,即是:
%// Look at the 8 neighbouring locations
[locs_y, locs_x] = meshgrid(loc(2)-1:loc(2)+1, loc(1)-1:loc(1)+1);
locs_y = locs_y(:);
locs_x = locs_x(:);
要以 4-connected 方式搜索,您只需修改此代码以仅提供这些主要方向:
%// Look at the 4 neighbouring locations
locs_y = [loc(2)-1; loc(2)+1; loc(2); loc(2)];
locs_x = [loc(1); loc(1); loc(1)-1; loc(1)+1];
其余代码保持不变。
bwlabel
函数如果你想匹配MATLAB的bwlabel
的输出函数,bwlabel
按主列或 FORTRAN 顺序搜索连通分量。上面的代码按行主要或 C 顺序搜索。因此,您只需要先按列搜索,而不是像上面的代码那样按行搜索,您可以通过交换两个 for
循环的顺序来完成此操作。
具体来说,而不是做:
for row = 1 : rows
for col = 1 : cols
....
....
你会这样做:
for col = 1 : cols
for row = 1 : rows
....
....
现在应该复制 bwlabel
的输出。
关于image - 如何在 Matlab 中找到二值图像中的所有连通分量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26332883/
我想使用错误组件显示我的错误消息,但不想在 中加载组件对于经过身份验证的用户,导航菜单也不应显示。 我有这样的应用程序组件.. 我有错误处理程序,它使用 router.navigate 路由
我正在尝试获取 RGB 图像,将其转换为 LAB(又名 CIE L* a* b*)色彩空间,然后提取 L* 分量。 这是我的代码: from skimage import io, color from
我在我的一个模型中定义了以下常量。 export const NEWS_TYPE_TEXT = { News: 'News', Interview: 'Intervie
我有一个Electron(6)/Angular(8)应用程序。 在正面( Angular ),我通过IPCRenderer向背面发送一条消息。 在背面,IPCMain接收消息并执行所需的操作,例如,获
我正在尝试在我的应用程序中创建一个可重用的 quickView 模式,以使用 ng-bootstrap modal library 动态加载任何组件。 就我加载文档中所示的相同示例组件而言,它工作正常
我需要将一个名为“photos”的数组从我的 component.ts 传递到 component.html。这是我的 component.ts 文件 export class PhotosCompo
我有一个按钮,单击该按钮会转到新路线并打开附加到该路线的另一个组件。 有没有一种方法可以从 DOM 中删除我们单击以转到不同组件的组件?示例:当单击“单击我返回主页”按钮时,它会打开另一个组件。在这种
这个问题在这里已经有了答案: Detect click outside Angular component (12 个答案) 5天前关闭。 我知道这方面有无数的问题,我尝试了每一个解决方案,但没有一个
我想将显示值的格式传递给 Angular 分量。 例如: format="'(utc, offset) location (tz)'" === '(UTC -04:00) New York (EDT)
我正在使用 Angular 组件将动态图表加载到我的小部件中: 这是我的组件的示例: angular.module("generalApp").component("chartPie", { temp
假设我有一个组件在被点击时发出一个事件,即 @Component({ selector: 'component-checkout-payment', template:
我有一个问题。 我正在处理另一个人的代码,有一个 JFrame 有很多 JSeparators(他用它们作为“面板”的边框)现在我将它们替换为 JBorderedPanel 类,该类遵循与整体相同的边
所以我在这里想做的是制作一个 Angular 组件并将其注入(inject)到我的 Angular 应用程序中。这是 Angular 分量的代码: (function(angular) { 'use
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 4 年前。 Improve this ques
我正在创建一个像这样的可重用组件: submit 我想在属性 isDisabled 为 true 时禁用点击事件,我尝试了类似的操作,但它不起作用。 packages/component/my-b
一种简单的说法是,当 RGB 分量相等时,它们形成灰色。然而,这还不是全部,因为如果它们只有细微的差别,它们看起来仍然是灰色的。 假设观看者具有健康的色彩视觉,我如何确定给定值是否会被视为灰色(大概具
您好我正在尝试使用带 Angular Electron 构建桌面应用程序,主要问题是在用户登录后我找不到正确加载主要组件的方法。正如您在 main.js 中看到的,这是我创建两个窗口(1 个用于登录的
new AngularJS 1.5 中似乎没有“替换”选项组件概念(就像指令一样)。 如果我想要表格行,你有什么建议元素作为组件?就有效的 HTML 而言是不可能的吗? 真实示例:mailBox组件内
我有颜色=#12FFFF。这是这种格式的颜色,其中 12FFFF 是十六进制数。现在我想获取每个独立的 R、G、B 分量的十进制。我该如何在java中做到这一点? 最佳答案 目前尚不清楚你的问题是什么
我需要一些关于 Java 的 ImageIO API 的帮助。我似乎迷失在 ComponentColorModel 类中。我需要逐像素检查 *.png 文件来检测它是灰度图像还是彩色图像。但是,我不知
我是一名优秀的程序员,十分优秀!