Пример #1
0
def findLongestCentredSegmentinImage(imageName, gaussianKernelSize,
                                     sobelKernelSize, upperT, lowerT):
    # Read image into array and show
    inputImage, width, height = imageReadL(imageName)

    # Compute edges and find the segment in the image
    magnitude, _ = applyCannyEdgeDetector(inputImage, gaussianKernelSize,
                                          sobelKernelSize, upperT, lowerT)
    mainSegmentAverage = findLongestSegment(magnitude)

    # Compute centre
    numPoints = len(mainSegmentAverage)
    centre = [0, 0]
    for p in range(0, numPoints):
        centre[0] += (mainSegmentAverage[p])[0]
        centre[1] += (mainSegmentAverage[p])[1]
    centre[0] /= numPoints
    centre[1] /= numPoints

    # Respect to the center and convert to an image array
    shape = createImageF(numPoints, 2)
    for p in range(0, numPoints):
        y, x = (mainSegmentAverage[p])[0], (mainSegmentAverage[p])[1]
        shape[0, p] = y - centre[0]
        shape[1, p] = x - centre[1]

    return centre, shape, width, height
Пример #2
0
def findLongesSegmentinImage(imageName, gaussianKernelSize, sobelKernelSize,
                             upperT, lowerT):
    # Read image into array and show
    inputImage, width, height = imageReadL(imageName)

    # Compute edges and find the segment in the image
    magnitude, _ = applyCannyEdgeDetector(inputImage, gaussianKernelSize,
                                          sobelKernelSize, upperT, lowerT)
    mainSegmentAverage = findLongestSegment(magnitude)

    # Convert to an image array
    numPoints = len(mainSegmentAverage)
    shape = createImageF(numPoints, 2)
    for p in range(0, numPoints):
        y, x = (mainSegmentAverage[p])[0], (mainSegmentAverage[p])[1]
        shape[0, p] = y
        shape[1, p] = x

    return shape, width, height
    k = Second term constant    
    op = H for Harris operator 
         M for minimum direction
'''
pathToDir = "../../Images/Chapter4/Input/"
imageName = "Shapes.png"
GaussianKernelSize = 7
sobelKernelSize = 3
upperT = 0.4
lowerT = 0.2
kernelSize = 9
k = .02
op = "H"

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

# Show input image
showImageL(inputImage)

# We apply Canny to obtain the edges from the image
# but also need the results of the Sobel operator (Gradient)
magnitude, angle, mX, mY = applyCannyEdgeDetector(inputImage, 
                                GaussianKernelSize, sobelKernelSize, upperT, lowerT, True)        \

# The center of the kernel
kernelCentre = int((kernelSize - 1) / 2)

# Compute curvature
curvature = createImageF(width, height)
for x,y in itertools.product(range(0, width), range(0, height)):
'''
Parameters:
    pathToDir = Input image directory
    imageName = Input image name
    templateName = Input template image name
    thresholdVal = Only pixels in the template with value greater that this are used
                   -1 to use all pixels or 0  to use edges with value >0
'''
pathToDir = "../../Images/Chapter5/Input/"
imageName = "Eye.png"
templateName = "EyeTemplate.png"
thresholdVal = -1 

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

# Show input image and template
showImageL(inputImage)
showImageL(templateImage)

# Create an accumulator. We look in a reduced size image
accumulator = createImageF(width, height)
  
# Template matching
templateCentreX = int((widthTemplate - 1) / 2)
templateCentreY = int((heightTemplate - 1) / 2)
for x in range(0, width):  
    printProgress(x, width)
    for y in range(0, height):
Пример #5
0
    pathToDir = Input image directory
    imageName = Input image name
    numPixels = Number of super pixels per row
    m = Proximity constant
    numIter =  Number of iterations
'''

pathToDir = "../../Images/Chapter8/Input/"
imageName = "fish.png"
numPixels = 700
m = 10.0
numIter = 10

