简体   繁体   中英

How to index properly

I am trying to look at individual pixel values within an image array, see if they are above a certain threshold,and find their coordinates if they meet this condition. I am having trouble with properly indexing each pixel within a for loop. The image in question is the first slice of a data cube called mkv_array with a size (1080,1920,500). I am finding stars within a star field, so the threshold that has to be met is based on the background levels in the image and the statistic noise (found with 'sep' function).

cap = cv2.VideoCapture('/home/jryan/Videos/RMS/20211019_233512-50mm.mkv') #50mm

frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) 
fps = cap.get(cv2.CAP_PROP_FPS)

count = 500 #number of frames to iterate over
mkv_array = np.zeros((1080,1920,count)) #initialize array of zeros
for i in range(count):
    frame = (cap.read()) # load frame from video, returns bool value and pixel array
    a = frame[1] # want only pixel array, excludes bool value
    gray_frame = cv2.cvtColor(np.float32(a), cv2.COLOR_BGR2GRAY) #convert from RGB to gray scale
    mkv_array[:,:,i] += gray_frame #add frames to initial array
print(mkv_array.shape)  


image_array = mkv_array[:,:,0].astype(np.float32) # first slice of a data cube
level = 5 #level above background noise

i = range(0,1080)
j = range(0,1920)
#find stars above certain threshold
for i in mkv_array[i,:,0]:
    for j in mkv_array[:,j,0]:
        bkg = sep.Background(image_array) # find background level of image array
        print(bkg.globalback)
        background = bkg.globalback #background level
        print(bkg.globalrms)
        noise = bkg.globalrms # noise in background
        if mkv_array[i,j,0] < background + level*noise: # reject stars below threshold
            continue 
        else:
            labeled, num_objects = ndimage.label(mkv_array[i,j,0]) # label stars that meet condition

        xy = np.array(ndimage.center_of_mass(image_array, labeled, range(1, num_objects+1))) #find coordinates for accepted stars
        print(xy)
        y,x = np.hsplit(xy,2)

The error I recieve is in my if mkv_array[i,j,0] < background + level*noise statement line: IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices. How can I fix this?

How I fixed this issue:

xdim = 1080
ydim = 1920
#find stars above certain threshold
star_list_x = []
star_list_y = []
for j in range(xdim): #loop through all x coordinates
    for k in range(ydim): #loop through all y coordinates
        if mkv_array[j,k,0] < threshold: #skip if below threshold

            continue 
        else:   #keep if above threshold, append to star list.
            star_list_x.append(j)
            star_list_y.append(k)

star_list = np.array(list(zip(star_list_x, star_list_y)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM