Пример #1
0
    def ncc_segment(self, segment1, segments2, tol):
        shape = [tol, tol]
        region1 = segment1.get_image_region()
        scale = segment1.get_segmented_image().get_scale()
        i1 = segments2.get_img().get_data()
        i1 = nputils.zoom(i1, region1.get_center(), [2 * tol, 2 * tol])
        i2 = region1.get_region()
        mask = (i2 > 0)

        if min(region1.get_region().shape) <= 3:
            return None

        if self.mode == 'ncc':
            ncc = nputils.norm_xcorr2(i1, i2, mode='same')
            ncc[ncc < self.ncc_threshold] = 0
            # ncc = nputils.weighted_norm_xcorr2(i1, i2, (i2 > 0), mode='same')
            data = nputils.resize(ncc, shape)
        elif self.mode == 'ncc_peaks':
            data = np.zeros(shape)
            ncc = nputils.norm_xcorr2(i1, i2, mode='same')
            ncc = nputils.resize(ncc, shape)
            peaks = nputils.find_peaks(ncc,
                                       4,
                                       self.ncc_threshold,
                                       fit_gaussian=False)
            for peak in peaks[:]:
                data += imgutils.gaussian(shape, width=8,
                                          center=peak) * ncc[tuple(peak)]
        return data
Пример #2
0
    def ncc_segment(self, segment1, segments2, tol):
        shape = [tol, tol]
        region1 = segment1.get_image_region()
        scale = segment1.get_segmented_image().get_scale()
        i1 = segments2.get_img().get_data()
        i1 = nputils.zoom(i1, region1.get_center(), [2 * tol, 2 * tol])
        i2 = region1.get_region()
        mask = (i2 > 0)

        if min(region1.get_region().shape) <= 3:
            return None

        if self.mode == 'ncc':
            ncc = nputils.norm_xcorr2(i1, i2, mode='same')
            ncc[ncc < self.ncc_threshold] = 0
            # ncc = nputils.weighted_norm_xcorr2(i1, i2, (i2 > 0), mode='same')
            data = nputils.resize(ncc, shape)
        elif self.mode == 'ncc_peaks':
            data = np.zeros(shape)
            ncc = nputils.norm_xcorr2(i1, i2, mode='same')
            ncc = nputils.resize(ncc, shape)
            peaks = nputils.find_peaks(ncc, 4, self.ncc_threshold, fit_gaussian=False)
            for peak in peaks[:]:
                data += imgutils.gaussian(shape, width=8, center=peak) * ncc[tuple(peak)]
        return data
Пример #3
0
def do_gaussien(exp, size, nsigma=None, width=None):
    res = imgutils.gaussian(size, nsigma=nsigma, width=width)
    return np.equal(exp, (res * 1000).astype(np.int)).all()
Пример #4
0
def test_gaussien_no_arg():
    try:
        imgutils.gaussian(5)
        assert False
    except ValueError:
        pass
Пример #5
0
def test_region_image():
    m2 = np.zeros([5, 5])
    m2[2:3, 2:4] = 1

    img1 = imgutils.gaussian(100, width=6, angle=-0.5, center=[70, 55])
    img1[img1 < 0.1] = 0
    seg1, index = nputils.crop_threshold(img1, output_index=True)

    region1 = imgutils.ImageRegion(img1, index)

    assert np.allclose(region1.get_region(), seg1)
    assert np.allclose(region1.get_data(), img1)

    assert region1.get_shape() == img1.shape
    assert region1.get_shift() == [0, 0]
    assert region1.get_index() == index
    assert list(region1.get_center()) == [70, 55]

    region1.set_shift([-5, -4])

    img2 = imgutils.gaussian(100, width=6, angle=-0.5, center=[65, 51])
    img2[img2 < 0.1] = 0
    seg2, index2 = nputils.crop_threshold(img2, output_index=True)

    assert np.allclose(region1.get_region(), seg2)
    assert np.allclose(region1.get_data(), img2)
    assert list(region1.get_center()) == [65, 51], region1.get_center()

    region1.set_shift([-70, 10])

    img3 = imgutils.gaussian(100, width=6, angle=-0.5, center=[0, 65])
    img3[img3 < 0.1] = 0
    seg3, index = nputils.crop_threshold(img3, output_index=True)

    assert np.allclose(region1.get_region(), seg3)
    assert np.allclose(region1.get_data(), img3)
    assert list(region1.get_center()) == [0 + 6 / 2, 65]

    region1.set_shift([30, 10])

    img3 = imgutils.gaussian(100, width=6, angle=-0.5, center=[100, 65])
    img3[img3 < 0.1] = 0
    seg3, index = nputils.crop_threshold(img3, output_index=True)

    assert np.allclose(region1.get_region(), seg3)
    assert np.allclose(region1.get_data(), img3)
    assert list(region1.get_center()) == [100 - 5 / 2, 65
                                          ], (region1.get_center(), seg3.shape)

    region1.set_shift([20, 10])

    img3 = imgutils.gaussian(100, width=6, angle=-0.5, center=[90, 65])
    img3[img3 < 0.1] = 0
    seg3, index = nputils.crop_threshold(img3, output_index=True)

    assert np.allclose(region1.get_region(), seg3)
    assert np.allclose(region1.get_data(), img3)
    assert list(region1.get_center()) == [90, 65]

    region2 = imgutils.ImageRegion(img2, index2)

    builder = imgutils.ImageBuilder()
    builder.add(region1)
    builder.add(region2)

    res = builder.get()

    seg4, index = nputils.crop_threshold(img2 + img3, output_index=True)

    assert res.get_shape() == img1.shape
    assert np.allclose(res.get_data(), img3 + img2)
    assert np.allclose(res.get_region(), seg4)
    assert res.get_index() == index
