def templateMatching(self, test_image, template_image, \ charac_index, elasticity=16): test = utils.im2array(test_image) template = utils.im2array(template_image) Coord = np.array(charac_index) width = 5 # creating K from the template image A = Coord[:, 0] - width B = Coord[:, 0] + width C = Coord[:, 1] - width D = Coord[:, 1] + width ## r, c = Coord.shape row, col = test.shape ONE = np.ones(Coord.shape[0]) ZERO = np.zeros(Coord.shape[0]) A = np.maximum(A, ZERO) B = np.minimum(B, row * ONE - 1) C = np.maximum(C, ZERO) D = np.minimum(D, col * ONE - 1) NumCharac = Coord.shape[0] ABCD = np.array(np.zeros((NumCharac, 4))) ABCD[:, 0] = A ABCD[:, 1] = B ABCD[:, 2] = C ABCD[:, 3] = D errors = np.zeros(NumCharac) for i in xrange(NumCharac): a, b, c, d = ABCD[i, :] K = template[a:b + 1, c:d + 1] pre_error = 10 ** 6 increment = 2 for j in xrange(-elasticity, elasticity + increment, increment): for k in xrange(-elasticity, elasticity + increment, increment): ## print "i, j, k", i, j, k if a + j >= 0 and b + j + 1 <= row \ and c + k >= 0 and d + k + 1 <= col: test_sample = test[a + j:b + j + 1, c + k:d + k + 1] errors[i] = norm(test_sample - K, 2) if pre_error > errors[i]: pre_error = errors[i] newX_index = np.ceil((a + b) / 2 + j) newY_index = np.ceil((c + d) / 2 + k) Coord[i, :] = [newX_index, newY_index] errors[i] = pre_error # for debugging ## print "modification toggle" ## print "Errors = ", errors error = errors.sum() ## print "Error Sum: ", error return Coord, error
def matchingError(self, test_image, template_image, warped_index): test = utils.im2array(test_image) template = utils.im2array(template_image) ti = np.transpose(np.nonzero(test)) wi = warped_index NumPixel = ti.shape[0] ERROR = np.zeros((NumPixel, 1)) ERROR[:, 0] = test[ti[:, 0], ti[:, 1]] - template[wi[:, 0], wi[:, 1]] ERROR **= 2 Error_matching = np.sqrt(ERROR.sum()) return Error_matching
def LAT(img, threshold=None): '''Adaptive thresholding''' imArray = utils.im2array(img) # set default threshold if not specified if threshold is None: T=imArray.mean() else: T=threshold Tdiff=1 while(Tdiff): # Segment image into object and background G1_mask=np.array(imArray>T, dtype='uint8') # object segments mask G2_mask=np.array(imArray<=T, dtype='uint8') # background mask G1=G1_mask*imArray # object image pixel G2=G2_mask*imArray # background image pixel # A new threshold is created using the mean values of the two segments Tnew= (G1.mean()+G2.mean())/2 Tdiff=Tnew-T T=Tnew # set the new threshold as threshold # iterate until new threshold aproximately matches the old threshold if Tdiff<0.0002: Tdiff=0 G1=np.array(imArray>T, dtype='uint8') G1*=255 G, imG1 = utils.formatimage(G1) return imG1
def EEIM(TemplateImage, EdgeIntensity, EdgeMap): ''' EEIM is a subfunction for elastic edge intensity matching, which returns a matching error. ''' # initialization imArray = utils.im2array(TemplateImage) #row_size, column_size = imArray.shape EdgeMapN = EdgeMap.sum() elasticity = 4 NumCluster = 10 # K-means Clustering of EdgeIntensity EdgeCoord = np.transpose(np.nonzero(EdgeMap > 0)) [centroid, label] = clusters.kmeans2(EdgeCoord, NumCluster) # Partitioning of EdgeIntensity Cluster = np.array(np.zeros((label.size, 4))) Cluster[:, 0] = label Cluster[:, 1:3] = EdgeCoord selEI = [] for i in label: row, col = EdgeCoord[i] selEI.append(EdgeIntensity[row, col]) selEI = np.array(selEI) Cluster[:, -1] = selEI
def EEIM(TemplateImage, EdgeIntensity, EdgeMap): ''' EEIM is a subfunction for elastic edge intensity matching, which returns a matching error. ''' # initialization imArray = utils.im2array(TemplateImage) #row_size, column_size = imArray.shape EdgeMapN = EdgeMap.sum() elasticity = 4 NumCluster = 10 # K-means Clustering of EdgeIntensity EdgeCoord = np.transpose(np.nonzero(EdgeMap > 0)) [centroid, label] = clusters.kmeans2(EdgeCoord, NumCluster) # Partitioning of EdgeIntensity Cluster = np.array(np.zeros((label.size, 4))) Cluster[:,0] = label Cluster[:,1:3] = EdgeCoord selEI = [] for i in label: row,col = EdgeCoord[i] selEI.append(EdgeIntensity[row,col]) selEI = np.array(selEI) Cluster[:,-1] = selEI
def gaborwavelet(self, img): ''' Gabor wavelet (GW) filter (Is this working?) ''' imArray = utils.im2array(img) row_size, col_size = imArray.shape x = np.array(range(1, row_size + 1), dtype='float') x = x.reshape((row_size, 1)) y = np.array(range(1, col_size + 1), dtype='float') y = y.reshape((1, col_size)) orientations = [] for i in range(8): orientations.append(i * pi / 8) w = signal.freqz() # spatial frequency G = np.exp(-(x**2 + y**2) / (2 * imArray.std()**2)) GList = [] # image edge container for different directions for theta in orientations: wave_vector = x * np.cos(theta) + y * np.sin(theta) G *= np.cos(w * wave_vector) + 1j * np.sin(w * wave_vector) # Apply Gabor filter to image sigma = convolve.convolve2d(imArray, G) GList.append(sigma) # Get the imaginary part of GW # format the output image sigma, output_image = utils.formatimage(sigma) return output_image
def LAT(img, threshold=None): '''Adaptive thresholding''' imArray = utils.im2array(img) # set default threshold if not specified if threshold is None: T = imArray.mean() else: T = threshold Tdiff = 1 while (Tdiff): # Segment image into object and background G1_mask = np.array(imArray > T, dtype='uint8') # object segments mask G2_mask = np.array(imArray <= T, dtype='uint8') # background mask G1 = G1_mask * imArray # object image pixel G2 = G2_mask * imArray # background image pixel # A new threshold is created using the mean values of the two segments Tnew = (G1.mean() + G2.mean()) / 2 Tdiff = Tnew - T T = Tnew # set the new threshold as threshold # iterate until new threshold aproximately matches the old threshold if Tdiff < 0.0002: Tdiff = 0 G1 = np.array(imArray > T, dtype='uint8') G1 *= 255 G, imG1 = utils.formatimage(G1) return imG1
def gaborwavelet(self, img): ''' Gabor wavelet (GW) filter (Is this working?) ''' imArray = utils.im2array(img) row_size, col_size = imArray.shape x = np.array(range(1, row_size + 1), dtype='float') x = x.reshape((row_size, 1)) y = np.array(range(1, col_size + 1), dtype='float') y = y.reshape((1, col_size)) orientations = [] for i in range(8): orientations.append(i * pi / 8) w = signal.freqz()# spatial frequency G = np.exp(-(x ** 2 + y ** 2) / (2 * imArray.std()**2)) GList = [] # image edge container for different directions for theta in orientations: wave_vector = x * np.cos(theta) + y * np.sin(theta) G *= np.cos(w * wave_vector) + 1j * np.sin(w * wave_vector) # Apply Gabor filter to image sigma = convolve.convolve2d(imArray, G) GList.append(sigma) # Get the imaginary part of GW # format the output image sigma, output_image = utils.formatimage(sigma) return output_image
def xp01(test_image, db_path, character_index): ''' Experiment on ORL facedatabase using template matching on the given image ''' linedivide = '=' * 50 print '\n', linedivide print "Test image: ", test_image testClass = os.path.dirname(test_image) testClass = os.path.split(testClass)[-1] print "\nPerforming template matching on ORL facedatabase" matching_error = np.zeros(40) for i in range(40): # for 40 templates template_class = 's' + str(i + 1) # get template class template_image = os.path.join(db_path, template_class, '1.pgm') ## print "\nTemplate: ", template_image tm = templateMatching.templateMatching2() AC = tm.templateMatching(test_image, template_image, \ character_index[i])[0] ## print "warped feature point coordinate is " ## print AC ## ErrorSum[i] = error ## print "\nComputing coeffs" test = utils.im2array(test_image) test_index = np.transpose(np.nonzero(test)) A, B = tm.sinusoidalCoeffs(character_index[i], AC) ## print "A = ", A ## print "B = ", B ## print "\nComputing DX" DX = tm.sinusoidalWarping(A, B, test_index) ## print "DX = ", DX # compute template matching error matching_error[i] = tm.matchingError(test_image, template_image, DX) ## print "With", template_class, "matching error = ", matching_error[i] ## print "ErrorSum = ", ErrorSum ## ErrMin = ErrorSum.min() error_min = matching_error.min() MinErrorClass = np.nonzero(matching_error == error_min)[0][0] + 1 MinErrorClass = 's' + str(MinErrorClass) verdict = "\tIncorrect match." if MinErrorClass == testClass: verdict = "\tCorrect match!" linedivide = '-' * 50 print "\n", linedivide print "Match result:", verdict print "\nMin Error = ", error_min print "Min Error Class is", MinErrorClass print "True Class: ", testClass
def sobel(img, threshold=0, mode=None): imArray = utils.im2array(img) Gx=ndimage.sobel(imArray, axis=0) # the horizontal derivative approx. Gy=ndimage.sobel(imArray, axis=1) # the vertical derivative approx. G_mag = np.sqrt( Gx**2 + Gy**2) G_angle = np.arctan2(Gy, Gx) G_mag -= threshold G_mag, imG_mag = utils.formatimage(G_mag, mode=mode) return [G_mag, imG_mag]
def sobel_handson(img): imArray = utils.im2array(img) gx = np.array([[1, 0, -1], [2, 0, -2] , [1, 0, -1]], dtype='float') gy = gx.transpose() Gx = ndimage.convolve(imArray, gx) # the horizontal derivative approx. Gy = ndimage.convolve(imArray, gy) # the vertical derivative approx. G_mag = np.sqrt(Gx**2 + Gy**2) G_angle = np.arctan2(Gy, Gx) G_mag, output_image = utils.formatimage(G_mag) return [output_image]
def EdgeImage(img, threshold=0): imArray = utils.im2array(img) G_mag, imG_mag = sobels.sobel(img, threshold=threshold) row_size, col_size = G_mag.shape EdgeMap = np.zeros(imArray.shape) # thresholding and Edge map #EdgeMap = np.array(G_mag > threshold, dtype='uint8') for row in xrange(0,row_size-3): for col in xrange(0, col_size-3): if G_mag[row, col] > threshold: EdgeMap[row:row+3, col:col+3] = np.ones((3,3)) EdgeIntensity = imArray * EdgeMap EdgeIntensity, imEdgeIntensity = utils.formatimage(EdgeIntensity) return [EdgeIntensity, EdgeMap, imEdgeIntensity]
def EdgeImage(img, threshold=0): imArray = utils.im2array(img) G_mag, imG_mag = sobels.sobel(img, threshold=threshold) row_size, col_size = G_mag.shape EdgeMap = np.zeros(imArray.shape) # thresholding and Edge map #EdgeMap = np.array(G_mag > threshold, dtype='uint8') for row in xrange(0, row_size - 3): for col in xrange(0, col_size - 3): if G_mag[row, col] > threshold: EdgeMap[row:row + 3, col:col + 3] = np.ones((3, 3)) EdgeIntensity = imArray * EdgeMap EdgeIntensity, imEdgeIntensity = utils.formatimage(EdgeIntensity) return [EdgeIntensity, EdgeMap, imEdgeIntensity]
def sobelLAT(img): ''' Edge extraction of image using 4-directional Sobel edge operators with LAT ''' imArray = utils.im2array(img) # low pass filtering of input image LowPassFilter=np.array([[1., 2., 1.],[2., 4., 2.],[1., 2., 1.]]) LowPassFilter /= 16 imArray_lowpass=ndimage.convolve(imArray, LowPassFilter) # sobel edge operators z0=np.array([[1., 2., 1.], [0., 0., 0.], [-1., -2., -1]]) z1=np.array([[2., 1., 0.], [1., 0., -1.], [0., -1., -2.]]) z2=np.array([[1., 0., -1.], [2., 0., -2.], [1., 0., -1.]]) z3=np.array([[0., -1., -2.], [1., 0., -1.], [2., 1., 0.]]) SobelEdgeList=[z0, z1, z2, z3] G=np.array(np.zeros(imArray.shape)) for zk in SobelEdgeList: Gx = ndimage.convolve(imArray, zk) Gy = ndimage.convolve(imArray, zk.transpose()) Gnew = np.sqrt( Gx**2 + Gy**2 ) # keeping max values left_mask = np.array( Gnew >= G, dtype='uint8') right_mask = np.array( left_mask==0, dtype='uint8') G = Gnew * left_mask + G * right_mask LAT = G/imArray_lowpass LAT, imLAT = utils.formatimage(LAT, dtype='uint8') # format image as uint8 binary_edge_map=np.array(LAT>1, dtype='uint8') binary_edge_map *= 255 # grayscaling binary_edge_map, imOutput = utils.formatimage(binary_edge_map) return [binary_edge_map, imOutput]
def wtHighFreq(img, mode='haar', level=1): ''' Apply Wavelet Transform to an image Author: Lee Seongjoo [email protected] 2009 (c) Lee Seongjoo ''' imArray = utils.im2array(img) # compute coefficients of multiresolution WT coeffs = pywt.wavedec2(imArray, mode, level=level) # high frequency coeffs coeffs_H = list(coeffs) # discarding the low frequency # Approximation coeffs are from the low-pass filter coeffs_H[0] = np.zeros(coeffs_H[0].shape) # multilevel reconstruction imArray_H = pywt.waverec2(coeffs_H, mode) #Compute binarization of image HA = imArray_H.mean() # mean value of high frequency part imArray_H SBEI = np.array(imArray_H < HA + 5, dtype='uint8') # binarized image #msg='Multiresolution Wavelet Transform level: '+ str(level) #print msg #print "execution took", t_end-t0, "seconds" # converting resulting ndarray into an image imArray_H, image_wavetransformed = utils.formatimage(imArray_H) SBEI *= 255 # SBEI to grayscale SBEI, image_binarized = utils.formatimage(SBEI) imgData = [SBEI, image_wavetransformed, image_binarized] return imgData
def wtHighFreq(img, mode='haar', level=1): ''' Apply Wavelet Transform to an image Author: Lee Seongjoo [email protected] 2009 (c) Lee Seongjoo ''' imArray = utils.im2array(img) # compute coefficients of multiresolution WT coeffs=pywt.wavedec2(imArray, mode, level=level) # high frequency coeffs coeffs_H=list(coeffs) # discarding the low frequency # Approximation coeffs are from the low-pass filter coeffs_H[0]=np.zeros(coeffs_H[0].shape) # multilevel reconstruction imArray_H=pywt.waverec2(coeffs_H, mode) #Compute binarization of image HA=imArray_H.mean() # mean value of high frequency part imArray_H SBEI=np.array(imArray_H<HA+5, dtype='uint8') # binarized image #msg='Multiresolution Wavelet Transform level: '+ str(level) #print msg #print "execution took", t_end-t0, "seconds" # converting resulting ndarray into an image imArray_H, image_wavetransformed = utils.formatimage(imArray_H) SBEI *= 255 # SBEI to grayscale SBEI, image_binarized = utils.formatimage(SBEI) imgData=[SBEI,image_wavetransformed, image_binarized] return imgData
def __init__(self, image): ''' Constructor ''' self.image = utils.im2array(image)