def applyKernel(inputImage, kernelImage): height = len(inputImage) width = len(inputImage[0]) kernelHeight = len(kernelImage) kerelWidth = len(kernelImage[0]) kernelCentreY = int((kernelHeight - 1) / 2) kernelCentreX = int((kerelWidth - 1) / 2) # Create images to store the result outputImage = createImageL(width, height) for x, y in itertools.product(range(0, width), range(0, height)): sumKernel = 0 sumKernelWeights = 0 for wx, wy in itertools.product(range(0, kerelWidth), range(0, kernelHeight)): posY = y + wy - kernelCentreY posX = x + wx - kernelCentreX if posY > -1 and posY < height and posX > -1 and posX < width: sumKernel += float(inputImage[posY, posX]) * kernelImage[wy, wx] sumKernelWeights += kernelImage[wy, wx] if sumKernelWeights > 0: outputImage[y, x] = sumKernel / sumKernelWeights return outputImage
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)
def thresholdImage(inputImage, threshold, binary = True): height = len(inputImage) width = len(inputImage[0]) # Create images to store the result outputImage = createImageL(width, height) # Set the pixels in the output image according to the accumulate histogram for x,y in itertools.product(range(0, width), range(0, height)): if inputImage[y,x] > threshold: if binary: outputImage[y,x] = 255 else: outputImage[y,x] = inputImage[y,x] else: outputImage[y,x] = 0 return outputImage
pathToDir = Input image directory imageName = Input image name kernelSize = Size of the kernel ''' pathToDir = "../../Images/Chapter3/Input/" imageName = "Fence.png" kernelSize = 5 # Read image into array inputImage, width, height = imageReadL(pathToDir + imageName) # Show input image showImageL(inputImage) # Create images to store the result outputImage = createImageL(width, height) # Apply filter kernelCentre = int((kernelSize - 1) / 2) for x,y in itertools.product(range(0, width), range(0, height)): region = [ ] for wx,wy in itertools.product(range(0, kernelSize), range(0, kernelSize)): posY = y + wy - kernelCentre posX = x + wx - kernelCentre if posY > -1 and posY < height and posX > -1 and posX < width: region.append(inputImage[posY,posX]) numPixels = len(region) if numPixels > 0:
''' 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) # Compute histogram histogramNormalizedImage = computeHistogram(outputNormalizedImage)
from timeit import itertools ''' Parameters: pathToDir = Input image directory imageName = Input image name ''' 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)
imageName = Input image name kernelSize = Size of the kernel ''' pathToDir = "../../Images/Chapter3/Input/" imageName = "Logs.png" kernelSize = 5 # Read image into array inputImage, width, height = imageReadL(pathToDir + imageName) # Show input image showImageL(inputImage) # Create Kernel kernelCentre = int((kernelSize - 1) / 2) kernelImage = createImageL(kernelSize, kernelSize) # Set the pixels of a flat kernel for x in range(0, kernelSize): for y in range(0, kernelSize): kernelImage[y, x] = 1 # Create images to store the result outputImage = createImageL(width, height) # Apply kernel kernelCentre = int((kernelSize - 1) / 2) for x, y in itertools.product(range(0, width), range(0, height)): maxValue = 0 for wx, wy in itertools.product(range(0, kernelSize), range(0,
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 # Set the pixels in the Logarithmic outputLogarithmicImage[y, x] = 20 * log(inputValue * 100.0)
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] for wx, wy in itertools.product(range(-1, 1), range(0, 2)): quadrantValue[1] += gaussianImage[y + wy, x + wx] for wx, wy in itertools.product(range(0, 2), range(-1, 1)): quadrantValue[2] += gaussianImage[y + wy, x + wx] for wx, wy in itertools.product(range(0, 2), range(0, 2)): quadrantValue[3] += gaussianImage[y + wy, x + wx]