Пример #6
0
def test_gaussien_no_arg():
    try:
        imgutils.gaussian(5)
        assert False
    except ValueError:
        pass
Пример #7
0
def test_region_image():
    m2 = np.zeros([5, 5])
    m2[2:3, 2:4] = 1

    img1 = imgutils.gaussian(100, width=6, angle=-0.5, center=[70, 55])
    img1[img1 < 0.1] = 0
    seg1, index = nputils.crop_threshold(img1, output_index=True)

    region1 = imgutils.ImageRegion(img1, index)

    assert np.allclose(region1.get_region(), seg1)
    assert np.allclose(region1.get_data(), img1)

    assert region1.get_shape() == img1.shape
    assert region1.get_shift() == [0, 0]
    assert region1.get_index() == index
    assert list(region1.get_center()) == [70, 55]

    region1.set_shift([-5, -4])

    img2 = imgutils.gaussian(100, width=6, angle=-0.5, center=[65, 51])
    img2[img2 < 0.1] = 0
    seg2, index2 = nputils.crop_threshold(img2, output_index=True)

    assert np.allclose(region1.get_region(), seg2)
    assert np.allclose(region1.get_data(), img2)
    assert list(region1.get_center()) == [65, 51], region1.get_center()

    region1.set_shift([-70, 10])

    img3 = imgutils.gaussian(100, width=6, angle=-0.5, center=[0, 65])
    img3[img3 < 0.1] = 0
    seg3, index = nputils.crop_threshold(img3, output_index=True)

    assert np.allclose(region1.get_region(), seg3)
    assert np.allclose(region1.get_data(), img3)
    assert list(region1.get_center()) == [0 + 6 / 2, 65]

    region1.set_shift([30, 10])

    img3 = imgutils.gaussian(100, width=6, angle=-0.5, center=[100, 65])
    img3[img3 < 0.1] = 0
    seg3, index = nputils.crop_threshold(img3, output_index=True)

    assert np.allclose(region1.get_region(), seg3)
    assert np.allclose(region1.get_data(), img3)
    assert list(region1.get_center()) == [100 - 5 / 2, 65], (region1.get_center(), seg3.shape)

    region1.set_shift([20, 10])

    img3 = imgutils.gaussian(100, width=6, angle=-0.5, center=[90, 65])
    img3[img3 < 0.1] = 0
    seg3, index = nputils.crop_threshold(img3, output_index=True)

    assert np.allclose(region1.get_region(), seg3)
    assert np.allclose(region1.get_data(), img3)
    assert list(region1.get_center()) == [90, 65]

    region2 = imgutils.ImageRegion(img2, index2)

    builder = imgutils.ImageBuilder()
    builder.add(region1)
    builder.add(region2)

    res = builder.get()

    seg4, index = nputils.crop_threshold(img2 + img3, output_index=True)

    assert res.get_shape() == img1.shape
    assert np.allclose(res.get_data(), img3 + img2)
    assert np.allclose(res.get_region(), seg4)
    assert res.get_index() == index
Пример #8
0
def do_gaussien(exp, size, nsigma=None, width=None):
    res = imgutils.gaussian(size, nsigma=nsigma, width=width)
    return np.equal(exp, (res * 1000).astype(np.int)).all()