Esempio n. 1
0
def weightedKrawtchoukPolynomials(p, width):

    # Data containers
    sigma = createVectorF(width)
    ro = createVectorF(width)
    K = createImageF(width, width)

    # Coefficient size
    N = width - 1

    # Weight
    for x in range(0, width):
        sigma[x] = nCr(N, x) * pow(p, x) * pow(1 - p, N - x)

    # Scale factor. Commented direct computation and using for to avoid factorial
    #for n in range(0,width):
    #    ro[n] = pow(-1,n) * pow((1-p)/p,n) * (float(factorial(n)) / risingFactorial(-N, n))
    ro[0] = 1
    for n in range(1, N):
        ro[n] = (-1 * ((1.0 - p) / p) * n / (-N + (n - 1))) * ro[n - 1]
    ro[N] = (((1.0 - p) / p) * N) * ro[N - 1]

    # Krawtchouk matrix that store result of the polynomial
    # Each row is a polynomial each column is the polynomial value for an x value
    # Alternatively, we could have used the polynomial generating function
    q = 1.0 / p
    for n, x in itertools.product(range(0, width), range(0, width)):
        for s in range(0, width):
            K[n, x] += pow(-1, s) * nCr(N - x, n - s) * nCr(x, s) * pow(
                q - 1, n - s)

    # Normalize rows for stability
    for n in range(0, width):
        scale = K[n, 0]
        for x in range(0, width):
            K[n, x] /= scale

    # Obtain the coefficients A of the polynomials from K
    # Solve for the coefficients A in A*C = K
    C = createImageF(width, width)
    for n, x in itertools.product(range(0, width), range(0, width)):
        C[n, x] = pow(x, n)

    CT = np.transpose(C)
    KT = np.transpose(K)
    AT = np.linalg.solve(CT,
                         KT)  # solves the equation A*x=b   A*C = k, C'*A' = K'
    A = np.transpose(AT)

    # Product defining the weighted
    w = createImageF(width, width)
    for n, x in itertools.product(range(0, width), range(0, width)):
        w[n, x] = sqrt(sigma[x] / ro[n])

    return K, A, sigma, ro, w
# Math and iteration
from math import  sqrt, factorial
from timeit import itertools

'''
Parameters:
    p = Polynomial parameter. 0.5 for centralized polynomials. It should be between .1 and .9
    width = Width of the data image. Number of polynomials. It must be less that 100 to avoid overflow
    numPolynomialsDraw = Number of polynomials to draw
'''
numPolynomialsDraw = 5
p = 0.3
width = 100

# Data containers
sigma = createVectorF(width)
ro = createVectorF(width)
K = createImageF(width,width)

# Coefficient size
N = width-1

# Weight. It can be replaced by recurrence relation
for x in range(0,width): 
    sigma[x] = nCr(N, x) * pow(p,x) * pow(1-p,N-x)
    
# Scale factor. It can be replaced by recurrence relation
for n in range(0,width): 
    ro[n] = pow(-1,n) * pow((1-p)/p,n) * (float(factorial(n)) / risingFactorial(-N, n))

# Krawtchouk matrix that store result of the polynomial
numEdges = len(sumArcLenghts)
shapeLenght = sumArcLenghts[numEdges - 1]

# If number descriptors is 0 use the maximum according to the lenght
if numDescriptors == 0:
    numDescriptors = 1 + int(numEdges /2)

# Compute coefficients 
coefficients = createImageF(numDescriptors, 2)
lenghtNorm = 2.0 * pi / shapeLenght
for k in range(1, numDescriptors):
    arcLenght = 0
    for p in range(0, numEdges):
        coefficients[0, k] += cumulativeFunc[p] * (sumArcLenghts[p] - arcLenght)          \
                                                * cos(k * sumArcLenghts[p] * lenghtNorm)
        coefficients[1, k] += cumulativeFunc[p] * (sumArcLenghts[p] - arcLenght)          \
                                                * sin(k * sumArcLenghts[p] * lenghtNorm)
        arcLenght = sumArcLenghts[p]
  
    coefficients[0, k] = coefficients[0, k] *(2.0/shapeLenght)
    coefficients[1, k] = coefficients[1, k] *(2.0/shapeLenght) - (2.0/k)

