Beispiel #1
0
# Set the pixels of Gaussian kernel
centre = (kernelSize - 1) / 2
sumValues = 0
for x,y in itertools.product(range(0, kernelSize), range(0, kernelSize)):
    kernelImage[y,x] = exp( -0.5 * (pow((x - centre)/sigma, 2.0) +    \
                                    pow((y - centre)/sigma, 2.0)) ) 
    sumValues += kernelImage[y,x] 
     
    # This is only set for the plot
    colorsRGB[y,x] = [kernelImage[y,x], kernelImage[y,x], kernelImage[y,x]]

# Normalisation
for x,y in itertools.product(range(0, kernelSize), range(0, kernelSize)):
    kernelImage[y,x] /= sumValues

# Apply kernel
outputImage =  applyKernel(inputImage, kernelImage)

# Show function
plotColorSurface(kernelImage, colorsRGB)

# Show kernel
plot3DColorHistogram(kernelImage, colorsRGB)

# Print kernel
printImageRangeF(kernelImage, [0, kernelSize-1], [0, kernelSize-1], '7.3')

# Show output image
showImageL(outputImage)
        pascal1[y,x,1] = factorial(kernelSize - 2) /                      \
                         (factorial(kernelSize - 2 - y) * factorial(y))
        
    # Pascal shift to the right
    xp = x - 1
    if (kernelSize - 2 - xp >= 0 and xp >= 0):
        pascal2[y,x,0] = factorial(kernelSize - 2) /                      \
                         (factorial(kernelSize - 2 - xp) * factorial(xp))
        
    yp = y - 1
    if (kernelSize - 2 - yp >= 0 and yp >= 0):
        pascal2[y,x,1] = factorial(kernelSize - 2) /                      \
                         (factorial(kernelSize - 2 - yp) * factorial(yp))
        
    # Sobel    
    sobel[y,x,0] = smooth[y,x,1] * (pascal1[y,x,0] - pascal2[y,x,0])
    sobel[y,x,1] = smooth[y,x,0] * (pascal1[y,x,1] - pascal2[y,x,1])                                       
        
# Print pixel's values of the kernel
printImageRangeF(smooth[:,:,0], [0, kernelSize-1], [0, kernelSize-1], '2.0f')
printImageRangeF(smooth[:,:,1], [0, kernelSize-1], [0, kernelSize-1], '2.0f')

printImageRangeF(pascal1[:,:,0], [0, kernelSize-1], [0, kernelSize-1], '2.0f')
printImageRangeF(pascal1[:,:,1], [0, kernelSize-1], [0, kernelSize-1], '2.0f')

printImageRangeF(pascal2[:,:,0], [0, kernelSize-1], [0, kernelSize-1], '2.0f')
printImageRangeF(pascal2[:,:,1], [0, kernelSize-1], [0, kernelSize-1], '2.0f')

printImageRangeF(sobel[:,:,0], [0, kernelSize-1], [0, kernelSize-1], '4.0f')
printImageRangeF(sobel[:,:,1], [0, kernelSize-1], [0, kernelSize-1], '4.0f')
else:
    t = 0.5 * atan(2.0*centMom[1,1]/(centMom[2,0]-centMom[0,2]))
    
# Opposite direction for rotation invariant
t = -t;

# Geometric invariant moments from image region
vn = createImageF(numMoments,numMoments)
for m,n in itertools.product(range(0, numMoments), range(0, numMoments)):
    for indexPixel in range(0, numPoints):
        y = (imageRegion[indexPixel])[0]
        x = (imageRegion[indexPixel])[1]
        val = (imageRegion[indexPixel])[2]
        vn[n,m] += ((x-xc)*cos(t) - (y-yc)*sin(t))**n *                     \
                   ((x-xc)*sin(t) + (y-yc)*cos(t))**m * val
    c = 1 + ((n + m) / 2.0)
    if vn[n,m] > 0: vn[n,m] = log10(vn[n,m] / pow(M[0,0],c))
    else:           vn[n,m] = 0  
    
printImageRangeF(vn, [0,numMoments-1],[0,numMoments-1], "6.2f")

# Compute invariant moments from normailsed central moments
m1 = centMomNorm[2,0] + centMomNorm[0,2]
m2 = (centMomNorm[2,0] - centMomNorm[0,2])**2 + 4*  (centMomNorm[1,1]**2)  
m3 = (centMomNorm[3,0] - 3.0*centMomNorm[1,2])**2 + (3.0*centMomNorm[2,1]  \
                       - centMomNorm[0,3])**2

