有没有办法在2d numpy阵列中找到连接像素?

2024-09-30 22:25:16 发布

您现在位置:Python中文网/ 问答频道 /正文

以以下数组为例:

points=np.array([[1,2],[1,3],[1,4],[2,4],[3,4],[4,4],[5,4],[6,4],[1,5],[1,6],[1,8],[1,9],[1,10],[1,11],[1,13]])

我想找到邻居像素

我的代码如下:

def npixel(pixel):
    pixels= np.array([[pixel[0]-1, pixel[1]-1], [pixel[0]-1, pixel[1]], [pixel[0]-1, pixel[1]+1], [pixel[0], pixel[1]-1], 
            [pixel[0], pixel[1]+1], [pixel[0]+1, pixel[1]-1], [pixel[0]+1, pixel[1]], [pixel[0]+1, pixel[1]+1]])
    
    return pixels
points=np.array([[1,2],[1,3],[1,4],[2,4],[3,4],[4,4],[5,4],[6,4],[1,5],[1,6],[1,8],[1,9],[1,10],[1,11],[1,13]])
for x in points:
        if p in npixel(x):
            count+=1
            print('neigbour pixel',p)
        
            print("count",count)

期望输出:

pixel:[1,2]
neigbour-pixel:[1,3]
count:1
pixel:[1,3]
neigbour-pixel:[1,2],[1,4]
count:2
so on

Tags: 代码inreturndefcountnp像素数组
2条回答

试图复制您的desidered输出,但不确定它是否是您所寻找的结果:

import numpy as np



pointz=np.array([[1,2],[1,3],[1,4],[2,4],[3,4],[4,4],[5,4],[6,4],[1,5],[1,6],[1,8],[1,9],[1,10],[1,11],[1,13]])


print(pointz, len(pointz), pointz.dtype)

def pixelz(points):
    
    pointzz = np.zeros((points.shape[0],points.shape[1]+3), dtype = object)
    for i in range(len(points)):
    # for i in range((points.shape[0])): #  > same as above in numpy len is shape 0 dimension { I think ;'-)  }
        # print(i)
        cnt = 0
        if i > 0:
            n_m = points[i-1]
            cnt +=1
        else:
            n_m = np.nan
        if i < (len(points)-1):
            n_p = points[i+1]
            cnt +=1
        else:
            n_p = np.nan
        print(i ,'  ', points[i],' neigh : ', n_m , n_p, ' counts : ',cnt)
        add = np.array([points[i][0],points[i][1] , n_m  ,  n_p , cnt], dtype = object)
        pointzz[i,0:5] = add
        # lines below same as two lines above
        #pointzz[i,0] = (int(points[i][0]))
        #pointzz[i,1] = (int(points[i][1]))
        #pointzz[i,2] = (n_m)
        #pointzz[i,3] = (n_p)
        #pointzz[i,4] = cnt
    return pointzz

# pixelz(pointz)

a = pixelz(pointz)

print('\n',a,'\n', a.shape, a.size ,a.ndim, a.dtype)

我的输出:

印刷品

0    [1 2]  neigh :  nan [1 3]  counts :  1
1    [1 3]  neigh :  [1 2] [1 4]  counts :  2
2    [1 4]  neigh :  [1 3] [2 4]  counts :  2
3    [2 4]  neigh :  [1 4] [3 4]  counts :  2
4    [3 4]  neigh :  [2 4] [4 4]  counts :  2
5    [4 4]  neigh :  [3 4] [5 4]  counts :  2
6    [5 4]  neigh :  [4 4] [6 4]  counts :  2
7    [6 4]  neigh :  [5 4] [1 5]  counts :  2
8    [1 5]  neigh :  [6 4] [1 6]  counts :  2
9    [1 6]  neigh :  [1 5] [1 8]  counts :  2
10    [1 8]  neigh :  [1 6] [1 9]  counts :  2
11    [1 9]  neigh :  [1 8] [ 1 10]  counts :  2
12    [ 1 10]  neigh :  [1 9] [ 1 11]  counts :  2
13    [ 1 11]  neigh :  [ 1 10] [ 1 13]  counts :  2
14    [ 1 13]  neigh :  [ 1 11] nan  counts :  1

