def mean_gray_img(imarray): Bc = np.ones((3, 3), dtype=bool) mxt = siamxt.MaxTreeAlpha(imarray, Bc) mean = mxt.computeNodeGrayAvg() mean_img = mean[mxt.node_index] mean_img = np.reshape(mean_img, [mean_img.shape[0], mean_img.shape[1], 1]) return mean_img
def volume_img(imarray): Bc = np.ones((3, 3), dtype=bool) mxt = siamxt.MaxTreeAlpha(imarray, Bc) volume = mxt.computeVolume() volume_img = volume[mxt.node_index] volume_img = np.reshape(volume_img, [volume_img.shape[0], volume_img.shape[1], 1]) return volume_img
def imgSemBuracos(img, area): Bc = np.ones((3, 3), dtype=bool) maxi = img.max() gray = maxi - img mxt = siamxt.MaxTreeAlpha(gray, Bc) mxt.areaOpen(area) imgSemBuracos = maxi - mxt.getImage() return imgSemBuracos
def height_gray_img(imarray): Bc = np.ones((3, 3), dtype=bool) mxt = siamxt.MaxTreeAlpha(imarray, Bc) mean = mxt.computeHeight() height_img = mean[mxt.node_index] height_img = np.reshape(height_img, [height_img.shape[0], height_img.shape[1], 1]) return height_img
def area_image_img(imarray): Bc = np.ones((3, 3), dtype=bool) mxt = siamxt.MaxTreeAlpha(imarray, Bc) area = mxt.node_array[3, :] area_img = area[mxt.node_index] area_img = np.reshape(area_img, [area_img.shape[0], area_img.shape[1], 1]) return area_img
def area_image(imarray): Bc = np.ones((3, 3), dtype=bool) r, c, b = tuple(imarray.shape) area_img = np.zeros([r, c, b], dtype=float) for i in range(b): mxt = siamxt.MaxTreeAlpha(imarray[:, :, i], Bc) area = mxt.node_array[3, :] area_img[:, :, i] = area[mxt.node_index] #area_img=np.array(area_img,dtype=np.uint16) return area_img
def height_image(imarray): Bc = np.ones((3, 3), dtype=bool) r, c, b = tuple(imarray.shape) height_img = np.zeros([r, c, b], dtype=float) for i in range(b): mxt = siamxt.MaxTreeAlpha(imarray[:, :, i], Bc) height = mxt.computeHeight() height_img[:, :, i] = height[mxt.node_index] height_img = np.array(height_img, dtype=np.uint16) return height_img
def AP(im, thresholds=option.thresholds, connexity=1): S = len(thresholds) n_features = 2 * S + 1 # Element structurant Se = np.ones((3, 3), dtype=bool) if connexity == 1: Se[0, 0] = False Se[2, 0] = False Se[0, 2] = False Se[2, 2] = False # Construction du tableau résultat h, w = im.shape ap = np.zeros((h, w, n_features)) # Max value of image for negative maxi = im.max() neg = maxi - im # Min-tree (max-tree with negative image) min_tree = siamxt.MaxTreeAlpha(neg, Se) # Max-tree max_tree = siamxt.MaxTreeAlpha(im, Se) # Closings (Thickening Profile) for i in range(S): n = S - i - 1 clone = min_tree.clone() clone.areaOpen(thresholds[n]) ap[:, :, i] = maxi - clone.getImage() # Original ap[:, :, S] = im # Openings (Thinnening Profile) for i in range(S + 1, 2 * S + 1): n = i - 1 - S clone = min_tree.clone() clone.areaOpen(thresholds[n]) ap[:, :, i] = maxi - clone.getImage() return ap
def mean_gray_image(imarray): Bc = np.ones((3, 3), dtype=bool) r, c, b = tuple(imarray.shape) mean_img = np.zeros([r, c, b], dtype=float) for i in range(b): mxt = siamxt.MaxTreeAlpha(imarray[:, :, i], Bc) mean = mxt.computeNodeGrayAvg() mean_img[:, :, i] = mean[mxt.node_index] mean_img = np.array(mean_img, dtype=np.uint16) return mean_img
def volume_image(imarray): Bc = np.ones((3, 3), dtype=bool) r, c, b = tuple(imarray.shape) volume_img = np.zeros([r, c, b], dtype=float) for i in range(b): mxt = siamxt.MaxTreeAlpha(imarray[:, :, i], Bc) volume = mxt.computeVolume() volume_img[:, :, i] = volume[mxt.node_index] volume_img = np.array(volume_img, dtype=np.uint16) return volume_img
def area_image_cube(imarray): Bc = np.zeros((3, 3, 3), dtype=bool) Bc[1, 1, :] = True Bc[:, 1, 1] = True Bc[1, :, 1] = True mxt = siamxt.MaxTreeAlpha(imarray, Bc) area = mxt.node_array[3, :] area_img = area[mxt.node_index] return area_img
def tree_one(imarray, r, c): Bc = np.ones((3, 3), dtype=bool) mxt = siamxt.MaxTreeAlpha(imarray, Bc) attributes1 = attribute_area_filter(mxt, (500)) attributes2 = attribute_area_filter(mxt, (5000)) attributes3 = attribute_area_filter(mxt, (50000)) attributes = np.stack((imarray, attributes1, attributes2, attributes3), axis=-1) image_array = np.reshape(attributes, (r * c, 4)) return image_array
def tree_per_date(imarray,r,c,b): Bc = np.ones((3, 3), dtype=bool) attributes = np.zeros([r, c, 3 * b], dtype=float) for i in range(b): mxt = siamxt.MaxTreeAlpha(imarray[:, :, i], Bc) attributes[:, :, i] = attribute_area_filter(mxt, (500)) attributes[:, :, b + i] = attribute_area_filter(mxt, (5000)) attributes[:, :, 2 * b + i] = attribute_area_filter(mxt, (50000)) image_array = np.reshape(attributes, (r * c, 3 * b)) return image_array
def cut_selection(im, connexity=1): # Element structurant Se = np.ones((3, 3), dtype=bool) if connexity == 1: Se[0, 0] = False Se[2, 0] = False Se[0, 2] = False Se[2, 2] = False # Computation of tree representation T(f) T = siamxt.MaxTreeAlpha(im, Se) # Computation of attribute A(T) on nodes A = T.node_array[3, :] # Sort lambda_ = np.unique(np.sort(A)) L = lambda_.shape[0] # Compute profile h, w = im.shape profile = np.zeros((h, w, L)) for i in range(L): clone = T.clone() clone.areaOpen(lambda_[i]) profile[:, :, i] = clone.getImage() GCF = np.zeros((L, )) for i in range(L): GCF[i] = gcf_pix(im, profile, i) # Problème de compréhension de l'algo # au niveau de l'estimation G^C^F (piecewise linear regression) nth = 1 cut_t = [lambda_[0]] while nth < L: GCF_p = np.zeros((nth, )) for i in range(nth): GCF_p[i] = gcf_pix(im, profile, i) gcf_t = gcf_pix(im, profile, nth) if GCF_p[:] == GCF[:nth]: break cut_t.append(lambda_[nth]) nth += 1 L_p = nth print(L_p) print(cut_t)
def post_proc(pred_folder): imgs = [f for f in os.listdir(pred_folder) if f.endswith('.nii.gz')] Bc = np.ones((3, 3, 3), dtype=bool) start_time = time.time() for img in imgs: img2 = img[:-7] + "_pp.nii.gz" print img2 data = nib.load(os.path.join(pred_folder, img)) affine = data.get_affine() data = data.get_data() mxt = siamxt.MaxTreeAlpha(data, Bc) mxt.areaOpen(mxt.node_array[3, 1:].max() - 5) data2 = mxt.getImage() new_data = data2 nii = nib.Nifti1Image(new_data, affine) nib.save(nii, os.path.join(pred_folder, img2)) print("--- %s seconds ---" % (time.time() - start_time))
def filtroLetras(img): gray = img.max() - img Bc = np.ones((3,3),dtype = bool) mxt = siamxt.MaxTreeAlpha(gray, Bc) Wmin, Wmax = 10 , 50 Hmin, Hmax = 10 ,50 rr = 0.20 dy = mxt.node_array[7,:] - mxt.node_array[6,:] dx = mxt.node_array[10,:] - mxt.node_array[9,:] area = mxt.node_array[3,:] RR = 1.0*area/(dx*dy) height = mxt.computeHeight() gray_var = mxt.computeNodeGrayVar() nodes = (dy > Hmin) & (dy < Hmax) & (dx > Wmin) & (dx < Wmax) & (RR > rr) & (gray_var < 15**2) & (dy*2.0 > dx) & (dx*4.0 > dy) & (height > 10) #Filtering mxt.contractDR(nodes) imgFiltered = mxt.getImage() return imgFiltered
def mean_gray_cube(imarray): Bc = np.ones((3, 3, 3), dtype=bool) mxt = siamxt.MaxTreeAlpha(imarray, Bc) mean = mxt.computeNodeGrayAvg() mean_img = mean[mxt.node_index] return mean_img
import siamxt from project_functions import * from scipy.misc import imsave Image = geoimread('samples/spaindataset/spaingraymerge.tif') imarray = geoImToArray(Image) imarray = imarray.clip(min=0) imarray = imarray.astype(np.uint8) std_img = stdSITS(imarray) #Tree construction Bc = np.ones((3, 3), dtype=bool) input_img = np.array(std_img, dtype=np.uint8) mxt = siamxt.MaxTreeAlpha(input_img, Bc) result = attribute_area_filter(mxt, (1000)) w, h = result.shape for x in range(w): for y in range(h): if result[x, y] >= result.mean(): result[x, y] = 255 else: result[x, y] = 0 #write geotiff imsave('samples/jordandataset/jordan_stdfilter1000.png', result)
plt.imshow(img, cmap='Greys_r') plt.axis('off') plt.title("Original image") #Structuring element with connectivity-8 Bc = np.ones((3, 3), dtype=bool) # Negating the image img_max = img.max() img_neg = img_max - img # Area threshold area = 40 #Building the max-tree of the negated image, i.e. min-tree mxt_neg = siamxt.MaxTreeAlpha(img_neg, Bc) # Making a hard copy of the max-tree mxt_neg2 = mxt_neg.clone() #Applying an area-open filter mxt_neg.areaOpen(area) #Recovering the image img_filtered = mxt_neg.getImage() # Negating the image back img_filtered = img_max - img_filtered #Displaying the filtered image fig = plt.figure()
def image_area_open_filter(img_neg, neighborhood, area): mxt_neg = siamxt.MaxTreeAlpha(img_neg, neighborhood) mxt_neg.areaOpen(area) return mxt_neg.getImage()
imgW = cv2.imread('knee.pgm') img = cv2.imread('knee.pgm', 0) img_max = img.max() neg_img = img_max - img kernel = np.ones((3, 3), np.uint8) kernel[0][0] = kernel[2][0] = 0 kernel[1][1] = kernel[0][2] = kernel[2][2] = 0 gradient = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel) ImgMax = gradient.max() gray = ImgMax - gradient Bc = np.ones((3,3),dtype = bool) mxt = siamxt.MaxTreeAlpha(gray, Bc) V = mxt.computeVolume() H = mxt.computeHeight() Vext = mxt.computeExtinctionValues(V, "volume") Hext = mxt.computeExtinctionValues(H, "height") mxt.extinctionFilter(Vext, 8) #mxt.extinctioxnFilter(Hext, 8) img_filtered = ImgMax - mxt.getImage() #img_filtered = (img_filtered) ret, thresh = cv2.threshold(img_filtered, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) cv2.imshow('a', thresh)
imgs[0].set_title('Imagem Original - Cabelos') imgs[1].imshow(after_morph_img[100:1000, 200:1200], cmap='gray') imgs[1].set_title( 'Resultado da remocao de cabelos por morfologia (fechamento)') plt.show() # Neighborhood connectivity-8 neighborhood = np.array([[True, True, True], [True, True, True], [True, True, True]]) img_max = after_morph_img.max() img_neg = img_max - after_morph_img mxt_lettters = siamxt.MaxTreeAlpha(img_neg, neighborhood) # Size and shape thresholds Wmin, Wmax = 6, 48 Hmin, Hmax = 26, 50 rr = 0.4 dy = mxt_lettters.node_array[7, :] - mxt_lettters.node_array[6, :] dx = mxt_lettters.node_array[10, :] - mxt_lettters.node_array[9, :] area = mxt_lettters.node_array[3, :] RR = 1.0 * area / (dx * dy) min_height = 14 # Compute values height = mxt_lettters.computeHeight() gray_var = mxt_lettters.computeNodeGrayVar()
def volume_cube(imarray): Bc = np.ones((3, 3, 3), dtype=bool) mxt = siamxt.MaxTreeAlpha(imarray, Bc) volume = mxt.computeVolume() volume_img = volume[mxt.node_index] return volume_img
im2=np.reshape(imarray2,[imarray2.shape[0],imarray2.shape[1]]) Image3 = geoimread('data/phr3.png') imarray3=geoImToArray(Image3) imarray3=np.array(imarray3,dtype=np.uint8) im3=np.reshape(imarray3,[imarray3.shape[0],imarray3.shape[1]]) im_show(im1) im_show(im2) im_show(im3) merged=np.concatenate((imarray1,imarray2,imarray3),axis=2) #max tree Bc = np.ones((3,3,3), dtype=bool) tree1 = siamxt.MaxTreeAlpha(merged, Bc) t=1000 #threshold a=attribute_area_filter(tree1,t) im_show(a[:,:,0]) im_show(a[:,:,1]) im_show(a[:,:,2]) imsave('results/orig1.png',im1) imsave('results/orig2.png',im2) imsave('results/orig3.png',im3) imsave('results/filt1.png',a[:,:,0]) imsave('results/filt2.png',a[:,:,1]) imsave('results/filt3.png',a[:,:,2])
def height_gray_cube(imarray): Bc = np.ones((3, 3, 3), dtype=bool) mxt = siamxt.MaxTreeAlpha(imarray, Bc) mean = mxt.computeHeight() height_img = mean[mxt.node_index] return height_img
def segm_watershed(wFA_ms, eigenvects_ms, gaussian_sigma=0.5): import numpy as np from scipy import ndimage import siamxt from libcc.preprocess import run_analysis, grad_morf from libcc.gets import getTheCC from skimage.morphology import disk, square, erosion, dilation from skimage.segmentation import watershed from skimage.measure import label, regionprops ## MORPHOLOGICAL GRADIENT # Gaussian filter wFA_gauss = ndimage.gaussian_filter(wFA_ms, sigma=gaussian_sigma) # Structuring element se1 = np.zeros((3, 3)).astype('bool') se1[1, :] = True se1[:, 1] = True # Gradient grad_wFA = grad_morf(wFA_gauss, se1) ## MAX-TREE #Structuring element. connectivity-4 se2 = se1.copy() # Computing Max Tree by volume mxt = siamxt.MaxTreeAlpha(((grad_wFA) * 255).astype("uint8"), se2) attr = "volume" leaves_volume = mxt.computeExtinctionValues(mxt.computeVolume(), attr) # Create canvas segm_markers = np.zeros(grad_wFA.shape, dtype=np.int16) # Labeling canvas indexes = np.argsort(leaves_volume)[::-1] counter = 1 for i in indexes[:85]: segm_markers = segm_markers + mxt.recConnectedComponent(i) * (counter) counter += 1 ## SEGMENTING CC # Watershed wc_wfa = watershed(grad_wFA, segm_markers) # Thresholding regions by FA seg_wFA = np.zeros((wFA_ms).shape).astype(bool) segs = seg_wFA listAll = np.unique(wc_wfa) for i in listAll: media = np.mean(wFA_ms[wc_wfa == i]) if media > 0.2 * wFA_ms.max(): seg_wFA[wc_wfa == i] = 1 # Getting the CC seg_wFA, ymed, xmed = getTheCC(seg_wFA) return seg_wFA
struct = np.array([[False, True, False], [True, True, True], [False, True, False]]) dilation = ndimage.grey_dilation(img_gray, structure=struct) erosion = ndimage.grey_erosion(img_gray, structure=struct) gradient_img = dilation - erosion # Neighborhood connectivity-8 neighborhood = np.ones((3, 3), dtype=bool) # Number of leaves to be preserved n = 8 max_from_gradient = gradient_img.max() mxt_gradient = siamxt.MaxTreeAlpha(max_from_gradient - gradient_img, neighborhood) # Computes volume attribute of the max-tree nodes area = mxt_gradient.node_array[3, :] height = mxt_gradient.computeHeight() volume = area * height # Apply extinct filter volume_ext = mxt_gradient.computeExtinctionValues(volume, "volume") mxt_gradient.extinctionFilter(volume_ext, n) #Recovering the image img_gradient_filtered = max_from_gradient - mxt_gradient.getImage() #Displaying the filtered image fig, imgs = plt.subplots(1, 2, figsize=(15, 15))
def EP(im, connexity=1): n_features = option.n_features extrema = option.extrema S = option.S # Element structurant Se = np.ones((3, 3), dtype=bool) if connexity == 1: Se[0, 0] = False Se[2, 0] = False Se[0, 2] = False Se[2, 2] = False # Construction du tableau résultat h, w = im.shape EPa = np.zeros((h, w, n_features)) #EPh = np.zeros((h, w, n_features)) # Max value of image for negative maxi = im.max() neg = maxi - im # Min-tree (max-tree with negative image) min_tree = siamxt.MaxTreeAlpha(neg, Se) # Max-tree max_tree = siamxt.MaxTreeAlpha(im, Se) # Area extinction area_min = min_tree.node_array[3, :] area_max = max_tree.node_array[3, :] # Extinction values extAreaMin = min_tree.computeExtinctionValues(area_min, "area") extAreaMax = max_tree.computeExtinctionValues(area_max, "area") #extContrastMin = min_tree.computeExtinctionValues(min_tree.computeHeight(), "height") #extContrastMax = max_tree.computeExtinctionValues(max_tree.computeHeight(), "height") # Thickening Profile for i in range(S): n = S - i - 1 clone = min_tree.clone() clone.extinctionFilter(extAreaMin, extrema[n]) EPa[:, :, i] = maxi - clone.getImage() # EPh #clone = min_tree.clone() #clone.extinctionFilter(extContrastMin, extrema[n]) #EPh[:, :, i] = maxi - clone.getImage() EPa[:, :, S] = im #EPh[:, :, S] = im # Thinning Profile for i in range(S + 1, 2 * S + 1): n = i - 1 - S clone = max_tree.clone() clone.extinctionFilter(extAreaMax, extrema[n]) EPa[:, :, i] = clone.getImage() # EPh #clone = max_tree.clone() #clone.extinctionFilter(extContrastMax, extrema[n]) #EPh[:, :, i] = clone.getImage() return EPa