How to use the cellprofiler.image.Image function in CellProfiler

To help you get started, we’ve selected a few CellProfiler examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github CellProfiler / CellProfiler / tests / modules / test_measurecolocalization.py View on Github external
def test_cropped_objects():
    """Test images and objects with a cropping mask"""
    numpy.random.seed(0)
    image1 = numpy.random.uniform(size=(20, 20))
    i1 = cellprofiler.image.Image(image1)
    crop_mask = numpy.zeros((20, 20), bool)
    crop_mask[5:15, 5:15] = True
    i2 = cellprofiler.image.Image(image1[5:15, 5:15], crop_mask=crop_mask)
    labels = numpy.zeros((10, 10), int)
    labels[:4, :4] = 1
    labels[6:, 6:] = 2
    o = cellprofiler.object.Objects()
    o.segmented = labels
    #
    # Make the objects have the cropped image as a parent
    #
    o.parent_image = i2
    workspace, module = make_workspace(i1, i2, o)
    module.run(workspace)
    m = workspace.measurements
    mi = module.get_measurement_images(None, OBJECTS_NAME, "Correlation", "Correlation")
    corr = m.get_current_measurement(OBJECTS_NAME, "Correlation_Correlation_%s" % mi[0])
    assert round(abs(corr[0] - 1), 7) == 0
    assert round(abs(corr[1] - 1), 7) == 0
github CellProfiler / CellProfiler / tests / modules / test_identifyprimaryobjects.py View on Github external
x.fill_holes.value = False
            x.smoothing_filter_size.value = 0
            x.automatic_smoothing.value = 0
            x.maxima_suppression_size.value = distance
            x.automatic_suppression.value = False
            x.manual_threshold.value = .05
            x.unclump_method.value = cellprofiler.modules.identifyprimaryobjects.UN_INTENSITY
            x.watershed_method.value = cellprofiler.modules.identifyprimaryobjects.WA_INTENSITY
            x.threshold_scope.value = cellprofiler.modules.identify.TS_MANUAL
            x.threshold_smoothing_scale.value = 0
            pipeline = cellprofiler.pipeline.Pipeline()
            x.module_num = 1
            pipeline.add_module(x)
            object_set = cellprofiler.object.ObjectSet()
            measurements = cellprofiler.measurement.Measurements()
            measurements.add(x.image_name.value, cellprofiler.image.Image(img))
            x.run(cellprofiler.workspace.Workspace(pipeline, x, measurements, object_set, measurements,
                                                   None))
            output = object_set.get_objects(x.object_name.value)
            self.assertEqual(output.count, 4)
            self.assertTrue(numpy.all(output.segmented[expected == 0] == 0))
            self.assertEqual(len(numpy.unique(output.segmented[expected == 1])), 1)
github CellProfiler / CellProfiler / tests / modules / test_identifyprimaryobjects.py View on Github external
"""Regression test - was using one-connected instead of 3-connected structuring element"""
        x = cellprofiler.modules.identifyprimaryobjects.IdentifyPrimaryObjects()
        x.use_advanced.value = True
        x.object_name.value = "my_object"
        x.image_name.value = "my_image"
        x.exclude_size.value = False
        x.smoothing_filter_size.value = 0
        x.automatic_smoothing.value = False
        x.watershed_method.value = cellprofiler.modules.identifyprimaryobjects.WA_NONE
        x.threshold_scope.value = cellprofiler.modules.identify.TS_MANUAL
        x.threshold_smoothing_scale.value = 0
        x.manual_threshold.value = .5
        img = numpy.zeros((10, 10))
        img[4, 4] = 1
        img[5, 5] = 1
        image = cellprofiler.image.Image(img)
        image_set_list = cellprofiler.image.ImageSetList()
        image_set = image_set_list.get_image_set(0)
        image_set.providers.append(cellprofiler.image.VanillaImageProvider("my_image", image))
        object_set = cellprofiler.object.ObjectSet()
        measurements = cellprofiler.measurement.Measurements()
        pipeline = cellprofiler.pipeline.Pipeline()
        x.run(cellprofiler.workspace.Workspace(pipeline, x, image_set, object_set, measurements, None))
        self.assertEqual(len(object_set.object_names), 1)
        self.assertTrue("my_object" in object_set.object_names)
        objects = object_set.get_objects("my_object")
        segmented = objects.segmented
        self.assertTrue(numpy.all(segmented[img > 0] == 1))
        self.assertTrue(numpy.all(img[segmented == 1] > 0))
