Python + OpenCV, change brightness/darkness outside a sliding window?
Question: I'm working with Python and OpenCV and I'm a newbie in both. For my project, I
need to move a sliding window over a picture; for each position of the window
the area outside the window must be shown darker than the area inside the
window.

This is the part of my code that takes care of the picture and window
visualization (the valid positions for the sliding window are calculated
somewhere else)

    
    
    for (x, y, window) in valid_positions:        
        if window.shape[0] != winH or window.shape[1] != winW:
           continue
    
        # Put here stuff to process the window content
        # i.e apply a classifier
    
        clone = image.copy()
        cv2.rectangle(clone, (x, y), (x + winW, y + winH), (0, 255, 0), 2)
    
        cv2.imshow("Window", clone)
        cv2.waitKey(1)
        time.sleep(0.025)
    

The window is created and it slides on the valid positions, so that part works
well. But I have absolutely no idea on how to make the picture outside the
window appear darker.

Any suggestions? Thanks in advance.

EDIT: i forgot to add an important detail: my input images are always in black
and white (not even greyscale, just black and white pixels). Maybe this makes
it easier to alter the brightness/darkness?


Answer: In general, you can preserve the content inside the window and lower the
intensity of the entire image. Then replace the area inside the window with
original content. That trick should work. This part of the code may look like

    
    
    clone = image.copy()
    windowArea = clone[y:y + winH, x:x + winW].copy()
    clone = np.floor(clone * 0.5).astype('uint8') # 0.5 can be adjusted
    clone[y:y + winH, x:x + winW] = windowArea
    cv2.rectangle(clone, (x, y), (x + winW, y + winH), (0, 255, 0), 2)
    

