Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reference histogram #1

Open
GianlucaCerilli opened this issue Sep 25, 2018 · 2 comments
Open

Reference histogram #1

GianlucaCerilli opened this issue Sep 25, 2018 · 2 comments

Comments

@GianlucaCerilli
Copy link

Hello egrinstein,
nice work. I am not understanding what is the reference histogram that you want to track.
For example, I would like to track the histogram of a particular object in a greyscale scene.
How can I do it with this code?

Thank you very much
Gianluca

@egrinstein
Copy link
Owner

Hey Gianluca.

Well, in order not to have to create an interface of some sort asking me what object I want to track in my program, I set the initial [x,y] of the histogram to be equal to [240,320] (this point was purely empirical based on my camera, screen, etc).

So it will start tracking the object at this point (it will compute the histogram for it at the start of the program).

In order to make this more interactive, we could implement a way of clicking in the screen to define the first point to be tracked, which i think is quite easy.

I also commented some of the functions within the code, I hope things are clearer. If not, keep asking!

Cheers,

Eric

@agnihsv
Copy link

agnihsv commented May 8, 2020

Good morning, sir,
I tried to use your particle filter code by defining by a screenshot, (from the mouse) my reference box to apply the particle filter, but it doesn't work so far. I'm attaching the code I wrote.
I'd like to have an article with it doing my job. Please help me. I'll quote your article in my publication.
best regars

import cv2
import numpy as np
from matplotlib import pyplot as plt
from particle_filter import ParticleFilter
objet=0
nbr_classes=180
seuil=30
term_criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1.0)

def click(event, x, y, flags, param):
global roi_x, roi_y, roi_w, roi_h, roi_hist, frame, objet

if event==cv2.EVENT_LBUTTONDBLCLK:
    roi_x, roi_y, roi_w, roi_h=cv2.selectROI('ROI', frame, False, False)
    roi=frame[roi_y: roi_y + roi_h, roi_x: roi_x + roi_w]
    hsv_roi=cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
    roi_hist=cv2.calcHist([hsv_roi], [0], None, [nbr_classes], [0, nbr_classes])
    cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)
    cv2.destroyWindow('ROI')
    plt.clf()
    plt.plot(roi_hist)
    plt.show(block=False)
    plt.pause(0.01)
    objet=1

video=cv2.VideoCapture('vtest.avi')
cv2.namedWindow('Camera')
cv2.setMouseCallback('Camera', click)
alpha = 0.5
while True:
ret, frame=video.read()

orig = np.array(frame)
img=frame
if objet:
    frame = cv2.resize(frame, (572, 572))
    #hsv=cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
   # mask=cv2.calcBackProject([hsv], [0], roi_hist, [0, nbr_classes], 1)
   # _, mask=cv2.threshold(mask, seuil, 255, cv2.THRESH_BINARY)
    #mask=cv2.erode(mask, None, iterations=3)
    #mask=cv2.dilate(mask, None, iterations=3)
    pf = ParticleFilter(roi_x,roi_y,frame,n_particles=500,square_size=50,
						dt=0.20)
    x,y,sq_size,distrib,distrib_control = pf.next_state(frame)
    p1 = (int(y - sq_size), int(x - sq_size))
    p2 = (int(y + sq_size), int(x + sq_size))
    # before resampling
    for (x2, y2, scale2) in distrib_control:
        x2 = int(x2)
        y2 = int(y2)
        cv2.circle(img, (y2, x2), 1, (255, 0, 0), thickness=10)
        # after resampling
    for (x1, y1, scale1) in distrib:
        x1 = int(x1)
        y1 = int(y1)
        cv2.circle(img, (y1, x1), 1, (0, 0, 255), thickness=10)

    cv2.rectangle(img, p1, p2, (0, 0, 255), thickness=5)
    cv2.addWeighted(orig, alpha, img, 1 - alpha, 0, img)

    cv2.imshow("video", img)
    key = cv2.waitKey(10) & 0xFF
    if key == ord('q'):
        quit()
    if key == ord('p'):
        seuil = min(250, seuil + 1)
    if key == ord('m'):
        seuil = max(1, seuil - 1)

video.release()
cv2.destroyAllWindows()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants