python, particle filter questions

Asked 2 years ago, Updated 2 years ago, 477 views

I want to track white objects using particle filters, but an error occurred when I tried to run them, so I didn't know the cause, so it stopped.If anyone knows the cause, please let me know.

The running environment will be Jupiter Lab.

error messages:

IndexError: only integers, slices(`:`), ellipsis(`...`), numpy.newaxis(`None`) and integer or boolean arrays are valid indications

code:

import numpy as np
import cv2

def tracking():
    cap=cv2.VideoCapture("tennisu.mp4")
    # particle filter initialization
    filter=ParticleFilter()
    filter.initialize()

    while True:
        ret, frame = cap.read()
        gray=cv2.cvtColor(frame,cv2.COLOR_RGB2GRAY)
        y,x=filter.filtering(gray)#tracking
        frame = cv2.circle(frame, (int(x), int(y))), 10, (0,0,255), -1)
        for i in range (filter.SAMPLEMAX):
            frame = cv2.circle(frame, (int(filter.X[i]), int(filter.Y[i]))), 2, (0,0,255), -1)
        cv2.imshow("frame", frame)
        if cv2.waitKey(20)&0xFF==27:
            break
    cap.release()
    cv2.destroyAllWindows()

# Particle filter class
classParticleFilter:
    def__init__(self):
        self.SAMPLEMAX=1000
        self.height, self.width=1440,2560# Frame image size

    # particle initialization
    # scatter throughout the image
    default initialize (self):
        self.Y=np.random.random(self.SAMPLEMAX) *self.height
        self.X = np.random.random(self.SAMPLEMAX) *self.width

    # Update Particle Status Assume that the object moves properly at the appropriate speed
    def modeling (self):
        self.Y+=np.random.random(self.SAMPLEMAX)*20-10
        self.X+=np.random.random(self.SAMPLEMAX)*20-10

    #weight normalization
    default (self, weight):
        return weight /np.sum(weight)

    # particle resampling
    # Select particles according to weight
    # Return index of remaining particles
    def resampling (self, weight):
        index=np.range(self.SAMPLEMAX)
        sample=[ ]

        for i in range (self.SAMPLEMAX):
            idx=np.random.choice(index,p=weight)
            sample.append(idx)

        return sample

    # likelihood calculation
    # Particles flying outside the image weigh 0
    #Assume a white object
    def calcLikelihood(self, image):
        mean, std = 250.0, 10.0
        intensity = [ ]

        for i in range (self.SAMPLEMAX):
            y, x = self.Y[i], self.X[i]
            if y>=0 and y<self.height and x>=0 and x<self.width:
                intensity.append(image[y,x])
            else:
                intensity.append(-1)

        weights = 1.0/np.sqrt(2*np.pi*std)*np.exp (-(np.array(intensity)-mean)**2/(2*std**2))
        weights [intensity==-1] = 0
        weights = self.normalize (weights)
        return weights

    # Start tracking
    # return one's expectations
    def filtering (self, image):
        self.modeling()
        weights = self.calcLikehood(image)
        index=self.resampling(weights)
        self.Y = self.Y [index]
        self.X = self.X [index]
        
        return np.sum(self.Y)/float(len(self.Y)) , np.sum(self.X)/float(len(self.X))

tracking()

python python3 opencv

2022-09-30 22:05

1 Answers

I don't know if it's the answer I'm looking for, but
Based on the error content,

in the calcLikelihood method
 intensity.append (image[y, x])

In part, the image is only integers (integer type), so the float, y, x, is not allowed, so there is an error.

The last part is

y, x=int(self.Y[i]), int(self.X[i])

Then it will work.


2022-09-30 22:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.