Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在骨架化图像中找到连接点?_Python_Image Processing - Fatal编程技术网

Python 如何在骨架化图像中找到连接点?

Python 如何在骨架化图像中找到连接点?,python,image-processing,Python,Image Processing,我找到了一个用于查找骨架化图像的连接点的代码 代码没有找到所有的连接点,只有主连接点 如何调整此代码以查找骨架图像中的所有连接点 在我的示例图像中:我假设在骨架中至少找到6个连接点 我的代码: 在找到连接点之前,我准备好了图像:我转换成二进制,填充孔和骨架 import numpy as np from numpy import array import cv2 import pymorph as m import mahotas import scipy.ndimage.morphology

我找到了一个用于查找骨架化图像的连接点的代码

代码没有找到所有的连接点,只有主连接点

如何调整此代码以查找骨架图像中的所有连接点

在我的示例图像中:我假设在骨架中至少找到6个连接点

我的代码:

在找到连接点之前,我准备好了图像:我转换成二进制,填充孔和骨架

import numpy as np
from numpy import array
import cv2
import pymorph as m
import mahotas
import scipy.ndimage.morphology as mo


def skeletonize(img):
    h1 = np.array([[0, 0, 0],[0, 1, 0],[1, 1, 1]]) 
    m1 = np.array([[1, 1, 1],[0, 0, 0],[0, 0, 0]]) 
    h2 = np.array([[0, 0, 0],[1, 1, 0],[0, 1, 0]]) 
    m2 = np.array([[0, 1, 1],[0, 0, 1],[0, 0, 0]])    
    hit_list = [] 
    miss_list = []
    for k in range(4): 
        hit_list.append(np.rot90(h1, k))
        hit_list.append(np.rot90(h2, k))
        miss_list.append(np.rot90(m1, k))
        miss_list.append(np.rot90(m2, k))    
    img = img.copy()
    while True:
        last = img
        for hit, miss in zip(hit_list, miss_list): 
            hm = mo.binary_hit_or_miss(img, hit, miss) 
            img = np.logical_and(img, np.logical_not(hm)) 
        if np.all(img == last):  
            break
    return img

#input image
nomeimg = 'DUPLInuova/fionda 3/c (3).jpg'
img = cv2.imread(nomeimg)
gray = cv2.imread(nomeimg,0)
element = cv2.getStructuringElement(cv2.MORPH_CROSS,(6,6)) #con 4,4 si vede tutta la stella e riconosce piccoli oggetti
graydilate = cv2.erode(gray, element) #imgbnbin

ret,thresh = cv2.threshold(graydilate,127,255,cv2.THRESH_BINARY_INV) 
imgbnbin = thresh

cv2.imshow('binaria',imgbnbin)
cv2.waitKey()





#finding a unique contour
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
print(len(contours))

Areacontours = list()
calcarea = 0.0
unicocnt = 0.0
for i in range (0, len(contours)):
    area = cv2.contourArea(contours[i])
    if (area > 90 ):  #con 90 trova i segni e togli puntini
        if (calcarea<area):
            calcarea = area
            unicocnt = contours[i]

cnt = unicocnt            
print(len(cnt))
cv2.drawContours(thresh,contours,-1,(0,255,0),3)

#fill holes
des = imgbnbin
cv2.drawContours(des,[unicocnt],0,255,-1)

gray = cv2.bitwise_not(des)

cv2.imshow('gray tappabuchi grAY',gray)
cv2.waitKey()


kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
res = cv2.morphologyEx(gray,cv2.MORPH_OPEN,kernel)

cv2.imshow('res tappabuchi',res)
cv2.waitKey()

rest = cv2.bitwise_not(res)

print(rest)


cv2.imshow('rest tappabuchi 2',rest)
cv2.waitKey()

skel = skeletonize(rest)
skel = skel.astype(np.uint8)*255
print(skel)
cv2.imshow('skel',skel)
cv2.waitKey(0)

