Refresh Viewer Node's image data when pass index of ID Mask node has changed in python
The answer to this question is the answer to the alternative question: "How to get the image where all pixel values are set to the pass_index of the particular object". Instead of using "ID Mask" node, the IndexOB output of "Render Layers" node can be connected directly to the Image input of "Viewer" node. Then the function can be modified like this to get the bounding box for particular objectPassIndex:

def getVisibleBoundingBox(objectPassIndex):

    S = bpy.context.scene
    width  = int( S.render.resolution_x * S.render.resolution_percentage / 100 )
    height = int( S.render.resolution_y * S.render.resolution_percentage / 100 )
    depth  = 4

    pixels = np.array( bpy.data.images['Viewer Node'].pixels[:] ).reshape( [height, width, depth] )
    # Keep only one value for each pixel (white pixels have 1 in all RGBA channels anyway), thus converting the image to black and white
    pixels = np.array( [ [ pixel[0] for pixel in row ] for row in pixels ] )

    bbox = np.argwhere( pixels == objectPassIndex )
    (ystart, xstart), (ystop, xstop) = bbox.min(0), bbox.max(0) + 1
    return xstart, xstop, height - ystart, height - ystop