示例#1
0
def test_windowed_histogram():
    # check the number of valid pixels in the neighborhood

    image8 = np.array([[0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 1, 1, 1, 0],
                       [0, 1, 1, 1, 0], [0, 0, 0, 0, 0]],
                      dtype=np.uint8)
    elem = np.ones((3, 3), dtype=np.uint8)
    outf = np.empty(image8.shape + (2, ), dtype=float)
    mask = np.ones(image8.shape, dtype=np.uint8)

    # Population so we can normalize the expected output while maintaining
    # code readability
    pop = np.array([[4, 6, 6, 6, 4], [6, 9, 9, 9, 6], [6, 9, 9, 9, 6],
                    [6, 9, 9, 9, 6], [4, 6, 6, 6, 4]],
                   dtype=float)

    r0 = np.array([[3, 4, 3, 4, 3], [4, 5, 3, 5, 4], [3, 3, 0, 3, 3],
                   [4, 5, 3, 5, 4], [3, 4, 3, 4, 3]],
                  dtype=float) / pop
    r1 = np.array([[1, 2, 3, 2, 1], [2, 4, 6, 4, 2], [3, 6, 9, 6, 3],
                   [2, 4, 6, 4, 2], [1, 2, 3, 2, 1]],
                  dtype=float) / pop
    rank.windowed_histogram(image=image8, selem=elem, out=outf, mask=mask)
    assert_array_equal(r0, outf[:, :, 0])
    assert_array_equal(r1, outf[:, :, 1])

    # Test n_bins parameter
    larger_output = rank.windowed_histogram(image=image8,
                                            selem=elem,
                                            mask=mask,
                                            n_bins=5)
    assert larger_output.shape[2] == 5
示例#2
0
def test_windowed_histogram():
    # check the number of valid pixels in the neighborhood

    image8 = np.array([[0, 0, 0, 0, 0],
                       [0, 1, 1, 1, 0],
                       [0, 1, 1, 1, 0],
                       [0, 1, 1, 1, 0],
                       [0, 0, 0, 0, 0]], dtype=np.uint8)
    elem = np.ones((3, 3), dtype=np.uint8)
    outf = np.empty(image8.shape+(2,), dtype=float)
    mask = np.ones(image8.shape, dtype=np.uint8)

    # Population so we can normalize the expected output while maintaining
    # code readability
    pop = np.array([[4, 6, 6, 6, 4],
                    [6, 9, 9, 9, 6],
                    [6, 9, 9, 9, 6],
                    [6, 9, 9, 9, 6],
                    [4, 6, 6, 6, 4]], dtype=float)

    r0 =  np.array([[3, 4, 3, 4, 3],
                    [4, 5, 3, 5, 4],
                    [3, 3, 0, 3, 3],
                    [4, 5, 3, 5, 4],
                    [3, 4, 3, 4, 3]], dtype=float) / pop
    r1 =  np.array([[1, 2, 3, 2, 1],
                    [2, 4, 6, 4, 2],
                    [3, 6, 9, 6, 3],
                    [2, 4, 6, 4, 2],
                    [1, 2, 3, 2, 1]], dtype=float) / pop
    rank.windowed_histogram(image=image8, selem=elem, out=outf, mask=mask)
    assert_array_equal(r0, outf[:,:,0])
    assert_array_equal(r1, outf[:,:,1])

    # Test n_bins parameter
    larger_output = rank.windowed_histogram(image=image8, selem=elem,
                                            mask=mask, n_bins=5)
    assert larger_output.shape[2] == 5
示例#3
0
def windowed_histogram_similarity(image, selem, reference_hist, n_bins):
    # Compute normalized windowed histogram feature vector for each pixel
    px_histograms = rank.windowed_histogram(image, selem, n_bins=n_bins)

    # Reshape coin histogram to (1,1,N) for broadcast when we want to use it in
    # arithmetic operations with the windowed histograms from the image
    reference_hist = reference_hist.reshape((1, 1) + reference_hist.shape)

    # Compute Chi squared distance metric: sum((X-Y)^2 / (X+Y));
    # a measure of distance between histograms
    X = px_histograms
    Y = reference_hist
    num = (X - Y)**2
    denom = X + Y
    frac = num / denom
    frac[denom == 0] = 0
    chi_sqr = 0.5 * np.sum(frac, axis=2)

    # Generate a similarity measure. It needs to be low when distance is high
    # and high when distance is low; taking the reciprocal will do this.
    # Chi squared will always be >= 0, add small value to prevent divide by 0.
    similarity = 1 / (chi_sqr + 1.0e-4)

    return similarity
def windowed_histogram_similarity(image, selem, reference_hist, n_bins):
    # Compute normalized windowed histogram feature vector for each pixel
    px_histograms = rank.windowed_histogram(image, selem, n_bins=n_bins)

    # Reshape coin histogram to (1,1,N) for broadcast when we want to use it in
    # arithmetic operations with the windowed histograms from the image
    reference_hist = reference_hist.reshape((1, 1) + reference_hist.shape)

    # Compute Chi squared distance metric: sum((X-Y)^2 / (X+Y));
    # a measure of distance between histograms
    X = px_histograms
    Y = reference_hist
    num = (X - Y) ** 2
    denom = X + Y
    frac = num / denom
    frac[denom == 0] = 0
    chi_sqr = 0.5 * np.sum(frac, axis=2)

    # Generate a similarity measure. It needs to be low when distance is high
    # and high when distance is low; taking the reciprocal will do this.
    # Chi squared will always be >= 0, add small value to prevent divide by 0.
    similarity = 1 / (chi_sqr + 1.0e-4)

    return similarity