I have a list named 'partition'
[array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]), array([214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226,
227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250]), array([251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263,
264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276]), array([277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302,
303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314]), array([30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,
47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
64, 65, 66, 67, 68, 69, 70]), array([ 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96,
97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121]), array([122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134,
135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147,
148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160,
161, 162, 163, 164]), array([165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190,
191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203,
204, 205, 206, 207, 208, 209, 210, 211, 212, 213])]
And i try to extract the index of an element (1 for example) with this code:
a= partition.index(1)
But i get this error :
ValueError Traceback (most recent call last)
/tmp/ipykernel_582922/1287398992.py in <module>
----> 1 a= partition.index(1)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Can you help me please to fix this error?
Kind regards
This should solve the problem:
import numpy as np
from numpy import array
partition = array([array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]), array([214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226,
227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250]), array([251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263,
264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276]), array([277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302,
303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314]), array([30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,
47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
64, 65, 66, 67, 68, 69, 70]), array([ 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96,
97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121]), array([122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134,
135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147,
148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160,
161, 162, 163, 164]), array([165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190,
191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203,
204, 205, 206, 207, 208, 209, 210, 211, 212, 213])], dtype="object")
result = [(i, np.where(j == 1)[0]) for i, j in enumerate(partition) if np.any(np.where(j == 1))]
print(result)
The result will be in the format of (This is an example):
[(*the index of the array*, *an array with all indexes of the elements that are 1*), (*the index of the array*, *an array with all indexes of the elements that are 1*)]
I'm not sure if this is the best way to do it though.
Or if you have a list of numpy arrays you could do something like this:
from numpy import array
partition = [array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]), array([214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226,
227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250]), array([251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263,
264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276]), array([277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302,
303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314]), array([30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,
47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
64, 65, 66, 67, 68, 69, 70]), array([ 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96,
97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121]), array([122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134,
135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147,
148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160,
161, 162, 163, 164]), array([165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190,
191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203,
204, 205, 206, 207, 208, 209, 210, 211, 212, 213])]
result = []
for i, j in enumerate(partition):
for l in j:
if l == 1:
result.append((i, l))
print(result)
I've got an image which to the human eye clearly contains several (2 here) overlapping 'laces'.
This stems from a pretty lengthy image analysis of an experiment.
The red dots are all the points contained in two vectors, px and py, one with the x_position of the red pixels, the other one with the y positions.
px= array([ 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3,
3, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5,
5, 5, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7,
7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9,
9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10,
11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12,
13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14,
15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16,
16, 16, 16, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18,
18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20,
20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, 22, 22,
22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25,
25, 25, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27,
27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29,
29, 29, 29, 29, 30, 30, 30, 30, 30, 30, 30, 30, 31,
31, 31, 31, 31, 31, 31, 31, 32, 32, 32, 32, 32, 32,
32, 33, 33, 33, 33, 33, 33, 33, 34, 34, 34, 34, 34,
34, 34, 35, 35, 35, 35, 35, 35, 35, 36, 36, 36, 36,
36, 36, 37, 37, 37, 37, 37, 37, 37, 38, 38, 38, 38,
38, 38, 38, 39, 39, 39, 39, 39, 39, 39, 40, 40, 40,
40, 40, 40, 40, 41, 41, 41, 41, 41, 41, 41, 41, 42,
42, 42, 42, 42, 42, 42, 43, 43, 43, 43, 43, 43, 43,
44, 44, 44, 44, 44, 44, 44, 44, 45, 45, 45, 45, 45,
45, 45, 45, 45, 45, 45, 46, 46, 46, 46, 46, 46, 46,
46, 46, 46, 46, 46, 46, 46, 46, 47, 47, 47, 47, 47,
47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 48, 48,
48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
48, 48, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49,
49, 49, 49, 49, 49, 49, 49, 50, 50, 50, 50, 50, 50,
50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
50, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51,
51, 51, 51, 51, 51, 51, 51, 52, 52, 52, 52, 52, 52,
52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52,
52, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53,
53, 53, 53, 53, 53, 53, 53, 53, 54, 54, 54, 54, 54,
54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54,
54, 54, 54, 54, 55, 55, 55, 55, 55, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
55, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57,
57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 58, 58, 58,
58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58,
58, 58, 58, 58, 58, 58, 59, 59, 59, 59, 59, 59, 59,
59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59,
59, 59, 59, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60,
60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60,
61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61,
61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 62, 62, 62,
62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
62, 62, 62, 62, 62, 62, 62, 63, 63, 63, 63, 63, 63,
63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
63, 63, 63, 63, 63, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65,
65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 66, 66,
66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66,
66, 66, 66, 66, 66, 66, 66, 66, 67, 67, 67, 67, 67,
67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67,
67, 67, 67, 67, 67, 67, 68, 68, 68, 68, 68, 68, 68,
68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68,
68, 68, 68, 68, 68, 69, 69, 69, 69, 69, 69, 69, 69,
69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69,
69, 69, 69, 69, 70, 70, 70, 70, 70, 70, 70, 70, 70,
70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70,
71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71,
71, 71, 71, 71, 71, 72, 72, 72, 72, 72, 72, 72, 72,
72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 73, 73, 73,
73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
73, 73, 73, 73, 74, 74, 74, 74, 74, 74, 74, 74, 74,
74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 75,
75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75,
75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 76, 76, 76,
76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76,
76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76,
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
77, 77, 77, 77, 77, 77, 77, 77, 78, 78, 78, 78, 78,
78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78,
78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78,
78, 78, 78, 78, 79, 79, 79, 79, 79, 79, 79, 79, 79,
79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79,
79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79,
79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79,
79, 79, 79, 79, 80, 80, 80, 80, 80, 80, 80, 80, 80,
80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
80, 80, 80, 80, 80, 80, 81, 81, 81, 81, 81, 81, 81,
81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,
81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,
81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,
81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,
81, 81, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82,
82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82,
82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82,
82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82,
82, 82, 82, 82, 82, 82, 82, 82, 83, 83, 83, 83, 83,
83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83,
83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83,
83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83,
83, 83, 83, 83, 83, 83, 83, 83, 83, 84, 84, 84, 84,
84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84,
84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84,
84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84,
84, 84, 84, 84, 85, 85, 85, 85, 85, 85, 85, 85, 85,
85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85,
85, 85, 85, 85, 86, 86, 86, 86, 86, 86, 86, 86, 86,
86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 87, 87,
87, 87, 87, 87, 88, 88, 88, 88, 88, 88, 89, 89, 89,
89, 89, 89, 89, 90, 90, 90, 90, 90, 90, 91, 91, 91,
91, 91, 91, 91, 92, 92, 92, 92, 92, 92, 92, 93, 93,
93, 93, 93, 93, 93, 94, 94, 94, 94, 94, 94, 95, 95,
95, 95, 95, 95, 96, 96, 96, 96, 96, 96, 97, 97, 97,
97, 97, 97, 97, 98, 98, 98, 98, 98, 98, 99, 99, 99,
99, 99, 99, 100, 100, 100, 100, 100, 100, 101, 101, 101, 101,
101, 101, 102, 102, 102, 102, 102, 102, 103, 103, 103, 103, 103,
103, 104, 104, 104, 104, 104, 104, 105, 105, 105, 105, 105, 105,
106, 106, 106, 106, 106, 106, 106, 107, 107, 107, 107, 107, 107,
108, 108, 108, 108, 108, 108, 108, 109, 109, 109, 109, 109, 109,
110, 110, 110, 110, 110, 110, 110, 111, 111, 111, 111, 111, 111,
112, 112, 112, 112, 112, 112, 113, 113, 113, 113, 113, 113, 114,
114, 114, 114, 114, 114, 115, 115, 115, 115, 115, 115, 116, 116,
116, 116, 116, 116, 117, 117, 117, 117, 117, 117, 117, 118, 118,
118, 118, 118, 118, 119, 119, 119, 119, 119, 119, 119, 120, 120,
120, 120, 120, 120, 121, 121, 121, 121, 121, 121, 121, 122, 122,
122, 122, 122, 122, 123, 123, 123, 123, 123, 123, 123, 124, 124,
124, 124, 124, 124, 125, 125, 125, 125, 125, 125, 125, 126, 126,
126, 126, 126, 126, 127, 127, 127, 127, 127, 127, 128, 128, 128,
128, 128, 128, 129, 129, 129, 129, 129, 129, 129, 130, 130, 130,
130, 130, 130, 131, 131, 131, 131, 131, 131, 131, 132, 132, 132,
132, 132, 132, 133, 133, 133, 133, 133, 133, 133, 134, 134, 134,
134, 134, 134, 135, 135, 135, 135, 135, 135, 135, 136, 136, 136,
136, 136, 136, 137, 137, 137, 137, 137, 137, 138, 138, 138, 138,
138, 138, 139, 139, 139, 139, 139, 139, 140, 140, 140, 140, 140,
140, 141, 141, 141, 141, 141, 141, 141, 142, 142, 142, 142, 142,
142, 143, 143, 143, 143, 143, 143, 143, 144, 144, 144, 144, 144,
144, 145, 145, 145, 145, 145, 145, 145, 146, 146, 146, 146, 146,
146, 147, 147, 147, 147, 147, 147, 148, 148, 148, 148, 148, 148,
149, 149, 149, 149, 149, 149, 150, 150, 150, 150, 150, 150, 150,
151, 151, 151, 151, 151, 151, 152, 152, 152, 152, 152, 152, 153,
153, 153, 153, 153, 153, 153, 154, 154, 154, 154, 154, 154, 155,
155, 155, 155, 155, 155, 155, 156, 156, 156, 156, 156, 156, 157,
157, 157, 157, 157, 157, 158, 158, 158, 158, 158, 158, 158, 159,
159, 159, 159, 159, 159, 159, 160, 160, 160, 160, 160, 160, 160,
161, 161, 161, 161, 161, 161, 162, 162, 162, 162, 162, 162, 162,
163, 163, 163, 163, 163, 163, 163, 164, 164, 164, 164, 164, 164,
164, 165, 165, 165, 165, 165, 165, 165, 165, 166, 166, 166, 166,
166, 166, 166, 166, 167, 167, 167, 167, 167, 167, 167, 168, 168,
168, 168, 168, 168, 168, 169, 169, 169, 169, 169, 169, 169, 170,
170, 170, 170, 170, 170, 170, 171, 171, 171, 171, 171, 171, 171,
172, 172, 172, 172, 172, 172, 172, 173, 173, 173, 173])
and
py=array([189, 190, 191, 192, 193, 194, 189, 190, 191, 192, 193, 194, 195,
190, 191, 192, 193, 194, 195, 196, 190, 191, 192, 193, 194, 195,
196, 191, 192, 193, 194, 195, 196, 197, 192, 193, 194, 195, 196,
197, 198, 193, 194, 195, 196, 197, 198, 199, 194, 195, 196, 197,
198, 199, 200, 194, 195, 196, 197, 198, 199, 200, 201, 195, 196,
197, 198, 199, 200, 201, 202, 196, 197, 198, 199, 200, 201, 202,
197, 198, 199, 200, 201, 202, 203, 198, 199, 200, 201, 202, 203,
199, 200, 201, 202, 203, 204, 199, 200, 201, 202, 203, 204, 205,
199, 200, 201, 202, 203, 204, 205, 206, 200, 201, 202, 203, 204,
205, 206, 207, 202, 203, 204, 205, 206, 207, 202, 203, 204, 205,
206, 207, 208, 203, 204, 205, 206, 207, 208, 209, 204, 205, 206,
207, 208, 209, 210, 205, 206, 207, 208, 209, 210, 211, 206, 207,
208, 209, 210, 211, 212, 206, 207, 208, 209, 210, 211, 212, 213,
207, 208, 209, 210, 211, 212, 213, 214, 208, 209, 210, 211, 212,
213, 214, 209, 210, 211, 212, 213, 214, 215, 210, 211, 212, 213,
214, 215, 216, 211, 212, 213, 214, 215, 216, 217, 211, 212, 213,
214, 215, 216, 217, 212, 213, 214, 215, 216, 217, 218, 219, 213,
214, 215, 216, 217, 218, 219, 220, 214, 215, 216, 217, 218, 219,
220, 215, 216, 217, 218, 219, 220, 221, 216, 217, 218, 219, 220,
221, 222, 217, 218, 219, 220, 221, 222, 223, 218, 219, 220, 221,
222, 223, 218, 219, 220, 221, 222, 223, 224, 219, 220, 221, 222,
223, 224, 225, 220, 221, 222, 223, 224, 225, 226, 221, 222, 223,
224, 225, 226, 227, 221, 222, 223, 224, 225, 226, 227, 228, 222,
223, 224, 225, 226, 227, 228, 223, 224, 225, 226, 227, 228, 229,
224, 225, 226, 227, 228, 229, 230, 311, 225, 226, 227, 228, 229,
230, 309, 310, 311, 312, 313, 225, 226, 227, 228, 229, 230, 231,
307, 308, 309, 310, 311, 312, 313, 314, 226, 227, 228, 229, 230,
231, 232, 306, 307, 308, 309, 310, 311, 312, 313, 314, 227, 228,
229, 230, 231, 232, 304, 305, 306, 307, 308, 309, 310, 311, 312,
313, 314, 228, 229, 230, 231, 232, 233, 302, 303, 304, 305, 306,
307, 308, 309, 310, 311, 312, 313, 228, 229, 230, 231, 232, 233,
234, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311,
312, 229, 230, 231, 232, 233, 234, 298, 299, 300, 301, 302, 303,
304, 305, 306, 307, 308, 309, 310, 230, 231, 232, 233, 234, 235,
296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308,
309, 230, 231, 232, 233, 234, 235, 236, 294, 295, 296, 297, 298,
299, 300, 301, 302, 303, 304, 305, 306, 231, 232, 233, 234, 235,
236, 237, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301,
302, 303, 304, 305, 232, 233, 234, 235, 236, 237, 238, 239, 288,
289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301,
302, 233, 234, 235, 236, 237, 238, 239, 240, 285, 286, 287, 288,
289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301,
234, 235, 236, 237, 238, 239, 240, 283, 284, 285, 286, 287, 288,
289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 235, 236, 237,
238, 239, 240, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290,
291, 292, 293, 294, 295, 296, 235, 236, 237, 238, 239, 240, 241,
278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290,
291, 292, 293, 236, 237, 238, 239, 240, 241, 242, 276, 277, 278,
279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291,
237, 238, 239, 240, 241, 242, 243, 273, 274, 275, 276, 277, 278,
279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 238, 239, 240,
241, 242, 243, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278,
279, 280, 281, 282, 283, 284, 285, 238, 239, 240, 241, 242, 243,
244, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278,
279, 280, 281, 282, 283, 239, 240, 241, 242, 243, 244, 265, 266,
267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279,
280, 240, 241, 242, 243, 244, 245, 261, 262, 263, 264, 265, 266,
267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 240, 241,
242, 243, 244, 245, 258, 259, 260, 261, 262, 263, 264, 265, 266,
267, 268, 269, 270, 271, 272, 273, 274, 241, 242, 243, 244, 245,
246, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266,
267, 268, 269, 270, 271, 272, 241, 242, 243, 244, 245, 246, 247,
252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264,
265, 266, 267, 268, 269, 242, 243, 244, 245, 246, 247, 249, 250,
251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263,
264, 265, 266, 267, 242, 243, 244, 245, 246, 247, 248, 249, 250,
251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263,
243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255,
256, 257, 258, 259, 260, 240, 241, 242, 243, 244, 245, 246, 247,
248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 235, 236, 237,
238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250,
251, 252, 253, 254, 231, 232, 233, 234, 235, 236, 237, 238, 239,
240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 227,
228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240,
241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 223, 224, 225,
226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238,
239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251,
215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227,
228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240,
241, 242, 246, 247, 248, 249, 250, 251, 210, 211, 212, 213, 214,
215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227,
228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 246, 247,
248, 249, 250, 251, 189, 190, 191, 192, 193, 194, 195, 196, 197,
198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210,
211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 247, 248,
249, 250, 251, 252, 183, 184, 185, 186, 187, 188, 189, 190, 191,
192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204,
205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217,
218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230,
247, 248, 249, 250, 251, 252, 172, 173, 174, 175, 176, 177, 178,
179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204,
205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217,
218, 219, 220, 221, 222, 223, 224, 225, 226, 248, 249, 250, 251,
252, 253, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178,
179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204,
205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217,
218, 219, 249, 250, 251, 252, 253, 254, 167, 168, 169, 170, 171,
172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184,
185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197,
198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210,
211, 212, 213, 249, 250, 251, 252, 253, 254, 167, 168, 169, 170,
171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183,
184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196,
197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 250, 251,
252, 253, 254, 255, 167, 168, 169, 170, 171, 172, 173, 174, 175,
176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 250, 251,
252, 253, 254, 255, 168, 169, 170, 171, 172, 173, 174, 175, 176,
177, 178, 179, 180, 250, 251, 252, 253, 254, 255, 256, 251, 252,
253, 254, 255, 256, 252, 253, 254, 255, 256, 257, 252, 253, 254,
255, 256, 257, 258, 253, 254, 255, 256, 257, 258, 253, 254, 255,
256, 257, 258, 259, 254, 255, 256, 257, 258, 259, 260, 254, 255,
256, 257, 258, 259, 260, 255, 256, 257, 258, 259, 260, 256, 257,
258, 259, 260, 261, 256, 257, 258, 259, 260, 261, 256, 257, 258,
259, 260, 261, 262, 257, 258, 259, 260, 261, 262, 258, 259, 260,
261, 262, 263, 258, 259, 260, 261, 262, 263, 259, 260, 261, 262,
263, 264, 259, 260, 261, 262, 263, 264, 260, 261, 262, 263, 264,
265, 260, 261, 262, 263, 264, 265, 261, 262, 263, 264, 265, 266,
261, 262, 263, 264, 265, 266, 267, 262, 263, 264, 265, 266, 267,
262, 263, 264, 265, 266, 267, 268, 263, 264, 265, 266, 267, 268,
263, 264, 265, 266, 267, 268, 269, 264, 265, 266, 267, 268, 269,
265, 266, 267, 268, 269, 270, 265, 266, 267, 268, 269, 270, 266,
267, 268, 269, 270, 271, 266, 267, 268, 269, 270, 271, 267, 268,
269, 270, 271, 272, 267, 268, 269, 270, 271, 272, 273, 268, 269,
270, 271, 272, 273, 268, 269, 270, 271, 272, 273, 274, 269, 270,
271, 272, 273, 274, 269, 270, 271, 272, 273, 274, 275, 270, 271,
272, 273, 274, 275, 270, 271, 272, 273, 274, 275, 276, 271, 272,
273, 274, 275, 276, 271, 272, 273, 274, 275, 276, 277, 272, 273,
274, 275, 276, 277, 273, 274, 275, 276, 277, 278, 273, 274, 275,
276, 277, 278, 273, 274, 275, 276, 277, 278, 279, 274, 275, 276,
277, 278, 279, 274, 275, 276, 277, 278, 279, 280, 275, 276, 277,
278, 279, 280, 275, 276, 277, 278, 279, 280, 281, 276, 277, 278,
279, 280, 281, 276, 277, 278, 279, 280, 281, 282, 277, 278, 279,
280, 281, 282, 278, 279, 280, 281, 282, 283, 278, 279, 280, 281,
282, 283, 279, 280, 281, 282, 283, 284, 279, 280, 281, 282, 283,
284, 279, 280, 281, 282, 283, 284, 285, 280, 281, 282, 283, 284,
285, 280, 281, 282, 283, 284, 285, 286, 281, 282, 283, 284, 285,
286, 281, 282, 283, 284, 285, 286, 287, 282, 283, 284, 285, 286,
287, 283, 284, 285, 286, 287, 288, 283, 284, 285, 286, 287, 288,
284, 285, 286, 287, 288, 289, 284, 285, 286, 287, 288, 289, 290,
285, 286, 287, 288, 289, 290, 286, 287, 288, 289, 290, 291, 286,
287, 288, 289, 290, 291, 292, 287, 288, 289, 290, 291, 292, 287,
288, 289, 290, 291, 292, 293, 288, 289, 290, 291, 292, 293, 289,
290, 291, 292, 293, 294, 289, 290, 291, 292, 293, 294, 295, 290,
291, 292, 293, 294, 295, 296, 291, 292, 293, 294, 295, 296, 297,
292, 293, 294, 295, 296, 297, 292, 293, 294, 295, 296, 297, 298,
293, 294, 295, 296, 297, 298, 299, 294, 295, 296, 297, 298, 299,
300, 294, 295, 296, 297, 298, 299, 300, 301, 295, 296, 297, 298,
299, 300, 301, 302, 296, 297, 298, 299, 300, 301, 302, 297, 298,
299, 300, 301, 302, 303, 298, 299, 300, 301, 302, 303, 304, 299,
300, 301, 302, 303, 304, 305, 300, 301, 302, 303, 304, 305, 306,
301, 302, 303, 304, 305, 306, 307, 302, 303, 304, 305])
To reproduce the image, this MWE should be enough, once you copy-paste the arrays as px and py into your editor
import numpy as np
plot_mtx = np.zeros( (5120,5120) )
px=...
py=...
window_size = max(px[-1]-px[0],[py[-1]-py[0]])
imshow(plot_mtx[min(px):min(px)+window_size,min(py):min(py)+window_size])
Essentially, these are two lines that each have a width of about 6 pixels. I would like to separate them into two distinct lines, so that I would have px_1,py_1 for the one running from top to down, and px_2,py_2 for the one running from left to right.
I've failed so far to find the right keywords to find such algorithms. What I would have in mind is a for loop, that starts at one extremity, to complete a list with pixel positions in such a way, that the direction in which the loop is progressing never exceeds a certain angle with respect to several previous steps. This way, the algorithm would always take the set of pixels that cross smoothly through the intersection. Then it's just a matter of removing the new found line from the data and starting again.
Would you have any better input ?
Any feedback appreciated
Skeletonize the image.
Split by the crossing point(it has more than 2 neighbours) - that gives you 4 segments.
Join similarly directed pairs together - you get 2 skeleton lines.
Assign original pixels to each of those lines by proximity threshold.