如何在骨架上找到角点? [英] How to find corner points on skeleton?

查看:645
本文介绍了如何在骨架上找到角点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找迷宫图像中的最短路径。我使用 OpenCV Networkx 搜索算法进行了一些形态学操作。我的代码运行得非常成功。我的意思是代码可以检测端点和分支点但是我还需要检测骨架图像上的角点并将这些点指定为节点

I am working on finding shortest path in maze image. I used some morphologic operations using OpenCV and Networkx search algorithm. My code is running quite succesfully. I mean the code can detect end points and branch points but I need to also detect corner points on skeleton image and assign this points as node.

我的代码是

import numpy as np
import cv2
from matplotlib import pyplot as plt
from skimage.morphology import skeletonize
import sknw
import networkx as nx


#Open Maze image
img = cv2.imread("C:/Users/Dell/HandMadeMaze1.jpg")
kernel = np.ones((1,1),np.uint8)

#Convert to GrayScaledImage
grayscaled = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

#BınaryThreshold + OtsuThreshold + BinaryThreshold
retval, threshold = cv2.threshold(grayscaled, 10, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
retval, threshold2 = cv2.threshold(threshold, 10, 255, cv2.THRESH_BINARY_INV)
threshold2[threshold2 == 255] = 1

#Skeletonize the Thresholded Image
skel = skeletonize(threshold2)


#Build Graph from skeleton
graph = sknw.build_sknw(skel, multi=False)
G = nx.Graph(graph)
plt.imshow(img, cmap='gray')

#Draw Edges by 'pts'
for (s,e) in graph.edges():
    ps = graph[s][e]['pts']
    plt.plot(ps[:,1], ps[:,0], 'red')

#Draw Node by 'o'  
node, nodes = graph.node, graph.nodes()
ps = np.array([node[i]['o'] for i in nodes])
plt.plot(ps[:,1], ps[:,0], 'g.')
plt.title('Skeletonize')
plt.savefig('Overlay_Maze.jpg')
plt.axis('on')
plt.show()



#G = nx.path_graph(len(ps))
#G = nx.karate_club_graph()
pos = nx.spring_layout(G)
nx.draw(G,pos,node_color='black')

#Draw Path in red
path = nx.shortest_path(G,source=0,target=len(G)-1)
path_edges = zip(path,path[1:])
nx.draw_networkx_nodes(G,pos,nodelist=path,node_color='r')
nx.draw_networkx_edges(G,pos,edgelist=path_edges,edge_color='g',width=5)
plt.axis('equal')

#Show Output
plt.show()
print(ps)
print('Number of Element = ',len(ps))
print('Number of Step = ', nx.shortest_path_length(G,source=0,target=len(G)-1))
print('Path Edges = ', path_edges)
print('Shortest Path = ', nx.shortest_path(G,source=0,target=len(G)-1))
for C in nx.connected_component_subgraphs(G):
    print('Length of Path = ',nx.average_shortest_path_length(C))

cv2.waitKey(0)
cv2.destroyAllWindows()

当我运行上面的代码时,我得到以下输出。

When I run the code above, I get the following outputs.

现在我正在检测骨架图像上的角点并分配这些作为查找路径算法的节点成功运行。

除分支和终点外,我如何找到整个转角/转折点?

提前致谢。

推荐答案


  1. 分支点:

查找度> 2的节点( networkx.Graph.degree )。节点的程度是节点连接的邻居的数量。分支点有3个或更多邻居。

Find nodes with a degree > 2 (networkx.Graph.degree). The degree of a node is the number of neighbours the node is connected to. Branch points have 3 or more neighbours.


  1. 终点

查找度= 1的节点( networkx.Graph.degree )。端点只有一个邻居。

Find nodes with a degree = 1 (networkx.Graph.degree). End points only have one neighbour.


  1. 转折点:

在没有直线的节点上有一个转折点。
因此,对于不是分支或终点的每个节点,找到两个邻居( networkx.Graph.neighbors ,IIRC),确定(x,y)所有三个点的坐标 c =(x,y),cn1 =(nx1,ny1),cn2 =(nx2,ny2)并计算节点坐标和邻居坐标之间的差异。如果 cn1 - c == c - cn2 c 处的节点不是转折点。

You have a "turning point" at nodes that do not lie on a straight line. Hence for each node that is not a branch or an end point, find the two neighbours (networkx.Graph.neighbors, IIRC), determine the (x, y) coordinates of all three points c = (x, y), cn1 = (nx1, ny1), cn2 = (nx2, ny2) and compute the differences between the node coordinate and the coordinates of the neighbours. If cn1 - c == c - cn2, the node at c is not a turning point.

这篇关于如何在骨架上找到角点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