QuPath - transferring objects from one image to the other

I have opened a mask (CellPose - png format) in QuPath. I detected the nuclei and I would like now to transfer the detected objects into another image which is open in QuPath (the original image). How can I do this? Is there away to transfer objects from one image to the other?

Many thanks,

I am not sure if there is any specific reason you are detecting object from the mask image and trying to transfer them to the data image.

You could directly import your objects from a mask image onto your data image using a script. That may simplify the workflow. It would be not too hard if you only have one mask file per image in the project. If you have multiple tiles of mask files for one image then there is a script on the forum to handle that as well, here:

This thread is also very useful for importing masks:

Hi all!

Ah, heck of a morning, almost forgot to respond to this :slight_smile: Feel free to try this import script that takes a collection of png exports from CellPose and batch imports them into your project.

This will only work for images that CellPose can handle in one go, and is not intended for subsets (say tiling a whole slide image).

//QP 0.2.3 compliant
//Target a directory of PNG exports from CellPose and import those masks as detection objects into QuPath.

def directoryPath = 'J:/CellPoseProcess/Treated/' // TO CHANGE

double downsample = 1 // TO CHANGE (if needed)
ImagePlane plane = ImagePlane.getDefaultPlane()

File folder = new File(directoryPath);
File[] listOfFiles = folder.listFiles();

currentImport = listOfFiles.find{ GeneralTools.getNameWithoutExtension(it.getPath()).contains(GeneralTools.getNameWithoutExtension(getProjectEntry().getImageName())) &&  it.toString().contains(".png")}

path = currentImport.getPath()
    /*if (!path.endsWith(".png"))
def imp = IJ.openImage(path)

int n = imp.getStatistics().max as int
if (n == 0) {
    print 'No objects found!'
def ip = imp.getProcessor()
    if (ip instanceof ColorProcessor) {
        throw new IllegalArgumentException("RGB images are not supported!")
def roisIJ = RoiLabeling.labelsToConnectedROIs(ip, n)

    def rois = roisIJ.collect {
        if (it == null)
        return IJTools.convertToROI(it, 0, 0, downsample, plane);
    rois = rois.findAll{null != it}
    // Convert QuPath ROIs to objects
    def pathObjects = rois.collect {
        return PathObjects.createDetectionObject(it)
print "Import completed"

import ij.gui.Wand
import qupath.lib.objects.PathObjects
import qupath.lib.regions.ImagePlane
import ij.IJ
import ij.process.ColorProcessor
import qupath.imagej.processing.RoiLabeling
import qupath.imagej.tools.IJTools
import java.util.regex.Matcher
import java.util.regex.Pattern

That said, you could use the object transfer scripts found here to move objects between images:

Your affine transform would be the default, since you are not rotate or manipulating the objects as they travel between images. You would also probably want to adjust the script to copy only to one target image rather than all images.


1 Like

Sounds as a great idea, I will try it.
Many thanks!