Track a fix area with meanshift -OpenCV I need your help in OpenCV. I'm a student and am working on a project in which involves a little autonomous car. I use Python for the main programming language.

I have some problem with my meanshift.  When I run the program, I want the camera fixed on an area on the floor and stay on this area.  I would like for the car to drive alone while following the floor, and I don't want that meanshift to find another object different from the floor.

The program works well, but not optimally for the floor.  Do you have a solution or a way to proceed?

def camshift(frame1,frame2,trackWindow) :
    points=[]
    size1=frame1.shape[0] #width
    size2=frame1.shape[1] #length
    #hsv referentiel
    hsv = cv2.cvtColor(frame1, cv2.COLOR_BGR2HSV)
    #Mask
    mask = cv2.inRange(hsv,np.array((0., 60.,32.)), np.array((180.,255.,255.)))
    x0, y0, w, h = trackWindow

    trackWindow1=[x0,y0,w,h]
    x1 = x0 + w -1
    y1 = y0 + h -1

    hsv_roi = hsv[y0:y1, x0:x1]
    mask_roi = mask[y0:y1, x0:x1]
    #compute histogram for that area
    hist = cv2.calcHist( [hsv_roi], [0], mask_roi, [16], [0, 180] )
    #Normalisation 
    cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX);
    hist_flat = hist.reshape(-1)
    points.append(hsv)
    prob = cv2.calcBackProject([hsv,cv2.cvtColor(frame2, cv.CV_BGR2HSV)],[0],hist_flat,  [0,180],1)
    prob &amp;= mask
    term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )
    #checking conditions not to go out of bounds
    if (trackWindow[0] &lt; 0) :
        trackWindow[0] = 0
    if (trackWindow[1] &lt; 0) :
        trackWindow[1] = 0
    if (trackWindow[0]+trackWindow[3] &gt;= size1) :
        trackWindow1.append(size1-y0)
        w=trackWindow1[4]
        trackWindow1.pop()
    if (trackWindow[0]+trackWindow[2] &gt;= size2) :
        trackWindow1.append(size2-x0)
        h=trackWindow1[4]
        trackWindow1.pop()
    x0, y0, w, h = trackWindow
    if (trackWindow[0]+trackWindow[2])&lt;100 : 
        trackWindow = (125,125,200,100) 
    if (trackWindow[1]+trackWindow[3])&lt;100 : 
        trackWindow = (125,125,200,100) 
    # self.ellipse, trackWindow = cv2.CamShift(prob, trackWindow, term_crit)

    new_ellipse, track_window = cv2.CamShift(prob,trackWindow, term_crit)
    #we draw the rectangle on the relevant area
     cv2.rectangle(frame1, (trackWindow[0],trackWindow[1]), (trackWindow[0]+trackWindow[2],trackWindow[1]+trackWindow[3]), color=(255,0,0))

    cv2.imshow("mask",mask)
    return track_window
Whitespace. Use it. It makes code much clearer. Here's a list of places where you can add whitespace to make your code clearer.


(frame1,frame2,trackWindow) : \$\rightarrow\$ (frame1, frame2, trackWindow):
trackWindow1=[x0,y0,w,h] \$\rightarrow\$ trackWindow1 = [x0, y0, w, h]
( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 ) \$\rightarrow\$ (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)
cv2.imshow("mask",mask) \$\rightarrow\$ cv2.imshow("mask", mask)


There are many other places like this in your code, so I'm going to recommend looking at PEP8, Python's official style guide, on how to correct these.

Finally, in all your if conditions, are just if conditions. If you're not using elif, then all of these conditions will be evaluated if they are true. Are you sure that you want this?