Randomly populating a stack with a randomly oriented 3D object

Hello guys, I am trying to simulate an image for benchmarking. I know the shape of a molecule in 3D. Is there any way I can make use ImageJ to place this object in random positions and orientations throughout my stack?

Hi @silverbulletxd,

Here a plugin example to shuffle some objects, note there is no rotation implemented yet. The 3D ImageJ Suite is required.

Screenshot_20190401_113618

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import ij.IJ;
import ij.ImagePlus;
import ij.WindowManager;
import ij.plugin.filter.PlugInFilter;
import ij.process.ImageProcessor;
import mcib3d.geom.Object3D;
import mcib3d.geom.Object3DVoxels;
import mcib3d.geom.Objects3DPopulation;
import mcib3d.image3d.ImageByte;
import mcib3d.image3d.ImageInt;
import mcib3d.image3d.ImageShort;

import java.util.ArrayList;

/**
 * @author thomasb
 */
public class Shuffle_ implements PlugInFilter {

    ImagePlus plus;

    @Override
    public int setup(String string, ImagePlus ip) {
        plus = ip;

        return DOES_16 + DOES_8G;
    }

    @Override
    public void run(ImageProcessor ip) {
        int w = plus.getWidth();
        int h = plus.getHeight();
        int d = plus.getNSlices();
        ImagePlus maskPlus = WindowManager.getImage("mask");
        ImageInt maskimg;
        if (maskPlus == null) {
            IJ.log("No image with name \"mask\" found. Not using mask for shuffle.");
            maskimg = new ImageByte("mask", w, h, d);
            maskimg.fill(1);
        } else {
            maskimg = ImageInt.wrap(maskPlus);
        }

        ImageInt res = new ImageShort("Shuffle", w, h, d);
        ImageInt spots = ImageInt.wrap(plus);

        Object3D mask = new Object3DVoxels(maskimg);
        Objects3DPopulation objPopA = new Objects3DPopulation(spots);
        objPopA.setMask(mask);
        IJ.log("Shuffle started");
        ArrayList<Object3D> shuObj = objPopA.shuffle();
        Objects3DPopulation objPopB = new Objects3DPopulation(shuObj);
        objPopB.draw(res);
        res.show();
        IJ.log("Shuffle finished");
    }

}

Best,

Thomas

… and the jython code for convenience :

from mcib3d.geom import Objects3DPopulation,Object3DVoxels
from mcib3d.image3d import ImageInt,ImageByte,ImageShort
from ij import IJ,WindowManager

# get current image, a image with labelled objects
plus=WindowManager.getCurrentImage()
# wrap ImagePlus into 3D suite image format
img=ImageInt.wrap(plus)
# create a population of 3D objects
pop=Objects3DPopulation(img)
# create a mask where to shuffle objects
# here same as input image
maskimg = ImageByte("mask", img.sizeX, img.sizeY, img.sizeZ)
maskimg.fill(1)
obj=Object3DVoxels(maskimg)
pop.setMask(obj)
# get the shuffled objects and build a population from them
listObjects=pop.shuffle()
pop2=Objects3DPopulation(listObjects)
# draw the results
res = ImageShort("res", img.sizeX, img.sizeY, img.sizeZ)
pop2.draw(res)
res.show("shuffle")

best,

Thomas

Thanks a lot, it’s very appreciated.