imgbnbin = m.binary(skel)

#FINDING junction and PRUNING
print("mohatas imgbnbin")
print(imgbnbin)

b2 = m.thin(imgbnbin)
b3 = m.thin(b2, m.endpoints('homotopic'), 15) # prune small branches, may need tuning

outputimage = m.overlay(imgbnbin, b3)
mahotas.imsave('outputs.png', outputimage)

# structuring elements to search for 3-connected pixels
seA1 = array([[False,  True, False],
       [False,  True, False],
       [ True, False,  True]], dtype=bool)

seB1 = array([[False, False, False],
       [ True, False,  True],
       [False,  True, False]], dtype=bool)

seA2 = array([[False,  True, False],
       [ True,  True,  True],
       [False, False, False]], dtype=bool)

seB2 = array([[ True, False,  True],
       [False, False, False],
       [False,  True, False]], dtype=bool)

# hit or miss templates from these SEs
hmt1 = m.se2hmt(seA1, seB1)
hmt2 = m.se2hmt(seA2, seB2)

# locate 3-connected regions
b4 = m.union(m.supcanon(b3, hmt1), m.supcanon(b3, hmt2))

# dilate to merge nearby hits
b5 = m.dilate(b4, m.sedisk(10))

# locate centroids
b6 = m.blob(m.label(b5), 'centroid')

outputimage = m.overlay(imgbnbin, m.dilate(b6,m.sedisk(5)))
mahotas.imsave('output.png', outputimage)
将numpy导入为np
从numpy导入数组
进口cv2
将pymorph作为m导入
进口麻花
将scipy.ndimage.MOTHORMATION导入为mo
def骨架化(img):
h1=np.数组([[0,0,0],[0,1,0],[1,1,1]]
m1=np.数组([[1,1,1],[0,0,0],[0,0,0]]
h2=np.数组([[0,0,0],[1,1,0],[0,1,0]]
m2=np.数组([[0,1,1],[0,0,1],[0,0,0]]
点击列表=[]
小姐名单=[]
对于范围(4)中的k:
点击列表追加(np.rot90(h1,k))
点击列表追加(np.rot90(h2,k))
miss_list.append(np.rot90(m1,k))
缺失列表追加(np.rot90(m2,k))
img=img.copy()
尽管如此:
last=img
对于hit,miss in zip(hit_列表,miss_列表):
hm=mo.binary\u命中或未命中(img,命中,未命中)
img=np.logical_和(img,np.logical_非(hm))
如果np.all(img==最后一个):
打破
返回img
#输入图像
nomeimg='Duplinova/fionda 3/c(3.jpg'
img=cv2.imread(nomeimg)
灰色=cv2.imread(nomeimg,0)
元素=cv2.getStructuringElement(cv2.MORPH_CROSS,(6,6))#con 4,4 si vede tutta la stella e riconosce piccoli oggetti
灰色扩张=cv2.侵蚀(灰色,元素)#imgbnbin
ret,thresh=cv2.阈值(灰度放大,127255,cv2.thresh\u二进制\u INV)
imgbnbin=阈值
cv2.imshow('binaria',imgbnbin)
cv2.waitKey()
#寻找独特的轮廓
等高线,层次=cv2.findContours(阈值,cv2.RETR\u树,cv2.CHAIN\u近似值\u简单)
打印(透镜(轮廓))
Area等高线=列表()
钙质=0.0
单碳纳米管=0.0
对于范围(0,len(等高线))内的i:
面积=cv2。轮廓面积(轮廓[i])
如果(面积>90):#控制90 trova i segni e togli puntini

如果(Calcrea根据我的理解,骨架中的连接点是一个像素,它有两个以上的黑色像素作为邻居(如果背景=白色,骨架=黑色)

因此,一个简单的解决方案是遍历黑色像素,并计算当前像素周围的黑色像素数。需要代码吗?可以修改用于骨架化的代码来完成此操作。

问题似乎相关。