8
$\begingroup$

I am using the blender to create a visual parking scene model, by using the python script the model could produces a RGB picture of the parking scene as well as the depth image, both are showing below.

enter image description here

enter image description here

I am just wondering would it possible to also create a segmentation image which separates each object class with a color, for example all the car object are in red, all the ground are in blue, all the building are in grey, like the examples

enter image description hereenter image description here

$\endgroup$

2 Answers 2

10
$\begingroup$

Give each object a unique 'pass index'. You can use the compositor, the Object Info node, and some math nodes to convert it to a colour.

This happens when you divide the object index by the maximum index you're using (to get it to a 0-1 value), and apply it to a diffuse material:

As diffuse material

By using a Colour Ramp you can take that 0-1 value and map it to discrete colours:

With colour ramp as diffuse material

By swapping out the Diffuse shader with an Emission shader, you just get the straight colour values:

With colour ramp as emission shader

This is the final material:

Cycles material node setup

This is the blend file I used:

See the manual for more info.

$\endgroup$
2
  • $\begingroup$ is there any tutorial for this ? $\endgroup$
    – user824624
    Jun 8, 2017 at 1:39
  • $\begingroup$ I've added example images, the node setup, and an example blend file for you. $\endgroup$
    – dr. Sybren
    Jun 10, 2017 at 11:54
9
$\begingroup$

A python package bpycv can render annotations for instance segmentation and panoptic segmentation.

First, follow the instruction to install bpycv in Blender.

Here is a simple code to show how to get segmentation annotations. Copy-paste this code to Text Editor and click Run Script button(or Alt+P)

import cv2
import bpy
import bpycv
import random

# remove all MESH objects
[bpy.data.objects.remove(obj) for obj in bpy.data.objects if obj.type == "MESH"]

for inst_id in range(1, 20):
    # create cube and sphere as instance at random location
    location = [random.random() * 4 - 2 for _ in range(3)]
    if inst_id % 2:
        bpy.ops.mesh.primitive_cube_add(size=0.5, location=location)
    else:
        bpy.ops.mesh.primitive_uv_sphere_add(radius=0.5, location=location)
    obj = bpy.context.active_object
    # set each instance a unique inst_id, which is used to generate instance annotation.
    obj["inst_id"] = inst_id

# render image, instance annoatation and depth in one line code
result = bpycv.render_data()

# save result
cv2.imwrite("demo-rgb.jpg", result["image"])
cv2.imwrite("demo-inst.png", result["inst"])
# normalizing depth
cv2.imwrite("demo-depth.png", result["depth"] / result["depth"].max() * 255)

# visualization inst|rgb|depth for human
cv2.imwrite("demo-vis(inst|rgb|depth).jpg", result.vis())

The visualization result of instance segmentation, RGB image and depth enter image description here

More detial: bpycv

$\endgroup$
4
  • $\begingroup$ doesnt work for me No module named boxx $\endgroup$
    – cxnt
    Jan 21, 2020 at 14:42
  • $\begingroup$ @cxnt1 just install boxx by ‘“your blender python” -m pip install boxx’ $\endgroup$
    – Yang
    Jan 22, 2020 at 15:21
  • $\begingroup$ Sorry, could you please explain how to install it, as I dont really understand... No module named 'bpycv.boxx $\endgroup$
    – cxnt
    Feb 3, 2020 at 6:55
  • $\begingroup$ @cxnt1 I don't know the details of the error. Could you please provide more context.(you can send me email: [email protected] ) $\endgroup$
    – Yang
    Feb 4, 2020 at 7:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .