Python Forum
Optimisation problem - star identification
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Optimisation problem - star identification
#1
Hi everyone

I have written a comet finding program however I find it runs a bit slow for my liking. I have been able to speed it up by using numba (compiler) but I probably can optimise the program further with some better coding. Your help in speeding these loops up would be appreciated.

Here is my code

def points(file):

# routine to find stars in a 1024 x 1024 pixel image stored as an array in imgarray (an array) 

    pnts=[]

# get file and read into array

    fileext="/archive/" + date + "/" + file 
    imgarray = ndimage.imread(fileext)
    

#Loop through all pixels excluding some defined areas I don't want to search
       
    for j in range(18,512):
        for i in range(18,512):
            
            if 345<j<512 and 363<i<512:
                continue
            
            if 380<j<512 and 320<i<363:
                continue
            if 300<j<346 and 400<i<512:
                continue

            if (i+j)<100:
                continue
            
# img[x,y,0] is the value of the pixel 
# sum 8 pixels which are 2 pixels from my target pixel and then take an average of that figure which is stored in a
           
            a=((int(imgarray[(i-2),(j-2),0])+int(imgarray[(i-2),(j+2),0])+int(imgarray[(i-2),j,0])+int(imgarray[(i+2),(j-2),0])+int(imgarray[(i+2),(j+2),0])+int(imgarray[(i+2),j,0])+int(imgarray[i,(j+2),0])+int(imgarray[i,(j-2),0]))/8)


# if the pixel location is below a certain level of brightness we don't want it so continue on to next pixel 
                
            if imgarray[i,j,0]<160:
                continue

# if the brightness of the selected pixel is more than 30 greater than the average stored in a stored that pixel's location in t and then append to pnts which is a list of points which I think are stars in the image.            
            
            if ((imgarray[i,j,0]-a)) >30:
                t=[j,i]
                pnts.append(t)
        
    return(pnts)    


Can anyone suggest a better way to write this code so that it runs faster. I had a look at numpy but couldn't find anything that would work for my code.

cheers Peter
Reply
#2
Did you look at scikit-image package? Your problem could be likely presented in vectorized form, e.g. You can apply averaging to entire image, e.g.

from skimage.filters.rank import mean
import numpy as np
aux_result = mean(your_image, np.array([[1,1,1],[1,0,1],[1,1,1]])) # intermediate result; chose appropriate mask;
Further, you can restore values for certain pixels, e.g.
aux_result[your_image > threshold] = your_image[your_image > threshold]
You need to restructure this code for your needs, though.

Also, you can significantly increase calculation speed, if you rewrite loops in Cython.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Turtle Star Fill Color Yellow-White Interchanging Color Effect codelab 9 904 Oct-25-2023, 09:09 AM
Last Post: codelab
  fpdf star character issue KatMac 3 2,919 May-01-2021, 06:22 PM
Last Post: KatMac
  How to scrape and count star rating using Selenium and Python? celinafregoso99 1 3,273 Feb-15-2021, 02:45 PM
Last Post: kashcode
  Help Getting Started - Optimisation Problem lummers 1 1,701 Sep-19-2020, 05:05 AM
Last Post: perfringo
  List Identification Tbot1000 2 1,691 Aug-28-2020, 12:08 PM
Last Post: Tbot1000
  Why there is a star inside randn? new_to_python 4 2,445 Mar-06-2020, 04:45 AM
Last Post: new_to_python
  Optimisation H0M1C1D4L_P1G 3 2,676 Dec-23-2019, 11:44 PM
Last Post: ichabod801
  Symbol identification in variables Ollie 2 3,197 Apr-05-2017, 09:48 AM
Last Post: gullidog

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020