[backport-1.6]: get_image_refs outputs too many images not belonging to a unit

A solution for issue #4385 (closed) needs to be backported to 1.6.x