# Rotation invariant descriptors
descriptors = createVectorF(numDescriptors)
for k in range(0, numDescriptors):
    descriptors[k] = sqrt(coefficients[0, k]*coefficients[0, k] +                         \
                          coefficients[1, k]*coefficients[1, k])

# Plot coefficients and descriptors
plotHistogram(descriptors, [0, 1], .95)
Esempio n. 4
0
    imageName = Input image name
'''
pathToDir = "../../Images/Chapter3/Input/"
imageName = "Horse.png"

# Read image into array
inputImage, width, height = imageReadL(pathToDir + imageName)

# Show input image
showImageL(inputImage)

# Compute histogram of the input image
inputHistogram = computeHistogram(inputImage)

# Create histograms to store cumulative moments
w = createVectorF(256)
m = createVectorF(256)

# Create histograms to store separation
separability = createVectorF(256)

# Obtain histograms
normalization = 1.0 / float(width * height)
w[0] = normalization * inputHistogram[0]
for level in range(1, 256):
    w[level] = w[level - 1] + normalization * inputHistogram[level]
    m[level] = m[level - 1] + level * normalization * inputHistogram[level]

# Look for the maximum
maximumLevel = 0
for level in range(0, 256):
Esempio n. 5
0
peakDetection = 0.6
peakDetectionR = 0.7
deltaPtRange = [10, 15]

# Read image into array and show
inputImage, width, height = imageReadL(pathToDir + imageName)
showImageL(inputImage)

# Compute edges
magnitude, angle = applyCannyEdgeDetector(inputImage, gaussianKernelSize,
                                          sobelKernelSize, upperT, lowerT)
showImageF(magnitude)

# Accumulator for the slope in degrees
maxLenght = int(sqrt(height * height + width * width) / 2)
accM = createVectorF(360)
cx, cy = int(width / 2), int(height / 2)

# Gather evidence for a point x,y
for x, y in itertools.product(range(0, width), range(0, height)):
    if magnitude[y, x] != 0:
        # Look for points at this distance
        for dx,dy in itertools.product(range(-deltaPtRange[1],deltaPtRange[1]+1),    \
                                       range(-deltaPtRange[1],deltaPtRange[1]+1)):

            if abs(dx) > deltaPtRange[0] or abs(dy) > deltaPtRange[0]:
                wx, wy = x + dx, y + dy
                if wx > 0 and wy > 0 and wx < width and wy < height                         \
                          and magnitude[wy, wx] !=0:
                    pointAngle = atan2(-float(wx - x), float(wy - y)) + pi
                                accumulator[y, intX] += (1.0 - weight)
                                accumulator[y, intX + 1] += weight

# Find maximum
maximumPos = imageArgMax(accumulator)
maximum = accumulator[maximumPos[0], maximumPos[1]]

# Plot a slide of the accumulator
plot3DHistogram(accumulator)

# Prepare output image as a dark version of the input
outputImage = createScaleImageL(inputImage, 0.5)

# Accumulator for the radius
maxR = int(max(width, height) / 2)
accR = createVectorF(maxR)
for x, y in itertools.product(range(0, width), range(0, height)):
    if magnitude[y, x] != 0:
        # Look for points at this distance
        r = sqrt( (maximumPos[1] - x) * (maximumPos[1] - x) +     \
                  (maximumPos[0] - y) * (maximumPos[0] - y) )
        bucket = int(r)
        if bucket > 0 and bucket < maxR - 1:
            weight = r - int(r)
            accR[bucket] += (1.0 - weight)
            accR[bucket + 1] += weight
plotHistogram(accR)
maximumR = imageArgMax(accR)[0]

# Draw located circle
for m in range(0, 360):