# 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. – Duarte Farrajota Ramos May 31 '17 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. – Duarte Farrajota Ramos May 31 '17 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). – dr. Sybren May 31 '17 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? – Erik Lönroth Jun 1 '17 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. – Erik Lönroth Jun 1 '17 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. – dr. Sybren Jun 1 '17 at 15:16
• BTW, please accept this answer as the answer. It'll mark your question as answered and give me some reputation points. – dr. Sybren Jun 5 '17 at 9:53
• Hello. I'll try the method and see how far I get. – Erik Lönroth Jun 20 '17 at 12:43
• I'm not able to produce a render with "Index Pass = 123". It turns out to be all grey all the time. Any hint on what I might be doing wrong? – Erik Lönroth Jun 20 '17 at 13:26

I was doing exactly the same thing, but my scene takes a lot more time to render, on top of my old machine to work on. So I used to check all the vertices of all the objects, if they are visible or not. First I would parse a single object in my scene, and check how many of the vertices are visible to camera(assuming the objects isn't occluded even a bit), second things is out of all those vertices, I did check how many vertices are actually visible(not occluded by other objects). All this is done using just a simple 2d array, which I also described in another question, here.

=> fraction of the object visible in rendered image =

Actually visible vertices / Total vertices supposed to be visible in case of no occlusion

Note: This approach might not be the best, but for the objects which has a good amount of vertices unlike cube/cuboids, this approach would do just fine, as it did for me. As for the computation expense, this is cheaper in comparison of complex scene rendering multiple times because of too many objects existing in it.

Let me know if any of you find any issue in here, it would be better for me as well to know any other flaws in this approach as I am still working with this.