0

I have 522 image arrays, each 87 by 87 pixels (arr.shape gives (522, 87, 87)). I want to find the maximum pixel of each image array and find the positions of where that is.

I figured doing the following:

centre = np.amax(subtracted_image, axis=(1,2))

where subtracted_image is the array with shape (522, 87, 87). Centre gives 522 values, so this is correct.

I want to know the location of centre for each image. How can I do this in a vectorized form (without for loops)?

Thanks in advance for any help/suggestions!

Edit: someone asked for an example.

This is a small piece of data the array:

[[[ 143.10010416   89.07006492   72.3224121  ...  102.30038117
    101.83050251   53.70256388]
  [  63.14898975   90.06379611  145.49647472 ...   95.98955971
    124.41020947   47.69529682]
  [  85.07709204  151.93995934   93.07749867 ...  107.79560335
     66.94020429   68.50914571]
  ...
  [ 112.54938612  124.40069381   76.66310822 ...  128.4671692
    135.70983661  148.73949865]
  [ 153.13556832  120.62338967  201.29726612 ...   81.20291718
    144.16804321  150.39085295]
  [ 142.46856954   63.87816379  106.85583428 ...  152.75065302
     22.06187136  189.30224044]]

 [[-248.55530078 -202.23631841 -252.93385385 ... -221.14094893
   -177.68373602 -194.88428689]
  [-246.91700985 -245.48314478 -230.05877006 ... -114.22985962
   -274.17192876 -248.72427823]
  [-255.5967223  -231.61400773 -258.5605146  ... -334.08997443
   -172.37263198 -277.13556604]
  ...

This is what print(centre) gives me:

[7894.85796851 7425.09011107 8010.76090547 7824.43785614 7690.78105527
 7582.09056691 7405.99799949 7643.07258175 7415.37243749 7879.47729479
 7403.25139847 7643.52545278 7444.67702851 7852.06528985 7996.03948599
 7880.97674207 7349.12390316 7164.35520703 7362.09659468 7150.45924123
 7420.52104232 7768.5170422  7960.62532522 7678.70140975 7798.45428569
 7597.84951612 7482.55289092 7387.59161616 7893.56705483 7601.03758863
 7787.73097268 7768.45019574 7700.76707531 8100.67670249 8111.04689785
 7845.76982674 7771.46619013 7572.78727451 7335.99480106 7667.00599757
 7647.6175962  7479.95359477 7802.843147   7637.26102979 7625.1680439
 7916.0219432  7489.98184031 7367.73847306 7523.45641321 8143.11254079
 7616.97493806 7699.07460167 7731.53959686 7117.57805671 7613.22257337
 7216.41459097 7996.05312463 8058.3746692  7291.38025517 8061.32669187
 7919.72930316 7795.21441075 7675.70043676 7423.03192387 7257.5800147
 7779.99363941 7580.68531321 7867.17047026 7654.68254612 7819.93729486
 7811.60234832 8153.06941196 7323.46889634 7546.41140483 7480.00123737
 7422.17889727 7485.07308686 7320.91935294 7747.79746355 7507.73044995
 7684.40819664 7217.06662692 7321.9260887  7629.79482277 7815.40933919
 7340.06640821 7670.27144323 7659.6589856  7442.65484672 7164.47563348
 7753.68512453 7858.54677517 7653.89624754 7665.91630397 7655.97884764
 7487.39677891 7152.98769971 7663.22163472 7114.76395902 7194.61980673
 7254.7880012  7103.26388065 7630.4384949  7450.79656852 7559.86854565
 7123.98150503 7828.7137076  7477.94225355 7548.0291233  7611.11817118
 7282.83728913 6975.15286762 7019.42464797 7112.80862409 7712.4895379
 7378.17813235 7487.22224696 7350.87373862 7551.79668236 7636.66145917
 7118.66622909 7268.57876731 7134.21778436 7910.96166024 7821.43643402
 7643.76299672 7192.86695383 7329.44593161 7441.76446155 7426.38997783
 7287.90511444 7499.5283171  7319.14310932 7166.2103344  6833.99482744
 7571.41701825 7438.72939421 7415.68190483 7476.47693031 7342.51276388
 7443.19021832 7512.71829611 7492.3252465  7144.73358867 7143.0106867
 6881.21498277 7465.46441967 7145.89261738 7609.06773664 7248.03161727
 7197.71244014 7312.10274306 7142.97945949 7131.94261679 7619.37632551
 7256.86114396 7186.87462049 6923.38132047 7417.33374177 7645.73098176
 7430.78093351 7114.29839958 6954.84071517 7025.02177436 7655.56772945
 7428.09194487 7100.66410574 6951.30735437 7286.23002872 7067.78608848
 7301.22779903 7224.98544263 7103.72779804 7185.10106533 6696.8832689
 7261.01797322 7107.63154442 7147.51564643 7243.85642749 7151.78972879
 7064.40326276 7482.00332915 6689.34714767 7503.93413592 7004.87660908
 7025.65996086 7079.140162   6945.92489219 6607.11729705 6885.62288028
 6924.65318198 7327.1652957  7284.44168375 7426.73077763 7554.54451053
 7351.37124149 7390.35899757 7495.77506661 7371.82857565 7369.26170502
 7081.93087447 7097.49352187 6892.63270385 7384.07146758 7489.85139123
 7241.6391444  7154.31172954 6630.72341612 7318.63368098 7177.40029053
 7362.0155895  6958.19406715 7080.47927429 6562.4233123  7355.60760453
 7218.98836391 7010.60604061 7313.75717653 7078.67001409 7235.57150738
 7236.4911855  7179.43935898 6773.15813137 6859.19669938 7187.42040536
 7778.17115062 7349.73334997 7060.66317016 7097.60623135 6786.25892897
 7271.0000178  7323.66463508 7002.26395104 6763.05248718 6777.0778576
 7487.59325404 7546.77449171 7043.26624763 6838.16707519 7636.5838768
 7486.16770318 7116.64584441 7459.97409249 6912.5544369  6960.3752517
 7068.32050549 7019.54129447 7333.23006512 7047.37935583 6735.84357124
 6729.81083605 7385.50318831 7441.24455884 6982.36139391 6635.59037138
 7158.92404512 6726.00710558 6990.89358581 7517.0363906  7029.70504983
 7021.45807067 7395.98546526 7731.68700563 7430.79689636 7205.75582957
 7345.07011637 7088.86978144 7216.86289878 6965.3550469  7261.1113675
 7152.19563294 7239.8613472  7057.72764163 6662.26990146 7072.75165203
 7238.84555409 7129.85710022 6655.02677643 6662.79600495 6886.41962988
 6872.74075866 7447.27085815 6967.83509492 7815.42327949 7358.11559671
 7071.89165995 6831.61979559 7021.45483434 7037.85922662 6868.16582867
 6966.52455493 6980.65037867 7302.80953606 7204.86017933 7285.55996307
 7058.93301969 7271.41787904 6983.91411477 7096.94200196 6984.0769252
 7109.51588211 7146.50233138 7143.28158905 7505.17808398 7531.5010614
 7284.3239355  7495.23300715 7839.04972106 7550.27314863 7231.35912726
 7072.7259103  6898.09213121 6887.47109331 7097.60295195 7475.01912287
 7209.24375456 7679.21510453 7569.02272354 7196.08046002 7610.93930228
 7498.75102689 7109.56839038 7027.54924394 6982.05744764 7147.86035316
 6800.65198571 7018.24289748 7249.96222185 7568.70264218 7282.69547786
 7490.83148102 7466.47419243 7184.89728663 6672.56849314 7453.7945462
 7315.52151725 7177.9243885  7349.85424435 7902.23732875 7547.05196317
 7305.16418591 7266.00186707 7309.15295017 7051.00662752 7141.02551501
 7051.19337905 6877.8196158  7123.36759533 7020.68086842 7341.94552544
 7251.2099518  7307.33019103 7924.99140617 7957.90997371 7531.89900715
 7253.22892788 7505.11840479 7219.23153083 6897.0059301  6942.00675894
 7666.79947164 7388.10900659 7563.11427431 7751.12921037 7610.46935924
 7668.6929885  7482.84068848 7523.54045298 7313.13902031 6747.14539387
 7180.53422289 7190.47234087 7641.68564061 7131.21552861 7416.25413727
 7226.24125293 7208.00882331 7581.83334991 7610.7877153  7428.52303742
 7017.06827158 6903.08162757 7160.23371731 7113.35345126 7083.3765027
 7382.35902976 7228.34958934 7726.67244011 7493.56718081 7190.31061763
 7215.74655626 6826.91174452 7386.78837334 7171.28864521 6958.31727546
 7647.81413998 7576.61774984 7855.18403054 7795.30646668 7491.37522891
 7322.64309186 7264.57053982 7078.4468389  7227.86379265 7016.8651598
 7192.29969372 7608.5519223  7411.9247559  7856.31484032 7454.94251145
 7276.77571707 7421.05067927 7216.1604203  6838.70369056 6955.03029047
 6994.14532487 7372.24402264 7419.43105214 6941.23188142 7278.07768082
 7636.49673335 7340.20892643 7122.32204954 6828.49821977 6903.39486761
 6983.4670751  7189.40025448 7204.91136214 7029.24312684 7226.63764058
 7505.54059882 7778.12163644 7628.42144847 7264.02421838 7383.92782021
 7526.19368241 7278.5237613  6917.61215723 6949.25383363 7208.16042861
 6960.69520775 7117.0155434  7235.84433861 7415.23997749 7371.05242913
 7225.80509741 7063.01719924 7161.32802751 6943.21081077 6932.62959785
 6853.72617936 7075.07009238 7349.08435381 7465.57125232 7137.1770792
 7269.39621744 7403.3287262  7002.85600017 7294.74297994 7305.59649594
 7341.50032991 7199.70753642 6821.80917396 7290.66195314 7363.48015133
 7610.18996169 7640.26266039 7370.66409668 7345.68214841 6967.62318358
 7231.1460617  7292.40440635 7295.67134126 7162.68750765 7318.71677181
 7229.7406079  7593.13153207 7643.53570053 7647.27861094 7463.94958896
 7378.69715475 7366.90206725 6724.19074752 7236.76588761 6873.32237764
 7137.67844591 6952.88482043 7064.89365637 7591.05231682 7562.26036182
 7448.2641532  7295.89405148 7043.43379149 7117.21805899 7061.2175695
 6817.26592699 7270.04118726 7177.54362113 7100.41695295 7588.69286845
 7573.9945463  7600.05664467 6934.00068539 7248.96855138 7157.63177333
 6546.4910111  7078.87754661 6622.8532518  7151.93590554 7181.90210889
 7361.30517503 7154.61359556 7424.64915621 7047.72583318 7168.3073814
 6994.06330256 6908.80983042 7324.77851002 7125.97243249 7512.56075782
 7450.85833002 7332.40268615]

As I said, these are the maximum pixel value for each image array. I want to know the x,y coordinates i.e. the position of each of the value in centre (which is the centre of the star). If you need any more explanation, don't hesitate to ask!

Andromeda
  • 11
  • 3
  • Can you provide a small example? – mozway Dec 06 '22 at 19:53
  • Possible duplicate of [`https://stackoverflow.com/questions/30589211`](https://stackoverflow.com/questions/30589211). I recommend reshape array to 2d and then call `np.unravel_index`. – mathfux Dec 06 '22 at 20:18

1 Answers1

0

Have a look at np.argmax. Unfortunately, it does not work with a tuple as its axis argument, but you can ravel the latter 2 dimensions, and then unravel the np.argmax outputs back to its original shape:

import numpy as np

arr = np.random.rand(522, 87, 87) # Your images here

indices = arr.reshape(arr.shape[0], -1).argmax(axis=1)
rows, cols = np.unravel_index(indices, arr.shape[1:])

Here, rows and cols will be numpy arrays of length 522 that locate the maximum values in each image. You can get the maxima as follows (those that you are currently getting using np.amax:

centre = arr[np.arange(arr.shape[0]), rows, cols]
Chrysophylaxs
  • 5,818
  • 3
  • 10
  • 21