printText("M1 = " + '%.4f' % m1)
printText("M2 = " + '%.4f' % m2)
printText("M3 = " + '%.4f' % m3)
'''
pathToDir = "../../Images/Chapter4/Input/"
imageName = "Squares.png"

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

# Show input image
showImageL(inputImage)

outputMagnitude = createImageF(width, height)
outputDirection = createImageF(width, height)

for x, y in itertools.product(range(0, width - 1), range(0, height - 1)):
    mX, mY = 0.0, 0.0
    for c in range(-1, 2):
        mX += float(inputImage[y - 1, x + c]) - float(inputImage[y + 1, x + c])
        mY += float(inputImage[y + c, x - 1]) - float(inputImage[y + c, x + 1])
    outputMagnitude[y, x] = sqrt(mX * mX + mY * mY)
    outputDirection[y, x] = atan2(mY, mX)

# Show output image
showImageF(outputMagnitude)
showImageF(outputDirection)

# Print pixel's values in an image range
printImageRangeF(outputDirection, [0, width - 1], [0, height - 1])

# Plot vectors
plotQuiver(outputMagnitude, outputDirection, 1300)
Beispiel #5
0
shapeImage = pixlesList(scaledImage, background)
numPoints = len(shapeImage)

# Polynomials, coefficients and weights for the Krawtchouk polynomials
# Considering that A*C = k. For a the coefficients and C the powers x, x^2, x^3,..
N = max(width, height)
kW, aW, sigma, ro, w = weightedKrawtchoukPolynomials(p, N)

# Krawtchouk moments of the shape  by standard definition
Q = createImageF(numMoments, numMoments)
for m, n in itertools.product(range(0, numMoments), range(0, numMoments)):
    for indexPixel in range(0, numPoints):
        y, x = (shapeImage[indexPixel])[0], (shapeImage[indexPixel])[1]
        v = (shapeImage[indexPixel])[2]
        Q[n, m] += w[x, m] * kW[x, m] * w[y, n] * kW[y, n] * v
printImageRangeF(Q, [0, numMoments - 1], [0, numMoments - 1], " 8.2f")

# Krawtchouk moments from the geometric moments Gij = x**i , y**j.
G = createImageF(N, N)
for i, j in itertools.product(range(0, N), range(0, N)):
    for indexPixel in range(0, numPoints):
        y, x = (shapeImage[indexPixel])[0], (shapeImage[indexPixel])[1]
        v = (shapeImage[indexPixel])[2]
        G[j, i] += sqrt(sigma[x] * sigma[y]) * y**j * x**i * v

Qs = createImageF(numMoments, numMoments)
for m, n in itertools.product(range(0, numMoments), range(0, numMoments)):
    for i, j in itertools.product(range(0, N), range(0, N)):
        Qs[n, m] += aW[m, i] * aW[n, j] * G[j, i]

    Qs[n, m] *= (1.0 / sqrt(ro[n] * ro[m]))
sigma = 1.5

# To store kernel
kernelLaplacian = createImageF(kernelSize, kernelSize)

# Create kernel
s2Inv = 1.0 / (sigma * sigma)
kernelCentre = (kernelSize - 1) / 2

# Generate kernel values
sumValues = 0.0
for x, y in itertools.product(range(0, kernelSize), range(0, kernelSize)):
    nx2 = float(x - kernelCentre) * float(x - kernelCentre)
    ny2 = float(y - kernelCentre) * float(y - kernelCentre)
    s = 0.5 * (nx2 + ny2) * s2Inv

    kernelLaplacian[y, x] = -s2Inv * s2Inv * (1.0 - s) * exp(-s)
    sumValues += kernelLaplacian[y, x]

# Normalize
for x, y in itertools.product(range(0, kernelSize), range(0, kernelSize)):
    kernelLaplacian[y, x] /= sumValues

# Print kernel
printImageRangeF(kernelLaplacian, [0, kernelSize - 1], [0, kernelSize - 1],
                 ' 8.2f')

# Plot surface
maxValue, minValue = imageMaxMin(kernelLaplacian)
plotSurface(kernelLaplacian, [minValue, maxValue], 1)