def reconstruction(coefficients): # Maximum frequency maxFrequencyH = int((len(coefficients) - 1) / 2) maxFrequencyW = int((len(coefficients[0]) - 1) / 2) height = 2 * maxFrequencyH width = 2 * maxFrequencyW # Adjust the size of the data to be even m = float(width) n = float(height) if width % 2 == 0: m = width + 1 if height % 2 == 0: n = height + 1 # Fundamental frequency ww = (2.0 * pi) / float(m) wh = (2.0 * pi) / float(n) reconstructionImage = createImageF(m, n) for x in range(0, width): printProgress(x, width - 1) for y in range(0, height): for kw,kh in itertools.product(range(-maxFrequencyW, maxFrequencyW + 1), \ range(-maxFrequencyH, maxFrequencyH + 1)): indexInArrayW = kw + maxFrequencyW indexInArrayH = kh + maxFrequencyH reconstructionImage[y,x] += \ (coefficients[indexInArrayH, indexInArrayW][0] / (m*n)) * (cos(x * ww * kw) * cos(y * wh * kh) - sin(x * ww * kw) * sin(y * wh * kh)) + \ (coefficients[indexInArrayH, indexInArrayW][1] / (m*n)) * (cos(x * ww * kw) * sin(y * wh * kh) + sin(x * ww * kw) * cos(y * wh * kh)) return reconstructionImage
def computeCoefficients(inputImage): height = len(inputImage) width = len(inputImage[0]) # Create coefficients Image. Two floats to represent a complex number # Maximum frequency according to sampling maxFrequencyW = int(width / 2) maxFrequencyH = int(height / 2) numCoefficientsW = 1 + 2 * maxFrequencyW numCoefficientsH = 1 + 2 * maxFrequencyH coefficients = createImageF(numCoefficientsW, numCoefficientsH, 2) # Adjust the size of the data to be even m = float(width) n = float(height) if width % 2 == 0: m = width + 1 if height % 2 == 0: n = height + 1 # Fundamental frequency ww = (2.0 * pi) / float(m) wh = (2.0 * pi) / float(n) # Compute values for kw in range(-maxFrequencyW, maxFrequencyW + 1): printProgress(kw + maxFrequencyW, numCoefficientsW - 1) for kh in range(-maxFrequencyH, maxFrequencyH + 1): indexInArrayW = kw + maxFrequencyW indexInArrayH = kh + maxFrequencyH for x, y in itertools.product(range(0, width), range(0, height)): coefficients[indexInArrayH, indexInArrayW][0] += inputImage[y, x] * ( cos(x * ww * kw) * cos(y * wh * kh) - sin(x * ww * kw) * sin(y * wh * kh)) coefficients[indexInArrayH, indexInArrayW][1] += inputImage[y, x] * ( cos(x * ww * kw) * sin(y * wh * kh) + sin(x * ww * kw) * cos(y * wh * kh)) for kw in range(-maxFrequencyW, maxFrequencyW + 1): for kh in range(-maxFrequencyH, maxFrequencyH + 1): coefficients[indexInArrayH, indexInArrayW][0] /= (m * n) coefficients[indexInArrayH, indexInArrayW][1] /= (m * n) return coefficients, maxFrequencyW, maxFrequencyH
def computePowerfromCoefficients(coefficients): # Maximum frequency maxFrequencyH = int((len(coefficients) - 1) / 2) maxFrequencyW = int((len(coefficients[0]) - 1) / 2) # Power powerImage = createImageF(1 + 2 * maxFrequencyW, 1 + 2 * maxFrequencyH) for kw in range(-maxFrequencyW, maxFrequencyW + 1): printProgress(kw + maxFrequencyW, 2 * maxFrequencyW) for kh in range(-maxFrequencyH, maxFrequencyH + 1): indexInArrayW = kw + maxFrequencyW indexInArrayH = kh + maxFrequencyH powerImage[indexInArrayH, indexInArrayW] = sqrt(coefficients[indexInArrayH, indexInArrayW][0] * coefficients[indexInArrayH, indexInArrayW][0] + \ coefficients[indexInArrayH, indexInArrayW][1] * coefficients[indexInArrayH, indexInArrayW][1]) return powerImage
# 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 # The threshold is used to accumulate only the edge pixels in an edge template # The difference of pixel values is inverted to show the best match as a peak if posY > -1 and posY < height and posX > -1 and posX < width and \ templateImage[wy,wx] > thresholdVal: diff = 1.0 - abs(float(inputImage[posY,posX]) - \ float(templateImage[wy, wx])) / 255.0 accumulator[y,x] += diff*diff # Show accumulator within a maxima and mininma region
colors = [0, 0, 0] npts = 0.0 for wx,wy in itertools.product(range(rx, rx + regionSide), \ range(ry, ry + regionSide)): if wy >= 0 and wy < height and wx >= 0 and wx < width: if (wy < height and wx < width): regionsID[wy, wx] = [y, x] colors += inputImage[wy, wx] npts += 1 if npts > 0: regionColour[y,x] = [int(colors[0] / npts), \ int(colors[1] / npts), int(colors[2] / npts)] # Modify regions for itr in range(0, numIter): printProgress(itr, numIter) # Values for new regions newRegionColour = createImageF(regW, regH, 3) newRegionPos = createImageUV(regW, regH) newRegionSize = createImageF(regW, regH) # Per pixel for x, y in itertools.product(range(0, width), range(0, height)): ry, rx = regionsID[y, x] colour = [ float(inputImage[y, x][0]), float(inputImage[y, x][1]), float(inputImage[y, x][2]) ] minD = [FLT_MAX, ry, rx] for wx, wy in itertools.product(range(rx - 2, rx + 3),
# The number of times the region has grown, its size timeRegions = {} sizeRegions = {} incSizeRegions = {} # Regions regionsImage = createImageF(width, height) # Stable regions resultImage = createImageF(width, height) # Use a threshold to flood regions nextRegionID = 1 for threshold in range(startL, endL, incL): printProgress(threshold - startL, endL - startL) # Init the change in size for regionID in incSizeRegions: incSizeRegions[regionID] = 0 # Repeatedly flood the image to grow regions flooded = True while flooded: flooded = False growRegion = [] # For each non-region pixels for x, y in itertools.product(range(0, width), range(0, height)): if inputImage[y, x] <= threshold and regionsImage[y, x] == 0: # List of neighbours
# Adjust the size of the data to be even m = float(width) n = float(height) if width % 2 == 0: m = width + 1.0 if height % 2 == 0: n = height + 1.0 # Fundamental frequency ww = (2.0 * pi) / m wh = (2.0 * pi) / n # Compute coefficients for kw in range(-maxFrequencyW, maxFrequencyW + 1): printProgress(kw + maxFrequencyW, numCoefficientsW) indexInArrayW = kw + maxFrequencyW for kh in range(-maxFrequencyH, maxFrequencyH + 1): indexInArrayH = kh + maxFrequencyH for x, y in itertools.product(range(0, width), range(0, height)): coefficients[indexInArrayH, indexInArrayW][0] += inputImage[y,x] * \ (cos(x * ww * kw) * cos(y * wh * kh) - sin(x * ww * kw) * sin(y * wh * kh)) coefficients[indexInArrayH, indexInArrayW][1] += inputImage[y,x] * \ (cos(x * ww * kw) * sin(y * wh * kh) + sin(x * ww * kw) * cos(y * wh * kh)) for kw in range(-maxFrequencyW, maxFrequencyW + 1): printProgress(kw + maxFrequencyW, numCoefficientsW) indexInArrayW = kw + maxFrequencyW for kh in range(-maxFrequencyH, maxFrequencyH + 1): indexInArrayH = kh + maxFrequencyH coefficients[indexInArrayH, indexInArrayW][0] *= m * n
# Adjust the size of the data to be even m = float(width) n = float(height) if width % 2 == 0: m = width + 1.0 if height % 2 == 0: n = height + 1.0 # Fundamental frequency ww = (2.0 * pi) / m wh = (2.0 * pi) / n # Compute values for u in range(-maxFreqW, maxFreqW + 1): printProgress(u + maxFreqW, numCoeffW) entryW = u + maxFreqW for v in range(-maxFreqH, maxFreqH + 1): entryH = v + maxFreqH for x, y in itertools.product(range(0, width), range(0, height)): coeff[entryH, entryW] += inputImage[y,x] * \ (cos(x * ww * u) + sin(x * ww * u)) * (cos(y * wh * v) + sin(y * wh * v)) # Include scale for u in range(-maxFreqW, maxFreqW + 1): printProgress(u + maxFreqW, numCoeffW) entryW = u + maxFreqW for v in range(-maxFreqH, maxFreqH + 1): entryH = v + maxFreqH coeff[entryH, entryW] /= m * n