-1

I trying to sort a tuple as below

input: ROI: 
[[191  60  23  18]
 [143  60  23  19]
 [ 95  52  24  21]
 [237  51  24  21]
 [ 47  38  27  22]
 [281  35  25  22]
 [  4  17  26  24]
 [324  13  22  21]]

Expected Output = S_ROI: 
[[4  17  26  24]
 [47  38  27  22]
 [ 95  52  24  21]
 [143  60  23  19]
 [ 191  60  23  18]
 [237  51  24  21]
 [281  35  25  22]
 [324  13  22  21]]

I have got intermediate array

column=[191 143 95 237 47 281 4 324]

I have tried this - But ROI is getting updated inside loop

sort_index = np.argsort(column) 
column.sort()

sorted_led_ROI=ROI;
index=0
    for y in sort_index:
        sorted_led_ROI[index]=ROI[y]
        index =index+1
    print('sorted_led_ROI:', sorted_led_ROI)

Result:

sorted_led_ROI: 
[[  4  17  26  24]
 [ 47  38  27  22]
 [ 95  52  24  21]
 [ 47  38  27  22]
 [  4  17  26  24]
 [ 47  38  27  22]
 [ 47  38  27  22]
 [324  13  22  21]]

help me out to sort this in python using np or cv

Yevhen Kuzmovych
  • 10,940
  • 7
  • 28
  • 48
  • `sorted_led_ROI=ROI` does NOT create a copy of the data - it simply makes the same data available under yet another name. You are overwriting the data in `sorted_led_ROI[index]=ROI[y]` leading to this mixed-up result. You want to have a seperate copied array sorted based on the original. – Patrick Artner Feb 10 '21 at 09:13
  • Thanks for the reply.. i agree i have done lot of duplication and mixed up everything.. print(ROI[ROI[:,0].argsort()]) this helped to solve – JAGADEESH R J Feb 10 '21 at 09:24

1 Answers1

1

Do you mean just this:

print(ROI[ROI[:,0].argsort()])

Output:

[[  4  17  26  24]
 [ 47  38  27  22]
 [ 95  52  24  21]
 [143  60  23  19]
 [191  60  23  18]
 [237  51  24  21]
 [281  35  25  22]
 [324  13  22  21]]
U13-Forward
  • 69,221
  • 14
  • 89
  • 114