Package: cythonflatiter
Locates values in NumPy Arrays with Cythonpip install cythonflatiterTested against Windows / Python 3.11 / AnacondaCython (and a C/C++ compiler) must be installedFlatIterArray is a utility class for efficiently searching multi-dimensional array data.|__init__(self,a,dtype=<class'numpy.int64'>, unordered=True)|InitializesaFlatIterArrayinstance.||Parameters:|-a(numpy.ndarray):Theinputarray.|-dtype(numpy.dtype,optional):Thedatatypeoftheindexarraythatwillbecreated.Defaultstonp.int64.(It's better not to change that, because it corresponds to cython.Py_ssize_t)|-unordered(bool,optional):Flagindicatingwhethertouseunorderediterations.DefaultstoTrue.(indexarraywillbecreatedusingmultiprocessing)||get_flat_pointer_array_from_orig_data(self)|Returnsaflatpointerarrayfromtheoriginaldata.|Ifyouchangedatahere,itchangesalsointheoriginalarray||Returns:|-numpy.ndarray:Flatpointerarray.||search_multiple_values_in_array(self,values)|Searchesformultiplevaluesinthearrayandreturnsindicesandvalues.||Parameters:|-values(listornumpy.ndarray):Listofvaluestosearchfor.||Returns:|-tuple:Arrayofindicesandarrayoffoundvalues.||search_single_value_in_array(self,value)|Searchesforasinglevalueinthearrayandreturnsindices.||Parameters:|-value:Thevaluetosearchfor.||Returns:|-numpy.ndarray:Arrayofindices.||sequence_is_in_dimension(self,seq,last_dim)|Checksifasequenceisinadimension||Parameters:|-seq(listornumpy.ndarray):Listofvaluesrepresentingthesequence.|-last_dim:Thedimensiontosearchin.||Returns:|-numpy.ndarray:Arrayofindices.||update_iterarray(self,dtype=<class'numpy.int64'>, unordered=True)|Updatestheiterrayattributewithnewparameters.||Parameters:|-dtype(numpy.dtype,optional):Thedatatypeoftheindexarraythatwillbecreated.Defaultstonp.int64.|-unordered(bool,optional):Flagindicatingwhethertouseunorderediterations.DefaultstoTrue.(indexarraywillbecreatedusingmultiprocessing)||value_is_in_dimension(self,value,last_dim)|Checksifavalueisinadimension||Parameters:|-value:Thevaluetosearchfor.|-last_dim:Thedimensiontosearchin.||Returns:|-numpy.ndarray:Arrayofindices.importnumpyasnpimportcv2fromcythonflatiterimportFlatIterArraydata=cv2.imread(r"C:\Users\hansc\Desktop\2023-08-29xx16_07_30-Window.png")f=FlatIterArray(data,dtype=np.int64,unordered=True)results255inarray=f.search_single_value_in_array(255)# results255inarray# Out[6]:# array([[195330,     34,      0,      0],#        [201075,     35,      0,      0],#        [206820,     36,      0,      0],#        ...,#        [488324,     84,   1914,      2],#        [494069,     85,   1914,      2],#        [499814,     86,   1914,      2]], dtype=int64)# print(data[84,1914,2])# print(data[34,0,0])# 255# 255indices,found_values=f.search_multiple_values_in_array(values=[255,11,0])# indices,found_values# Out[12]:# (array([[195330,     34,      0,      0],#         [201075,     35,      0,      0],#         [206820,     36,      0,      0],#         ...,#         [488324,     84,   1914,      2],#         [494069,     85,   1914,      2],#         [499814,     86,   1914,      2]], dtype=int64),#  array([255, 255, 255, ..., 255, 255, 255], dtype=uint8))concat_values=np.hstack([indices,found_values.reshape((-1,1))])# print(concat_values)# [[195330     34      0      0    255]#  [201075     35      0      0    255]#  [206820     36      0      0    255]#  ...#  [488324     84   1914      2    255]#  [494069     85   1914      2    255]#  [499814     86   1914      2    255]]# print(data[85,1914,2])# print(data[36,0,0])lastdimwith255=f.value_is_in_dimension(255,3)# lastdimwith255# Out[31]:# array([[195330,     34,      0],#        [201075,     35,      0],#        [206820,     36,      0],#        ...,#        [488322,     84,   1914],#        [494067,     85,   1914],#        [499812,     86,   1914]], dtype=int64)# print(data[86,1914])# print(data[34,0])# [255 255 255]# [255 255 255]penultimatedimwith255255=f.sequence_is_in_dimension([255,255,255],2)# penultimatedimwith255255# Out[38]:# array([[      0,       0],#        [   5745,       1],#        [  11490,       2],#        ...,#        [5538180,     964],#        [5543925,     965],#        [5549670,     966]], dtype=int64)# data[964]# Out[40]:# array([[255, 255, 255],#        [255, 255, 255],#        [255, 255, 255],#        ...,#        [ 43,  43,  43],#        [ 43,  43,  43],#        [ 43,  43,  43]], dtype=uint8)ultimatedimwith255255255=f.sequence_is_in_dimension([255,255,255],3)# ultimatedimwith255255255# Out[42]:# array([[195330,     34,      0],#        [201075,     35,      0],#        [206820,     36,      0],#        ...,#        [488322,     84,   1914],#        [494067,     85,   1914],#        [499812,     86,   1914]], dtype=int64)# [750 433   0]# [751 433   0]# [752 433   0]# [753 433   0]# [754 433   0]# [755 433   0]# [756 433   0]# [757 433   0]# [758 433   0]