def fft_display(im, videoFilename=None): rows = im.shape[0] cols = im.shape[1] template = np.zeros((rows, cols)) # Generates the FFt FFT = np.fft.fft2(im) FFT = np.fft.fftshift(FFT) logFFT = np.log10(np.abs(FFT)) logFFT = (logFFT/np.max(logFFT)) * 255 # Creates array to poulate with max values maxValues = np.flipud(np.argsort(logFFT.flatten())) used = template.copy() current = template.copy() scaled = template.copy() summed = template.copy() # creates writer and image window cv2.namedWindow(videoFilename) writer = video_writer(im.shape,videoFilename) for i in maxValues: # puts freq into used array used.flat[i] = logFFT.flat[i] # returns spatial sine wave for freq template.flat[i] = logFFT.flat[i] current = np.fft.ifft2(template) template.flat[i] = 0 #scales the current array and sums all freqs scaled = ((current - np.min(current))/ np.max(current)) *255 summed = summed + current # stitching all images together frame = stitch(im,logFFT,used,current,scaled,summed) if writer.isOpened(): writer.write(frame) # shows the image cv2.imshow(videoFilename,frame) action = ipcv.flush() if action == "pause": action = ipcv.flush() if action == "pause": continue elif action == "exit": writer.release() sys.exit()
def dft2(f, scale=True, method='break', verbose=False): #initialize variables shape = f.shape j = complex(0, 1) e = math.exp(1) M = shape[0] N = shape[1] pi = math.pi f_x_y = numpy.zeros((shape), dtype=numpy.complex128) dst = numpy.zeros(shape, dtype=numpy.complex128) # checkerboard image to center for x in range(0, shape[0]): for y in range(0, shape[1]): f[x, y] = f[x, y] * pow(-1, (x + y)) #now compute fourier transform if method == 'together': #discrete, four-loop method for u in range(0, shape[0]): for v in range(0, shape[1]): for x in range(0, shape[0]): for y in range(0, shape[1]): f_x_y[x, y] = f[x, y] * (e**(-j * 2 * pi * (((u * x) / M) + ((v * y) / N)))) if scale == True: dst[u, v] = sum(sum(f_x_y)) / (M * N) else: dst[u, v] = sum(sum(f_x_y)) if method == 'break': #modified, less computationally-heavy method f_x_v = numpy.zeros(shape, dtype=numpy.complex128) for x in range( 0, M ): #use DFT algorithm twice, computing rows first, then columns. f_x_v[x, :] = dft(f[x, :], scale=scale, shift=False, verbose=False) for y in range(0, N): dst[:, y] = dft(f_x_v[:, y], scale=scale, shift=False, verbose=False) # Optional display method if verbose == True: f_complex = dst[:, :] + 1j * dst[:, :] f_abs = numpy.abs(f_complex) + 1 # lie between 1 and 1e6 f_bounded = 20 * numpy.log(f_abs) f_img = 255 * f_bounded / numpy.max(f_bounded) f_img = f_img.astype(numpy.uint8) cv2.imshow('F(z) of Source Image', f_img) ipcv.flush() return dst else: return dst
def otsu_threshold(im, maxCount=255, verbose=False): hist = cv2.calcHist([im], [0], None, [maxCount+1], [0, maxCount+1]) firstIndex = np.min(im) lastIndex = np.max(im) print(firstIndex) print(lastIndex) r = np.zeros(maxCount+1) for i in range(65, 206): threshold = np.argmax(r) threshIm = np.ones(np.shape(im)) threshIm = threshIm * im np.place(threshIm, im < threshold, 0) np.place(threshIm, im >= threshold, 1) return (threshIm, threshold) if __name__ == '__main__': import cv2 import ipcv import os.path import time home = os.path.expanduser('~') filename = home + os.path.sep + 'src/python/examples/data/giza.jpg' im = cv2.imread(filename, cv2.IMREAD_UNCHANGED) print('Filename = {0}'.format(filename)) print('Data type = {0}'.format(type(im))) print('Image shape = {0}'.format(im.shape)) print('Image size = {0}'.format(im.size)) startTime = time.time() thresholdedImage, threshold = ipcv.otsu_threshold(im, verbose=True) print('Elapsed time = {0} [s]'.format(time.time() - startTime)) print('Threshold = {0}'.format(threshold)) cv2.namedWindow(filename, cv2.WINDOW_AUTOSIZE) cv2.imshow(filename, im) cv2.namedWindow(filename + ' (Thresholded)', cv2.WINDOW_AUTOSIZE) cv2.imshow(filename + ' (Thresholded)', thresholdedImage * 255) action = ipcv.flush()
def display(img,name='test'): cv2.namedWindow(name,cv2.WINDOW_AUTOSIZE) cv2.imshow(name,img) ipcv.flush()
maxCount = numpy.max(blankSheet) if (numRowsB-numColsB) % 2 != 0: blankSheet = numpy.pad(blankSheet, ((0,0),(pad1,pad1+1)), 'constant', constant_values=((maxCount, maxCount),(maxCount,maxCount))) elif (numRowsA-numColsA) % 2 == 0: blankSheet = numpy.pad(blankSheet, ((0,0),(pad1,pad1)), 'constant', constant_values=((maxCount, maxCount),(maxCount,maxCount))) return answerSheet, blankSheet if __name__ == '__main__': #answerSheet1 = cv2.imread('gray0001.tif') answerSheet1 = cv2.imread('black0007.tif') #############FOR HIGH RES############## #blankSheet1 = cv2.imread('original300dpi.tif') #############FOR LOW RES############## blankSheet1 = cv2.imread('original.tif') answerSheet, blankSheet = paddingAnswers(answerSheet1, blankSheet1) ''' cv2.namedWindow('Fiducial1 Padded', cv2.WINDOW_AUTOSIZE) cv2.imshow('Fiducial1 Padded', fiducial1.astype(numpy.uint8)) cv2.waitKey() cv2.namedWindow('Answer Sheet', cv2.WINDOW_AUTOSIZE) cv2.imshow('Answer Sheet', fiducial3) cv2.imwrite('fiducial1.tif', fiducial1) cv2.imwrite('fiducial2.tif', fiducial2) cv2.imwrite('fiducial3.tif', fiducial3) cv2.imwrite('answerSheet1.tif', answerSheet) ''' action = ipcv.flush()
import time home = os.path.expanduser('~') filename = home + os.path.sep + 'src/python/examples/data/crowd.jpg' filename = home + os.path.sep + 'src/python/examples/data/lenna.tif' #filename = home + os.path.sep + 'src/python/examples/data/redhat.ppm' #filename = home + os.path.sep + 'src/python/examples/data/lenna_color.tif' src = cv2.imread(filename) map1, map2 = ipcv.map_rotation_scale(src, rotation=30, scale=[1.3, 0.8]) startTime = time.clock() dst = ipcv.remap(src, map1, map2, interpolation=ipcv.INTER_NEAREST, borderMode=ipcv.BORDER_CONSTANT, borderValue=0) elapsedTime = time.clock() - startTime print('Elapsed time (remap) = {0} [s]'.format(elapsedTime)) srcName = 'Source (' + filename + ')' cv2.namedWindow(srcName, cv2.WINDOW_AUTOSIZE) cv2.imshow(srcName, src) dstName = 'Destination (' + filename + ')' cv2.namedWindow(dstName, cv2.WINDOW_AUTOSIZE) cv2.imshow(dstName, dst) ipcv.flush()
home = os.path.expanduser('~') filename = home + os.path.sep + 'src/python/examples/data/redhat.ppm' filename = home + os.path.sep + 'src/python/examples/data/crowd.jpg' filename = home + os.path.sep + 'src/python/examples/data/checkerboard.tif' filename = home + os.path.sep + 'src/python/examples/data/lenna.tif' src = cv2.imread(filename, cv2.IMREAD_UNCHANGED) dstDepth = ipcv.IPCV_8U #kernel = numpy.asarray([[-1,-1,-1],[-1,9,-1],[-1,-1,-1]]) # offset = 0 # kernel = numpy.asarray([[-1,-1,-1],[-1,8,-1],[-1,-1,-1]]) # offset = 128 # kernel = numpy.ones((15,15)) # offset = 0 kernel = numpy.asarray([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) offset = 0 startTime = time.time() dst = ipcv.filter2D(src, dstDepth, kernel, delta=offset) print('Elapsed time = {0} [s]'.format(time.time() - startTime)) cv2.namedWindow(filename, cv2.WINDOW_AUTOSIZE) cv2.imshow(filename, src) cv2.namedWindow(filename + ' (Filtered)', cv2.WINDOW_AUTOSIZE) cv2.imshow(filename + ' (Filtered)', dst) action = ipcv.flush()
def fft_display(img, videoFilename=None): try: img = img.copy() dims = ipcv.dimensions(img) r, c = dims["rows"], dims["cols"] temp = np.zeros((r, c)) # generating FFT # FFT = np.fft.fftshift(np.fft.fft(img)) # FFT = np.fft.fftshift( np.fft.fft2(img) ) # logFFT = np.log( np.abs( FFT / np.sqrt(FFT.size) ) ).astype(ipcv.IPCV_8U) FFT = np.fft.fft2(img) # print("AVERAGE VALUE IS EQUAL TO:",FFT.flat[0]) FFT = np.fft.fftshift(FFT) FFT = np.abs(FFT) logFFT = np.log10(FFT) logFFT = (logFFT / np.max(logFFT)) * 255 print("MAX IS EQUAL TO:", np.max(logFFT)) print("MIN IS EQUAL TO:", np.min(logFFT)) # creating array to poulate with maximum values maximumValues = np.flipud(np.argsort(logFFT.flatten())) used = temp.copy() current = temp.copy() currentScaled = temp.copy() summed = temp.copy() #creating writer and image window cv2.namedWindow(videoFilename) writer = create_video_writer(img.shape, videoFilename) for freqIndex in maximumValues: # for index in np.arange(0,maximumValues.size,2) # puting frequency in 'used' array used.flat[freqIndex] = logFFT.flat[freqIndex] # returning the spatial sine wave for freq temp.flat[freqIndex] = logFFT.flat[freqIndex] current = np.fft.ifft2(temp) temp.flat[freqIndex] = 0 print("MAX IS EQUAL TO:", np.max(current)) print("MIN IS EQUAL TO:", np.min(current)) #scaling the current currentScaled = ( (current - np.min(current)) / np.max(current)) * 255 #summing up all the freq summed = summed + current #stiching all images together frame = stich(img, logFFT, used, current, currentScaled, summed) print("frame dataType =", frame.dtype) #writing frame if writer.isOpened(): writer.write(frame) #displaying image cv2.imshow(videoFilename, frame) action = ipcv.flush() if action == "pause": action = ipcv.flush() if action == "pause": continue elif action == "exit": writer.release() sys.exit() except Exception as e: ipcv.debug(e)
import ipcv import os.path import time home = os.path.expanduser('~') filename = home + os.path.sep + 'src/python/examples/data/crowd.jpg' filename = home + os.path.sep + 'src/python/examples/data/lenna.tif' src = cv2.imread(filename) startTime = time.clock() map1, map2 = ipcv.map_rotation_scale(src, rotation=30, scale=[1.3, 0.8]) #map1, map2 = ipcv.map_rotation_scale(src, rotation=0, scale=[2.0, 2.0]) elapsedTime = time.clock() - startTime print('Elapsed time (map creation) = {0} [s]'.format(elapsedTime)) startTime = time.clock() dst = cv2.remap(src, map1, map2, cv2.INTER_NEAREST) # dst = ipcv.remap(src, map1, map2, ipcv.INTER_NEAREST) elapsedTime = time.clock() - startTime print('Elapsed time (remapping) = {0} [s]'.format(elapsedTime)) srcName = 'Source (' + filename + ')' cv2.namedWindow(srcName, cv2.WINDOW_AUTOSIZE) cv2.imshow(srcName, src) dstName = 'Destination (' + filename + ')' cv2.namedWindow(dstName, cv2.WINDOW_AUTOSIZE) cv2.imshow(dstName, dst) ipcv.flush()
def character_recognition(src, templates, threshold, filterType='spatial', verbose=False): """ Function to determine the number of characters (for each character) that exist in an image. Author: Jesse Jurman (jrj2703) Args: src (array of ints): image to read charcters from templates (array of arrays): images for each character to be read in threshold (float): how strong the response needs to be before it is considered a match filterType (string): type of filter to run with images. defaults to 'spatial', can be 'match' verbose (bool): plot character maps Returns: a list of the indicies for the templates, in the order that they appear on the source image a histogram (size of the array templates) Raises: ValueError: filterType of not spatial or matched """ isrc = invertImage(src) results = [] text = [] # text map is a mapping of x,y coords to letters # this will be sorted to retrieve the final text textMap = {} if (filterType == 'spatial'): # for every template, build a 2D map of where the template matches for character in templates: if (verbose): cv2.namedWindow('isrc', cv2.WINDOW_AUTOSIZE) cv2.imshow('isrc', isrc) if (verbose): cv2.namedWindow('character', cv2.WINDOW_AUTOSIZE) cv2.imshow('character', character) # invert the character icharacter = invertImage(character) # normalize template icharacter = icharacter.astype(np.float64) if (icharacter.sum() != 0): icharacter /= icharacter.sum() if (verbose): cv2.namedWindow('icharacter', cv2.WINDOW_AUTOSIZE) cv2.imshow('icharacter', icharacter) # find points using filter2D cloudMatch = cv2.filter2D(src, -1, icharacter) if (verbose): cv2.namedWindow('CloudMatch', cv2.WINDOW_AUTOSIZE) cv2.imshow('CloudMatch', cloudMatch) # make single points using np.where pointMatch = np.where(cloudMatch >= threshold, 0, 255) pointMatch = np.array(pointMatch, cloudMatch.dtype) if (verbose): cv2.namedWindow('PointMatch', cv2.WINDOW_AUTOSIZE) cv2.imshow('PointMatch', pointMatch) if (verbose): ipcv.flush() results.append((pointMatch/255).sum()) coords = np.where(pointMatch >= threshold) for coordInd in range(len(coords[0])): coordTuple = (coords[0][coordInd], coords[1][coordInd]) # we don't know what actual letter we're dealing with, # but we kinda know the index using the size of the results list # this will totally overwrite letters that sit in the same coord textMap[coordTuple] = len(results)-1 elif (filterType == 'matched'): # you can set the loading bar width here # make sure it's smaller than your window, otherwise it'll # print a LOT of newlines loadingBarWidth = 50 loading = ipcv.Bar(loadingBarWidth) load = 0 # iterate through our templates for character in templates: # update loading bar load += 1 loading.showBar(load/len(templates)) # image of matches resImage = np.zeros(src.shape) # size of 2d slices s = templates[0].shape arrayWidth = s[0]*s[1] # invert character icharacter = invertImage(character) # flatten template rcharacter = icharacter.flatten() # invert source isrc = invertImage(src) row = 0 for row in range(src.shape[0]-s[0]): for col in range(src.shape[1]-s[1]): # cut out a portion of the image, based on the row and column cut = isrc[row:row+s[0], col:col+s[1]] wideCut = cut.flatten() # manipulate the character numerator = np.vdot(rcharacter, wideCut) denominator = np.vdot(np.linalg.norm(rcharacter), np.linalg.norm(wideCut)) if (denominator != 0): resImage[row][col] = numerator/denominator loading.showBar(load/len(templates)) if (verbose): cv2.namedWindow('resImage', cv2.WINDOW_AUTOSIZE) cv2.imshow('resImage', resImage) finImage = np.where(resImage >= threshold, 255, 0) finImage = np.array(finImage, src.dtype) if (verbose): cv2.namedWindow('finImage', cv2.WINDOW_AUTOSIZE) cv2.imshow('finImage', finImage) if (verbose): ipcv.flush() results.append((finImage/255).sum()) coords = np.where(finImage >= threshold) for coordInd in range(len(coords[0])): coordTuple = (coords[0][coordInd], coords[1][coordInd]) # we don't know what actual letter we're dealing with, # but we kinda know the index using the size of the results list # this will totally overwrite letters that sit in the same coord textMap[coordTuple] = len(results)-1 else: raise ValueError("only filter types supported are spatial and matched") # sort the text by it's x and y value # print("TEXTMAP.KEYS()", textMap.keys()) sortedMapKeys = sorted(textMap.keys(), key=itemgetter(0,1)) # print("SORTEDMAPKEYS",sortedMapKeys) for smk in sortedMapKeys: text.append(textMap[smk]) return text, results
def character_recognition(src, templates, threshold, filterType='spatial', verbose=False): """ Function to determine the number of characters (for each character) that exist in an image. Author: Jesse Jurman (jrj2703) Args: src (array of ints): image to read charcters from templates (array of arrays): images for each character to be read in threshold (float): how strong the response needs to be before it is considered a match filterType (string): type of filter to run with images. defaults to 'spatial', can be 'match' verbose (bool): plot character maps Returns: a list of the indicies for the templates, in the order that they appear on the source image a histogram (size of the array templates) Raises: ValueError: filterType of not spatial or matched """ isrc = invertImage(src) results = [] text = [] # text map is a mapping of x,y coords to letters # this will be sorted to retrieve the final text textMap = {} if (filterType == 'spatial'): # for every template, build a 2D map of where the template matches for character in templates: if (verbose): cv2.namedWindow('isrc', cv2.WINDOW_AUTOSIZE) cv2.imshow('isrc', isrc) if (verbose): cv2.namedWindow('character', cv2.WINDOW_AUTOSIZE) cv2.imshow('character', character) # invert the character icharacter = invertImage(character) # normalize template icharacter = icharacter.astype(np.float64) if (icharacter.sum() != 0): icharacter /= icharacter.sum() if (verbose): cv2.namedWindow('icharacter', cv2.WINDOW_AUTOSIZE) cv2.imshow('icharacter', icharacter) # find points using filter2D cloudMatch = cv2.filter2D(src, -1, icharacter) if (verbose): cv2.namedWindow('CloudMatch', cv2.WINDOW_AUTOSIZE) cv2.imshow('CloudMatch', cloudMatch) # make single points using np.where pointMatch = np.where(cloudMatch >= threshold, 0, 255) pointMatch = np.array(pointMatch, cloudMatch.dtype) if (verbose): cv2.namedWindow('PointMatch', cv2.WINDOW_AUTOSIZE) cv2.imshow('PointMatch', pointMatch) if (verbose): ipcv.flush() results.append((pointMatch / 255).sum()) coords = np.where(pointMatch >= threshold) for coordInd in range(len(coords[0])): coordTuple = (coords[0][coordInd], coords[1][coordInd]) # we don't know what actual letter we're dealing with, # but we kinda know the index using the size of the results list # this will totally overwrite letters that sit in the same coord textMap[coordTuple] = len(results) - 1 elif (filterType == 'matched'): # you can set the loading bar width here # make sure it's smaller than your window, otherwise it'll # print a LOT of newlines loadingBarWidth = 50 loading = ipcv.Bar(loadingBarWidth) load = 0 # iterate through our templates for character in templates: # update loading bar load += 1 loading.showBar(load / len(templates)) # image of matches resImage = np.zeros(src.shape) # size of 2d slices s = templates[0].shape arrayWidth = s[0] * s[1] # invert character icharacter = invertImage(character) # flatten template rcharacter = icharacter.flatten() # invert source isrc = invertImage(src) row = 0 for row in range(src.shape[0] - s[0]): for col in range(src.shape[1] - s[1]): # cut out a portion of the image, based on the row and column cut = isrc[row:row + s[0], col:col + s[1]] wideCut = cut.flatten() # manipulate the character numerator = np.vdot(rcharacter, wideCut) denominator = np.vdot(np.linalg.norm(rcharacter), np.linalg.norm(wideCut)) if (denominator != 0): resImage[row][col] = numerator / denominator loading.showBar(load / len(templates)) if (verbose): cv2.namedWindow('resImage', cv2.WINDOW_AUTOSIZE) cv2.imshow('resImage', resImage) finImage = np.where(resImage >= threshold, 255, 0) finImage = np.array(finImage, src.dtype) if (verbose): cv2.namedWindow('finImage', cv2.WINDOW_AUTOSIZE) cv2.imshow('finImage', finImage) if (verbose): ipcv.flush() results.append((finImage / 255).sum()) coords = np.where(finImage >= threshold) for coordInd in range(len(coords[0])): coordTuple = (coords[0][coordInd], coords[1][coordInd]) # we don't know what actual letter we're dealing with, # but we kinda know the index using the size of the results list # this will totally overwrite letters that sit in the same coord textMap[coordTuple] = len(results) - 1 else: raise ValueError("only filter types supported are spatial and matched") # sort the text by it's x and y value # print("TEXTMAP.KEYS()", textMap.keys()) sortedMapKeys = sorted(textMap.keys(), key=itemgetter(0, 1)) # print("SORTEDMAPKEYS",sortedMapKeys) for smk in sortedMapKeys: text.append(textMap[smk]) return text, results