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 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 BEI(img, scale=6): ''' Compute sum of SBEIs. Default scale is 6. ''' sumSBEI = np.zeros((112, 92)) # initialization imageList = [] # wavelet transformed images container list #print "Processing image's BEI" for level in range(1, scale): SBEI, image_WT, im_SBEI = wtHighFreq(img, level=level) imageList.append(image_WT) # store wave transformed image sumSBEI += SBEI #imageList.insert(0, imgData[5]) # insert the original image into the list imgSize = imageList[-1].size[-1] * imageList[-1].size[-1] NfpList = [] TList = [] for t in range(1, scale - 1): threshold = np.array(sumSBEI >= t, dtype='uint8') Nfp = threshold.sum() T = abs(Nfp / imgSize - 0.2) NfpList.append(Nfp) TList.append(T) threshold = min(TList) # set threshold BEI = np.array(sumSBEI > threshold, dtype='uint8') # binary image BEI *= 255 BEI, imBEI = utils.formatimage(BEI) return [BEI, imBEI]
def BEI(img, scale=6): ''' Compute sum of SBEIs. Default scale is 6. ''' sumSBEI=np.zeros((112,92)) # initialization imageList=[] # wavelet transformed images container list #print "Processing image's BEI" for level in range(1, scale): SBEI, image_WT, im_SBEI = wtHighFreq(img,level=level) imageList.append(image_WT) # store wave transformed image sumSBEI+=SBEI #imageList.insert(0, imgData[5]) # insert the original image into the list imgSize=imageList[-1].size[-1]*imageList[-1].size[-1] NfpList=[] TList=[] for t in range(1, scale-1): threshold=np.array(sumSBEI>=t, dtype='uint8') Nfp=threshold.sum() T=abs(Nfp/imgSize-0.2) NfpList.append(Nfp) TList.append(T) threshold=min(TList) # set threshold BEI = np.array(sumSBEI > threshold, dtype='uint8') # binary image BEI *= 255 BEI, imBEI = utils.formatimage(BEI) return [BEI, imBEI]
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 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 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 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 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 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 BEI2(img, scale=6): ''' noise filtered enhanced BEI Algorithm based on Song et al 2007 ''' arrayBEI, imBEI = BEI(img, scale) binary_image_map, imsobelLAT = sobels.sobelLAT(img) # noise filtering BEI2 = arrayBEI * binary_image_map # clustering # finding all eight-connected components from BEI # then # image formatting BEI2 *= 255 # Black-and-White image value BEI2, imBEI2 = utils.formatimage(BEI2) return [BEI2, imBEI2]
def outputImage(self, response): output, imOutput = utils.formatimage(response) return [output, imOutput]