# Read image into array and show
inputImage, width, height = imageReadRGB(pathToDir + imageName)
inputImageL, _, _ = imageReadL(pathToDir + imageName)
showImageRGB(inputImage)

# Gradient used to determine initial positions
sobelX, sobelY = createSobelKernel(3)
normalizeMagnitude = False
gradient, _, _, _ = applyKernelMA(inputImageL, sobelX, sobelY,
                                  normalizeMagnitude)

# Determine the number of regions in horizontal and vertical
regionSide = int(sqrt(width * height / numPixels))
if regionSide % 2 == 0: regionSide -= 1
halfRegionSide = (regionSide - 1.0) / 2.0
regW, regH = 1 + int(width / regionSide), 1 + int(height / regionSide)

# Image to store the region colour and other image for position
Пример #6
0
from timeit import itertools
'''
Parameters:
    pathToDir = Input image directory
    imageName = Input image name
    maskName = Mask image name
    transformationType  = Similarity, Affine, Homography 
'''
pathToDir = "../../Images/Chapter10/Input/"
imageName = "cube1.png"
maskName = "mask1.png"
transformationType = "Homography"

# Read image
inputImage, width, height = imageReadRGB(pathToDir + imageName)
maskImage, width, height = imageReadL(pathToDir + maskName)
showImageRGB(inputImage)

# Image cente
centreX, centreY = width / 2, height / 2

# Perform transformation
if transformationType == "Similarity":
    # Similarity transformation
    s = [.4, 0.8, 0.8, 100.0, 0.0]  # Angle, scaleXY, translationXY
    T = [[ s[1]*cos(s[0]), s[1]*sin(s[0]), s[3]],                        \
         [ -s[2]*sin(s[0]), s[2]*cos(s[0]), s[4]],                       \
         [0 ,0, 1]]
if transformationType == "Affine":
    # Affine transformation
    T = [[ .8, .1, 100],                                                  \
Parameters:
    pathToDir = Input image directory
    imageName = Input image name
    kernelSize = Size of the kernel 
    maxDisp = Maximum size of displacement 
    step = Delta that defines the image sample positions used to obtain optical flow
'''
pathToDir = "../../Images/Chapter4/Input/"
image1Name = "Rino0.png"
image2Name = "Rino1.png"
kernelSize = 11
maxDisp = 10
step = 10

# Read image into array. both images must have same size
inputImage1, width, height = imageReadL(pathToDir + image1Name)
inputImage2, _, _  = imageReadL(pathToDir + image2Name)

# Show input image
showImageL(inputImage1)
showImageL(inputImage2)

# The center of the kernel
kernelCentre = int((kernelSize - 1) / 2)

# Compute Motion in sampled points
motionMagnitude = createImageF(width, height)
motionDirection = createImageF(width, height)
motionWeight = createImageF(width, height)
for x,y in itertools.product(range(2 * step, width-2*step, step),                 \
                             range(2 * step, height-2*step,step)):
Пример #8
0
Parameters:
    pathToDir = Input image directory
    imageName = Input image name
    p = Polynomial parameter. 0.5 for centralized polynomials. Power
    numMoments = Number of moments
    background = The gray level range of the background pixels
'''
pathToDir = "../../Images/Chapter7/Input/"
imageName = "f14.png"
numMoments = 4
p = 0.5
background = [200, 255]  # white background image
reducedSize = 80  # reduce the image size to avoid overflowing or use recurrence relations

# Read image into array and show
inputImage, inputWidth, inputHeight = imageReadL(pathToDir + imageName)

# Reduce the image size to avoid large exponents in the computation
scale = max(max(inputWidth, inputHeight) / float(reducedSize), 1.0)
width, height = int(inputWidth / scale), int(inputHeight / scale)
scaledImage = scaleImageL(inputImage, width, height)
showImageL(scaledImage)

# Get a list that contains the pixels of the shape in the form (y,x,v)
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)