和返回的数组:

[[1 2 nan array([1, 3]) 1]
 [1 3 array([1, 2]) array([1, 4]) 2]
 [1 4 array([1, 3]) array([2, 4]) 2]
 [2 4 array([1, 4]) array([3, 4]) 2]
 [3 4 array([2, 4]) array([4, 4]) 2]
 [4 4 array([3, 4]) array([5, 4]) 2]
 [5 4 array([4, 4]) array([6, 4]) 2]
 [6 4 array([5, 4]) array([1, 5]) 2]
 [1 5 array([6, 4]) array([1, 6]) 2]
 [1 6 array([1, 5]) array([1, 8]) 2]
 [1 8 array([1, 6]) array([1, 9]) 2]
 [1 9 array([1, 8]) array([ 1, 10]) 2]
 [1 10 array([1, 9]) array([ 1, 11]) 2]
 [1 11 array([ 1, 10]) array([ 1, 13]) 2]
 [1 13 array([ 1, 11]) nan 1]] 
 (15, 5) 75 2 object

这是我的方法。为了便于操作,我将numpy数组更改为list

points=np.array([[1,2],[1,3],[1,4],[2,4],[3,4],[4,4],[5,4], [6,4],[1,5],[1,6],[1,8],[1,9],[1,10],[1,11],[1,13]])
points=[l.tolist() for l in points]
temp=points
templist=list(points)
templist=list(temp)
end_pixel=[]
junction_pixel=[]
segment_pixel=[]
for pixel in points:
    npixel=((pixel[0]-1, pixel[1]-1), (pixel[0]-1, pixel[1]), 
    (pixel[0]-1, pixel[1]+1), (pixel[0], pixel[1]-1), (pixel[0], pixel[1]+1), (pixel[0]+1, pixel[1]-1), 
    (pixel[0]+1, pixel[1]), (pixel[0]+1, pixel[1]+1))

    count=0
    neighbour=[]
    for x in temp:
        for n in npixel:

            if(n[0]==x[0]and n[1]==x[1]):

                count+=1
                neighbour.append(n)


    if count==1:
        end_pixel.append(pixel)
        print('end_pixel:',pixel,'neigbour pixel:',neighbour,'count:',count)
    elif count==2:
        segment_pixel.append(pixel)
        print('segment_pixel:',pixel,'neigbour pixel:',neighbour,'count:',count)
    elif count>2:
        junction_pixel.append(pixel)
        print('junction_pixel:',pixel,'neigbour pixel:',neighbour,'count:',count)

输出:

end_pixel: [1, 2] neigbour pixel: [(1, 3)] count: 1
junction_pixel: [1, 3] neigbour pixel: [(1, 2), (1, 4), (2, 4)] count: 3
junction_pixel: [1, 4] neigbour pixel: [(1, 3), (2, 4), (1, 5)] count: 3
junction_pixel: [2, 4] neigbour pixel: [(1, 3), (1, 4), (3, 4), (1, 5)] count: 4
segment_pixel: [3, 4] neigbour pixel: [(2, 4), (4, 4)] count: 2
segment_pixel: [4, 4] neigbour pixel: [(3, 4), (5, 4)] count: 2
segment_pixel: [5, 4] neigbour pixel: [(4, 4), (6, 4)] count: 2
end_pixel: [6, 4] neigbour pixel: [(5, 4)] count: 1
junction_pixel: [1, 5] neigbour pixel: [(1, 4), (2, 4), (1, 6)] count: 3
end_pixel: [1, 6] neigbour pixel: [(1, 5)] count: 1
end_pixel: [1, 8] neigbour pixel: [(1, 9)] count: 1
segment_pixel: [1, 9] neigbour pixel: [(1, 8), (1, 10)] count: 2
segment_pixel: [1, 10] neigbour pixel: [(1, 9), (1, 11)] count: 2
end_pixel: [1, 11] neigbour pixel: [(1, 10)] count: 1

相关问题 更多 >