我正在寻找一个如何在python中使用OpenCV的ConnectedComponentsWithStats()函数的示例,请注意,这仅适用于OpenCV 3或更高版本.官方文档仅显示了C++的API,即使在为python编译时该函数存在.我无法在网上找到它.
我们目前正在尝试使用OpenCV,C++版本中提供的方法检测医疗器械图像中的对象区域.示例图像如下所示:
以下是我们遵循的步骤:
这种方法适用于图像1
,结果如下:
到目前为止,一切都很好,但另一个图像样本使我们的工作复杂化如下所示.
在物体下面放一条浅绿色毛巾会产生这样的图像:
像我们之前那样过滤了区域后,我们得到了这个:
显然,这不是我们需要的......我们除了这样的东西:
我正在考虑聚集最近发现的连接组件(不知何故!!),这样我们可以最大限度地减少毛巾存在的影响,但是不知道它是否可行,或者之前有人试过这样的东西?此外,有没有人有更好的想法来克服这种问题?
提前致谢.
opencv cluster-analysis object-detection connected-components
我有许多公司的董事数据,但有时候"XYZ的董事John Smith"和"ABC的董事John Smith"是同一个人,有时他们不是.此外,"XYZ的导演约翰J.史密斯"和"ABC的导演约翰史密斯"可能是同一个人,也可能不是.通常检查附加信息(例如,关于"约翰史密斯,XYZ主任"和"约翰史密斯,ABC主任"的传记数据的比较)使得有可能解决两个观察是否是同一个人.
本着这种精神,我正在收集识别匹配对的数据.例如,假设我有以下匹配对:{(a, b), (b, c), (c, d), (d, e), (f, g)}
.我想使用关系"与人相同"的传递属性来生成"连通组件" {{a, b, c, d, e}, {f, g}}
.那是{a, b, c, d, e}
一个人,{f, g}
是另一个人.(该问题的早期版本提到了"派系",这显然是别的东西;这可以解释为什么find_cliques
在networkx
给出"错误"结果(为了我的目的).
以下Python代码完成了这项工作.但我想知道:是否有更好的(计算成本更低)方法(例如,使用标准或可用的库)?
这里和那里似乎有相关的例子(例如,python中的Cliques),但这些是不完整的,所以我不确定他们指的是什么库或如何设置我的数据来使用它们.
def get_cliques(pairs):
from sets import Set
set_list = [Set(pairs[0])]
for pair in pairs[1:]:
matched=False
for set in set_list:
if pair[0] in set or pair[1] in set:
set.update(pair)
matched=True
break
if not matched:
set_list.append(Set(pair))
return set_list
pairs …
Run Code Online (Sandbox Code Playgroud) 如何用open cv在python中实现连通组件标签?这是一个图像示例:
我需要连接组件标签来分隔黑白图像上的对象.
操作 使用连通分量基于距离和标签对点进行聚类。
问题 NetworkX 节点存储属性和 Pandas DataFrame 之间的来回切换
尝试 使用不同的函数,如 Scikit NearestNeighbours,但导致数据的来回移动相同。
问题 是否有更简单的方法来执行此连接组件操作?
例子
import numpy as np
import pandas as pd
import dask.dataframe as dd
import networkx as nx
from scipy import spatial
#generate example dataframe
pdf = pd.DataFrame({'x':[1.0,2.0,3.0,4.0,5.0],
'y':[1.0,2.0,3.0,4.0,5.0],
'z':[1.0,2.0,3.0,4.0,5.0],
'label':[1,2,1,2,1]},
index=[1, 2, 3, 4, 5])
df = dd.from_pandas(pdf, npartitions = 2)
object_id = 0
def cluster(df, object_id=object_id):
# create kdtree
tree = spatial.cKDTree(df[['x', 'y', 'z']])
# get neighbours within distance for every point, store …
Run Code Online (Sandbox Code Playgroud) 我没有找到关于其实现复杂性的声明.
通常,查找连通分量可以在线性时间内完成,例如通过广度优先搜索或深度优先搜索(参见维基百科文章).但是,假设您可以将图形保留在内存中.GraphX实现了一个分布式的核外算法,因此我认为它不具有可比性.
你知道他们的算法如何工作以及它有多复杂性?
algorithm time-complexity apache-spark connected-components spark-graphx
我有一个值为0或1的矩阵,我想获得一个相邻1的组列表.
例如,矩阵
mat = rbind(c(1,0,0,0,0),
c(1,0,0,1,0),
c(0,0,1,0,0),
c(0,0,0,0,0),
c(1,1,1,1,1))
> mat
[,1] [,2] [,3] [,4] [,5]
[1,] 1 0 0 0 0
[2,] 1 0 0 1 0
[3,] 0 0 1 0 0
[4,] 0 0 0 0 0
[5,] 1 1 1 1 1
Run Code Online (Sandbox Code Playgroud)
应返回以下4个连接组件:
C1 = {(1,1);(2,1)}
C2 = {(2,4)}
C3 = {(3,3)}
C4 = {(5,1);(5,2);(5,3);(5,4);(5,5)}
有没有人知道如何在R中快速做到这一点?我的真实矩阵确实相当大,如2000x2000(但我希望连接组件的数量相当小,即200).
我正在get_connected_components
为一个类写一个函数Graph
:
def get_connected_components(self):
path=[]
for i in self.graph.keys():
q=self.graph[i]
while q:
print(q)
v=q.pop(0)
if not v in path:
path=path+[v]
return path
Run Code Online (Sandbox Code Playgroud)
我的图是:
{0: [(0, 1), (0, 2), (0, 3)], 1: [], 2: [(2, 1)], 3: [(3, 4), (3, 5)], \
4: [(4, 3), (4, 5)], 5: [(5, 3), (5, 4), (5, 7)], 6: [(6, 8)], 7: [], \
8: [(8, 9)], 9: []}
Run Code Online (Sandbox Code Playgroud)
其中键是节点,值是边缘.我的函数给了我这个连接组件:
[(0, 1), (0, 2), (0, 3), (2, 1), (3, 4), (3, 5), …
Run Code Online (Sandbox Code Playgroud) 我一直在尝试使用二进制图像中的8个邻居来查找所有连接的组件,而不使用函数"bwlabel".
例如,我的输入矩阵是:
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
Run Code Online (Sandbox Code Playgroud)
我会有这样的事情:
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 …
Run Code Online (Sandbox Code Playgroud) cv2.connectedComponentsWithStats
与白底黑字相比,是否必须通过黑底白字的图像?做一个和另一个我得到不同的结果。
示例代码:
import os
import cv2
root = r'pth/to/img'
fl = r'img.png'
src = os.path.join( root, fl )
img = cv2.imread( src, 0 )
img_inv = cv2.bitwise_not( img )
cv2.imshow( 'Black-on-White', img )
cv2.waitKey(0)
cv2.imshow( 'White-on-Black', img_inv )
cv2.waitKey(0)
bw_nlbls, bw_lbls, bw_stats, _ = cv2.connectedComponentsWithStats( img )
wb_nlbls, wb_lbsl, wb_stats, _ = cv2.connectedComponentsWithStats( img_inv )
bw = 'Black-On-White'
wb = 'White-On-Black'
print( bw )
print( '-'*len(bw) )
print()
print('Number of Components: ', bw_nlbls)
print()
print( wb )
print( '-'*len(wb) …
Run Code Online (Sandbox Code Playgroud) opencv ×4
python ×4
algorithm ×1
apache-spark ×1
binary ×1
clique ×1
image ×1
kdtree ×1
matlab ×1
networkx ×1
pandas ×1
plpython ×1
postgresql ×1
python-2.7 ×1
r ×1
scipy ×1
spark-graphx ×1