github CellProfiler / CellProfiler / tests / modules / test_straightenworms.py View on Github external
):
            feature = "_".join(
                (cellprofiler.modules.straightenworms.C_WORM, f, str(i + 1))
            )
            m.add_measurement(OBJECTS_NAME, feature, v)
    feature = "_".join(
        (
            cellprofiler.modules.straightenworms.C_WORM,
            cellprofiler.modules.straightenworms.F_LENGTH,
        )
    )
    m.add_measurement(OBJECTS_NAME, feature, lengths)

    image_set_list = cellprofiler.image.ImageSetList()
    image_set = image_set_list.get_image_set(0)
    image_set.add(IMAGE_NAME, cellprofiler.image.Image(image, mask))

    if auximage is not None:
        image_set.add(AUX_IMAGE_NAME, cellprofiler.image.Image(auximage))
        module.add_image()
        module.images[1].image_name.value = AUX_IMAGE_NAME
        module.images[1].straightened_image_name.value = AUX_STRAIGHTENED_IMAGE_NAME

    object_set = cellprofiler.object.ObjectSet()
    objects = cellprofiler.object.Objects()
    labels = numpy.zeros(image.shape, int)
    for i in range(control_points.shape[2]):
        if lengths[i] == 0:
            continue
        rebuild_worm_from_control_points_approx(
            control_points[:, :, i], radii, labels, i + 1
        )
github CellProfiler / CellProfiler / tests / test_image.py View on Github external
def test_02_02_must_be_binary_throws(self):
        x = cellprofiler.image.ImageSet(0, {}, {})
        x.add(IMAGE_NAME, cellprofiler.image.Image(numpy.zeros((10, 20), float)))
        self.assertRaises(ValueError, x.get_image, IMAGE_NAME, must_be_binary=True)
github CellProfiler / CellProfiler / tests / test_image.py View on Github external
def test_03_01_must_be_gray(self):
        x = cellprofiler.image.ImageSet(0, {}, {})
        x.add(IMAGE_NAME, cellprofiler.image.Image(numpy.zeros((10, 20), float)))
        image = x.get_image(IMAGE_NAME, must_be_grayscale=True)
        self.assertEqual(tuple(image.pixel_data.shape), (10, 20))
github CellProfiler / CellProfiler / tests / modules / test_tile.py View on Github external
module.set_module_num(1)
    module.tile_method.value = cellprofiler.modules.tile.T_ACROSS_CYCLES
    module.input_image.value = INPUT_IMAGE_NAME
    module.output_image.value = OUTPUT_IMAGE_NAME

    pipeline = cellprofiler.pipeline.Pipeline()

    def callback(caller, event):
        assert not isinstance(event, cellprofiler.pipeline.RunExceptionEvent)

    pipeline.add_listener(callback)
    pipeline.add_module(module)
    image_set_list = cellprofiler.image.ImageSetList()
    for i, image in enumerate(images):
        image_set = image_set_list.get_image_set(i)
        image_set.add(INPUT_IMAGE_NAME, cellprofiler.image.Image(image))

    workspace = cellprofiler.workspace.Workspace(
        pipeline,
        module,
        image_set_list.get_image_set(0),
        cellprofiler.object.ObjectSet(),
        cellprofiler.measurement.Measurements(),
        image_set_list,
    )
    return workspace, module
github CellProfiler / CellProfiler / tests / modules / test_maskimage.py View on Github external
def test_binary_mask():
    image_set_list = cellprofiler.image.ImageSetList()
    image_set = image_set_list.get_image_set(0)
    np.random.seed(0)
    pixel_data = np.random.uniform(size=(10, 15)).astype(np.float32)
    image_set.add(IMAGE_NAME, cellprofiler.image.Image(pixel_data))

    masking_image = np.random.uniform(size=(10, 15)) > 0.5
    image_set.add(MASKING_IMAGE_NAME, cellprofiler.image.Image(masking_image))

    pipeline = cellprofiler.pipeline.Pipeline()
    module = cellprofiler.modules.maskimage.MaskImage()
    module.source_choice.value = cellprofiler.modules.maskimage.IO_IMAGE
    module.object_name.value = OBJECTS_NAME
    module.image_name.value = IMAGE_NAME
    module.masking_image_name.value = MASKING_IMAGE_NAME
    module.masked_image_name.value = MASKED_IMAGE_NAME
    module.invert_mask.value = False
    module.set_module_num(1)

    workspace = cellprofiler.workspace.Workspace(
        pipeline,
github CellProfiler / CellProfiler / tests / modules / test_matchtemplate.py View on Github external
def test_run(module, image_set, workspace):
    coins = skimage.data.coins()

    image = cellprofiler.image.Image(coins)

    image_set.add("image", image)

    module.input_image_name.value = "image"

    module.output_image_name.value = "output"

    module.template_name.value = os.path.join(
        os.path.dirname(__file__), "../resources/template.png"
    )

    module.run(workspace)