def showShapeinImage(shape, centre, width, height): segmentsImage = createImageL(width, height) numPoints = len(shape[0]) for p in range(0, numPoints): y, x = int(centre[0] + shape[0, p]), int(centre[1] + shape[1, p]) if x > 0 and y > 0 and x < width and y < height: segmentsImage[y, x] = 255 showImageL(segmentsImage)
''' 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)): # If it is an edge if magnitude[y,x] > 0: A, B, C = 0.0, 0.0, 0.0
weightA, weightB = a - aInt, b - bInt accumulatorAxis[bInt, aInt, r] += (1.0 - weightA) + (1.0 - weightB) accumulatorAxis[bInt, aInt + 1, r] += weightA + (1.0 - weightB) accumulatorAxis[bInt + 1, aInt, r] += (1.0 - weightA) + weightB accumulatorAxis[bInt + 1, aInt + 1, r] += weightA + weightB # Find maximum and plot accumulator maximumAxis = imageArgMax(accumulatorAxis) plot3DHistogram(accumulatorAxis[:, :, maximumAxis[2]]) # Draw ellipse on an output image outputImage = createScaleImageL(inputImage, 0.5) rotAngle = ((maximumAxis[2] + angleRange[0]) * pi) / 180.0 a, b = maximumAxis[1] + axisRange[0], maximumAxis[0] + axisRange[0] #print(a, b) for m in range(0, 360): angle = (m * pi) / 180.0 x = int(maximumPos[1] + a * cos(angle) * cos(rotAngle) - b * sin(angle) * sin(rotAngle)) y = int(maximumPos[0] + a * cos(angle) * sin(rotAngle) + b * sin(angle) * cos(rotAngle)) if x < width and x > 0 and y < height and y > 0: outputImage[y, x] = 255 showImageL(outputImage)
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): for wx,wy in itertools.product(range(0, widthTemplate), range(0, heightTemplate)): posY = y + wy - templateCentreY posX = x + wx - templateCentreX
# Iteration from timeit import itertools ''' Parameters: pathToDir = Input image directory 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) # Create image to store the normalization outputNormalizedImage = createImageL(width, height) # Maximum and range maxVal, miniVal = imageMaxMin(inputImage) brightRange = float(maxVal - miniVal) # Set the pixels in the output image for x, y in itertools.product(range(0, width), range(0, height)): # Normalize the pixel value according to the range outputNormalizedImage[y, x] = round( (inputImage[y, x] - miniVal) * 255.0 / brightRange)
pathToDir = "../../Images/Chapter2/Input/" imageName = "Dandelion.png" # Read image into array inputImage, width, height = imageReadL(pathToDir + imageName) # Shift the image shiftDistance = int(width / 3); shiftImage = createImageL(width, height) for x,y in itertools.product(range(0, width), range(0, height)): xShift = (x - shiftDistance) % width shiftImage[y][x] = inputImage[y][xShift] # Show images showImageL(inputImage) showImageL(shiftImage) # Compute power and phase powerImage, phaseImage = computePowerandPhase(inputImage) powerShiftImage, phaseShiftImage = computePowerandPhase(shiftImage) # Show power powerImageLog = imageLogF(powerImage) powerShiftImageLog = imageLogF(powerShiftImage) showImageF(powerImageLog) showImageF(powerShiftImageLog) # show phase showImageF(phaseImage) showImageF(phaseShiftImage)
from timeit import itertools ''' Parameters: pathToDir = Input image directory imageName = Input image name intevalSize = Define the sawtooth fixed interval size ''' pathToDir = "../../Images/Chapter3/Input/" imageName = "Horse.png" intevalSize = 64 # Read image into array inputImage, width, height = imageReadL(pathToDir + imageName) # Show input image showImageL(inputImage) # Create 3 images to store the result of 3 operators outputSawtoothImage = createImageL(width, height) outputLogarithmicImage = createImageL(width, height) outputExponentialImage = createImageL(width, height) # Set the pixels in the output image for x, y in itertools.product(range(0, width), range(0, height)): inputValue = int(inputImage[y, x]) # Set the pixels in the sawtooth image pixelInInterval = inputValue % intevalSize gain = float(pixelInInterval) / float(intevalSize) outputSawtoothImage[y, x] = inputValue * gain
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)): minDiference, nextDiference = float("inf"), float("inf") mDisp = [0,0] for dx,dy in itertools.product(range(-maxDisp, maxDisp), \
Parameters: pathToDir = Input image directory imageName = Input image name kernelSize = Size of the kernel sigma = Standard deviation of the kernel ''' pathToDir = "../../Images/Chapter4/Input/" imageName = "Lizard.png" kernelSize = 12 sigma = 2 # Read image into array inputImage, width, height = imageReadL(pathToDir + imageName) # Show input image showImageL(inputImage) # Create Kernel kernelLaplacian = createLaplacianKernel(kernelSize, sigma) # Apply kernel gaussianImage = applyKernelF(inputImage, kernelLaplacian) # Zero-crossing detector edges = createImageL(width, height) kernelCentre = int((kernelSize - 1) / 2) for x, y in itertools.product(range(1, width - 1), range(1, height - 1)): quadrantValue = [0.0, 0.0, 0.0, 0.0] for wx, wy in itertools.product(range(-1, 1), range(-1, 1)): quadrantValue[0] += gaussianImage[y + wy, x + wx]
''' 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) # 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]
threshold = Threshold value quiverSample = Distance between arrows in the quiver plot. Increase to have less arrows quiverScale = Scale of arrows in the quiver plot. Increase to make arrows smaller ''' pathToDir = "../../Images/Chapter4/Input/" imageName = "Zebra.png" kernelSize = 5 threshold = 4000 quiverSample = 5 quiverScale = 500 # Read image into array inputImage, width, height = imageReadL(pathToDir + imageName) # Show input image showImageL(inputImage) # Create images to store the result outputMagnitude = createImageF(width, height) outputDirection = createImageF(width, height) # Create Kernel sobelX, sobelY = createSobelKernel(kernelSize) # The center of the kernel kernelCentre = int((kernelSize - 1) / 2) # Convolution with two kernels for x, y in itertools.product(range(0, width), range(0, height)): mX, wX, mY, wY = 0.0, 0.0, 0.0, 0.0 for wx, wy in itertools.product(range(0, kernelSize), range(0,