How do I calculate how much of a object is covered in a 2D image?

I'm using code from here: Save the 2D bounding box of an object in rendered image to a text file

To calculate a bounding box coordinates for an object (say a human) in a rendered image. That works great.

I want however to expand this to be able to calculate how much of the object that is covered by other objects in the rendered image.

I want to do some research in AI/deep-learning as to try to train a neural network in detecting various computer generated objects in traffic. So any help here would be adding to that work.

• What have you tried so far? What have you got already? This sounds more like a general coding question rather than about Blender itself. Commented May 31, 2017 at 12:54
• I'm voting to close this question as off-topic because it seems like a general coding question, rather than about Blender itself. Commented May 31, 2017 at 12:55
• I think closing this question is unwarranted, as there is a clear, Blender-specific approach that easily answers this question (see my answer below). Commented May 31, 2017 at 20:21
• I have so far not tried to solve this specific problem. I was reaching out to the community to get some ideas on how to pursue the solution to avoid getting into dead ends. It seems that @codemanx user who made the code I refer to in my post would be a great person to ask? Commented Jun 1, 2017 at 12:40

In the "Render Layer" / "Passes" panel, enable the Object Index pass. You can then give each object, like your human object, a unique index using the "Pass Index" property in their "Relations" panel.

Once you've given each object of interest a unique "Pass Index", you can inspect that render pass after the image has rendered. Each pixel will have an RGB value equal to the "Pass Index". You can render twice, once with the obstructing objects and once without. Once you've done that, you can find your answer simply by counting counting pixels in each case; dividing the obstructed count by the unobstructed count will give you the unobstructed percentage.

• Thanx alot! This at least gives me a path forward. I was hoping to be able to do this with some linear algebra/projections or similar to take advantage over the inbuilt python support in Blender. But without knowing the internals, its a steep curve to learn how and what primitives to use to be able to calculate it in some efficient manner. After all, rendering the scene multiple times is heavy. Commented Jun 1, 2017 at 12:39
• Rendering this isn't heavy at all. This render pass is very cheap, and you don't need any other passes for this calculation. Commented Jun 1, 2017 at 15:16
=> fraction of the object visible in rendered image =
Actually visible vertices / Total vertices supposed to be visible in case of no occlusion