Is there a way to know (programmatically) how many objects are visibile in a rendering?
You can give each object a unique object ID ("pass index" in the object properties).These can be output as render pass; each pixel will have the ID of the object that's visible there. This will not only give you a count of the number of objects (just count the number of unique values) but also a ground truth for segmentation &amp; shape recognition. 

This Python script will give each selected object another ID:

import bpy

for nr, obj in enumerate(bpy.context.selected_objects):
    obj.pass_index = nr + 1  # reserve pass index 0 for "no object".


For more info, see the ID Mask Node section of the Blender manual.