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

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