• Aucun résultat trouvé

library(tiff) library(rgl) library(magick) library(geometry) library(grDevices) library(plot3D) library(raster) library(scales) library(smacpod) library(svMisc) library(plotrix) library(ijtiff) library(Hmisc) library(dplyr)

# Appel du script contenant les fonctions

#pour calculer les centres et rayons des spheres de Delaunay

source("D:\\fdanfa\\workspace\\work\\MOSAIC\\Calcul_centre_rayon.R")

source("D:\\fdanfa\\workspace\\work\\MOSAIC\\Loi_young_laplace.R")

# Chargement des données

#processed.image <-"Selection1"

processed.image <-"Selection1"

load(file=paste("D:/fdanfa/workspace/work/MOSAIC/WO_MLH2048/",processed.ima

ge,"/data.RData", sep="")

liste.im

<-list.files(paste("D:\\fdanfa\\workspace\\work\\MOSAIC\\WO_MLH2048\\Selectio n2\\",processed.image,"\\image_pore_blanc1", sep=""))

#Lecture de l'image sous format tiff

im1<- list()

for(i in 1:(length(liste.im))){

im1[[i

]]<-readTIFF(paste("D:\\fdanfa\\workspace\\work\\MOSAIC\\WO_MLH2048\\Selection2 \\",processed.image,"\\image_pore_blanc1\\",liste.im[i], sep=""))

}

# Transformation en objet raster l'image 3D # Initialisation

x <- c()

y <- c()

z <- c()

for(i in 1:length(im1)){

im1.i <- im1[[i]]

im1.ir <- raster(im1.i, xmn=0, xmx=nrow(im1[[i]]), ymn=0, ymx=ncol(im1[[i]]), crs=NA, template=NULL)

im1.irSum <- focal(im1.ir, w=matrix(1,3,3), fun=sum)

im1.irSum.values <- as.matrix(im1.irSum)

im1.i.inv <- im1.i im1.i.inv[im1.i==0] <- 1

im1.i.inv[im1.i==1] <- 0

im1.i.bord <- im1.i.inv * im1.irSum.values

im1.i.bord <- im1.i.bord[2:(nrow(im1[[i]])-1),2:(ncol(im1[[i]])-1)]

for(k in 1:nrow(im1.i.bord)){ for(j in 1:ncol(im1.i.bord)){

# Recupération des points de la frontiere de l'espace poral(les pixels dont leur valeur est superieur a zero )

2 x <- c(x, k) y <- c(y, j) z <- c(z, i) } } } }

bord.coord<- cbind(x,y,z)

#Matrice des coordonnees des pixels qui sont sur la frontiere de la forme #Triangulation de Delaunay des point s dela frontiere de la forme

Delaunay <- delaunayn(bord.coord,options="Qt",full = T) # Calcul des centres et rayons des spheres de Delaunay

ind_points<-list()

for(i in 1:nrow(Delaunay$tri)){

print(paste(i,"/",nrow(Delaunay$tri), sep=""))

ind_points[[i]]<-Delaunay$tri[i,] # les indices des points de chaque tetraedre

}

#Initialisation des Centre et rayons des spheres de Delaunay

C<-list()

r<-list()

for(j in 1:nrow(Delaunay$tri)){

print(paste(j,"/",nrow(Delaunay$tri), sep=""))

# Extraction des coordonnées des quatres points de chaque tetraedre

ind_points.j<-ind_points[[j]]

P1<-bord.coord[ind_points.j[1],]

P2<-bord.coord[ind_points.j[2],]

P3<-bord.coord[ind_points.j[3],]

P4<-bord.coord[ind_points.j[4],]

#Appel de la fonction qui calcul le centre et le rayon de chaque sphere de Delaunay res.j <- circumcenter_radius(P1,P2,P3,P4) C[[j]]<-res.j[[1]] r[[j]]<-res.j[[2]] }

# Suppression des NA (cas des matrices non-inversibles)

C <- na.omit.list(C)

r <- na.omit.list(r)

# Matrice des centres des spheres de Delaunay

C.m <- matrix(data=unlist(C), nrow = length(C), ncol = 3, byrow = T) # Matrice des rayons des spheres de Delaunay

r.m <- unlist(r)

#Matrice composee des centres et rayons des spheres de Delaunay

sphere.m <- cbind(C.m, r.m)

colnames(sphere.m) <- c("x", "y", "z","r")

#Nettoyage de la matrice sphere.m

sphere.m <- unique(sphere.m) # enlèle les duplications

sphere.m <- floor(sphere.m) # la partie entière

sphere.m <- unique(sphere.m) # enleve encore les duplications #suppression de toutes les lignes qui ont des valeurs negatives

sphere.m_new <- sphere.m[sphere.m[,"r"]>0,]

sphere.m_new <- sphere.m_new[sphere.m_new[,"x"]>0,]

sphere.m_new<- sphere.m_new[sphere.m_new[,"y"]>0,]

sphere.m_new <- sphere.m_new[sphere.m_new[,"z"]>0,]

3 #sphere.m_new1 permet de supprimer les spheres de meme centre avec des rayon differents

sphere.m_new1 <- unique(sphere.m_new[duplicated(sphere.m_new[,1:3]),]) # suppression des spheres de meme centre avec des rayons differents

for(i in 1: nrow(sphere.m_new1)){

print(paste(i,"/",nrow(sphere.m_new1), sep=""))

#sphere numero i de la matrice des spheres de meme centre avec des rayons differents

sphere.i <- sphere.m_new1[i,]

#Rayons maximal des sphere de meme centre pour la suppression

rmax.i <- max(sphere.m_new[sphere.m_new[,"x"]==sphere.i["x"] &

sphere.m_new[,"y"]==sphere.i["y"] & sphere.m_new[,"z"]==sphere.i["z"],"r"])

# Matrice des spheres de Delaunay de centre different

sphere.m_new2 <- sphere.m_new[!(sphere.m_new[,"x"]==sphere.i["x"] &

sphere.m_new[,"y"]==sphere.i["y"] & sphere.m_new[,"z"]==sphere.i["z"] &

sphere.m_new[,"r"]<rmax.i),]

}

sphere.m_new2<-sphere.m_new2[sphere.m_new2[,"x"]<=nrow((im1[[1]])),]

sphere.m_new2<-sphere.m_new2[sphere.m_new2[,"y"]<=ncol((im1[[1]])),] # Initialisation xtot <- c() ytot <- c() ztot <- c() P<-c() V<-list()

#Initialisation de la matrice des spheres de Delaunay qui sont 90% dans la forme

sphere_in_form<-matrix(0,nrow=nrow(sphere.m_new2),ncol=4)

for(l in 1:nrow(sphere.m_new2)){

print(paste(l,"/",nrow(sphere.m_new2), sep=""))

# coordonnees des pixels du cube qui comprend la sphere

x <- (sphere.m_new2[l,"x"

]-sphere.m_new2[l,"r"]):(sphere.m_new2[l,"x"]+sphere.m_new2[l,"r"])

y <- (sphere.m_new2[l,"y"

]-sphere.m_new2[l,"r"]):(sphere.m_new2[l,"y"]+sphere.m_new2[l,"r"])

z <- (sphere.m_new2[l,"z"

]-sphere.m_new2[l,"r"]):(sphere.m_new2[l,"z"]+sphere.m_new2[l,"r"])

# grid des des coordonnees des pixels du cube qui comprend la sphere l

pixel_grid <- expand.grid(xtot = x, ytot = y, ztot=z)

s<-c()

# selection des pixels du cube qui sont dans la sphère

pixel_form <-pixel_grid[(pixel_grid[,"xtot"]-sphere.m_new2[l,"x"])^2+ (pixel_grid[,"ytot"]-sphere.m_new2[l,"y"])^2 +(pixel_grid[,"ztot"

]-sphere.m_new2[l,"z"])^2 <=(sphere.m_new2[l,"r"])^2,]

#On pend les pixels qui sont dans l'image (pas de pixels a coordonnees negatives)

pixel_form <- pixel_form[pixel_form[,"ztot"]>0 &

pixel_form[,"ztot"]<=length(im1),] # on prend les lignes de pixel_form dont ztot est inferieur ou egal à 100 et ztot >0 (pas de pixels

à coordonnées négatives)

#pixel_form <- pixel_form[pixel_form[,"xtot"]>0,]

pixel_form <- pixel_form[pixel_form[,"xtot"]>0 &

pixel_form[,"xtot"]<=nrow((im1[[1]])),]

#pixel_form <- pixel_form[pixel_form[,"ytot"]>0,]

pixel_form <- pixel_form[pixel_form[,"ytot"]>0 &

pixel_form[,"ytot"]<=ncol((im1[[1]])),]

if(nrow(pixel_form)!=0){

4 liste.imi <- unique(pixel_form[,"ztot"])

# On recupere les valeurs des pixels qui sont et dans l'image et dans la sphere

vi <- c()

for(i in 1:length(liste.imi)){

liste.pixi <- pixel_form[pixel_form[,"ztot"]==liste.imi[i],]

vi

<-c(vi,im1[[liste.imi[i]]][cbind(liste.pixi[,"xtot"],liste.pixi[,"ytot"])])

V[[l]]<-vi

P[l]<-sum(V[[l]])/nrow(pixel_form) if(P[l]>0.9){

sphere_in_form[l,]<-sphere.m_new2[l,] # on garde uniquement les spheres qui sont a x% dans la forme

}else{ sphere_in_form[l,]<- sphere_in_form[l,] } } } }

sphere_in_form<-sphere_in_form[sphere_in_form[,1]>0,]

colnames(sphere_in_form) <- c("x","y","z","r")

# On ordonne et tri sphere_in_form par ordre croissant selon les rayons

sphere_in_form <- sphere_in_form[order(sphere_in_form[,"r"]),]

for(i in sort(unique(sphere_in_form[,"z"]))){

sphere_in_form.i <- sphere_in_form[sphere_in_form[,"z"]==i,]

if(is.matrix(sphere_in_form.i)){ sphere_in_form.circles.i

<-circles.intersect(sphere_in_form.i[,c("x","y")], sphere_in_form.i[,"r"])

#Pour chaque sphere la liste des intersections

tmp.i<- matrix(1:nrow(sphere_in_form.circles.i),nrow =

nrow(sphere_in_form.circles.i), ncol = ncol(sphere_in_form.circles.i), byrow=T)

tmp.i[tmp.i==0] <- NA

list_spher_inter.i <- tmp.i*sphere_in_form.circles.i list_spher_inter.i

<-tapply(list_spher_inter.i,rep(1:nrow(list_spher_inter.i),ncol(list_spher_in

ter.i)),function(i)i)

list_spher_inter.i <- lapply(list_spher_inter.i, function(x)

x[!is.na(x)])

list_spher_inter.i <- unname(list_spher_inter.i)

# fonction pour rassembler toutes les boules qui s'intersectent dans un vecteur

sphere.inter<- function(t, l=list_spher_inter.i){

sort(unique(unlist(l[unlist(t)])))

}

list_spher_inter1.i <- lapply(list_spher_inter.i, FUN=sphere.inter)

# Pour enlever les elements de la liste duplique

list_spher_inter2.i

<-list_spher_inter1.i[!duplicated(lapply(list_spher_inter1.i, sort))]

# Pour trouver le rayon de la plus grande sphere

liste_rayon.i <- sphere_in_form.i[,"r"] # liste des rayons des spheres qui sont au moins 90% dans la forme

# Fonction Rayon_maxi pour trouver le plus grand rayon

Rayon_maxi <- function(t, l=list_spher_inter2.i){

#sort(unique(unlist(l[unlist(li)])))

5 }

res1.i <- lapply(list_spher_inter2.i, FUN=Rayon_maxi)

# Fonction Indice_rayon_maxi pour trouver le numero de la sphere de plus grand rayon

Indice_rayon_maxi<- function(t, l=res1.i){

#sort(unique(unlist(l[unlist(li)])))

t[which.max(liste_rayon.i[t])]

}

# Indice de la sphere de plus grand rayon

res2.i <- unique(lapply( list_spher_inter2.i, FUN= Indice_rayon_maxi))

# Coordonnees + rayon de la sphere de plus grand rayon

f<-function(w) w+1

for(k in 1:length( list_spher_inter2.i)){ if( list_spher_inter2.i[[k]][1]==0){

res3.i <- sphere_in_form.i[unlist(unique(lapply( lapply(res2.i,f) , FUN= Indice_rayon_maxi))), ]

}else{

res3.i <- sphere_in_form.i[unlist(unique(lapply(res2.i, FUN=

Indice_rayon_maxi))), ]

}

}

}else{

# Cas ou on n'a qu'un seule sphere

liste_rayon.i <- sphere_in_form.i["r"] res3.i <- sphere_in_form.i } if(i==1){ res.tot <- res3.i }else{

# Indice de toutes les spheres de rayon maxi

res.tot <- rbind(res.tot, res3.i)

}

}

res.tot <- res.tot[res.tot[,"z"]<=length(im1),]

res.tot <- cbind(1:nrow(res.tot), res.tot)

colnames(res.tot)[1] <- "id.sp"

# Recuperation des pixels des l'espace poral ie les coef des matrices im1[[i]] qui valent 1

xpix <- c()

ypix <- c()

zpix <- c()

start_time <- Sys.time()

for(i in 1:length(im1)){

print(paste(i,"/",length(im1), sep=""))

for(k in 1:nrow(im1[[1]])){ for(j in 1:ncol(im1[[1]])){ if(im1[[i]][k,j]==1){ xpix <- c(xpix, k) ypix <- c(ypix, j) zpix <- c(zpix, i) }

6 } } }

coord.pix.por<- cbind(1:length(xpix),xpix,ypix,zpix)

colnames(coord.pix.por)[1] <- "id.pix"

#On affecte à chaque pixel qui est au moins dans une sphere le plus grand rayon des spheres qui le contiennent

#Initalisation de la matrice qui contient les coord de chaque voxel de la forme et du rayon associe

afec <- matrix(0,nrow=nrow(coord.pix.por),ncol=5)

afec.sp <- matrix(0,nrow=nrow(coord.pix.por),ncol=5)

start_time <- Sys.time()

for(j in 1:nrow(coord.pix.por)){

ps <- (coord.pix.por[j,"xpix"]-res.tot[,"x"])^2+

(coord.pix.por[j,"ypix"]-res.tot[,"y"])^2 +(coord.pix.por[j,"zpix"

]-res.tot[,"z"])^2 <=(res.tot[,"r"])^2

if(TRUE %in% unique(ps)){

afec[j,]<-c(coord.pix.por[j,],max(res.tot[ps,"r"]))

afec.sp[j,]<-c(res.tot[which.max(res.tot[ps,"r"]),])

} }

colnames(afec)<-c("id.pix","x","y","z","r.afec")

afec<-afec[afec[,"x"]>0,]

afec<-afec[afec[,"y"]>0,]

afec<-afec[afec[,"z"]>0,]

afec<-afec[afec[,"r.afec"]>0,]

colnames(afec.sp)<-c("id.sp","x","y","z","r")

afec.sp<-afec.sp[afec.sp[,"x"]>0,]

afec.sp<-afec.sp[afec.sp[,"y"]>0,]

afec.sp<-afec.sp[afec.sp[,"z"]>0,]

afec.sp<-afec.sp[afec.sp[,"r"]>0,]

afec.sp <- unique(afec.sp)

#Listes des pixels qui ne sont dans aucune sphere de la forme

liste.pix.sr <- coord.pix.por[coord.pix.por[,"id.pix"] %nin%

afec[,"id.pix"],]

#On affecte à chaque pixel le rayon de la sphere dont il est plus proche du centre parmis toutes les sphere qui le contiennent

# Initialisation du vecteur qui contient pour chaque voxel de la forme ça distance avec le centre du sphere dont il appartient

dis_pix_centre<-c()

#Initalisation de la matrice qui contient les coord de chaque voxel de la forme et le rayon qui lui est associé

afec.pix.sr <- matrix(0,nrow=nrow(liste.pix.sr),ncol=5)

for(j in 1:nrow(liste.pix.sr)){

for(i in 1:nrow(afec.sp)){

d

<-dist(matrix(c(liste.pix.sr[j,"xpix"],liste.pix.sr[j,"ypix"],liste.pix.sr[j,

"zpix"],afec.sp[i,"x"],afec.sp[i,"y"],afec.sp[i,"z"]),ncol=3,byrow=T))

dis_pix_centre[i]<- d }

afec.pix.sr[j,

]<-c(liste.pix.sr[j,],afec.sp[which.min(dis_pix_centre),"r"]) }

# Ajout de la colonne de afec.pix.sr contenant les rayons affectés aux pixels à la matrice afec

afec <- rbind(afec, afec.pix.sr)

#Pourcentage du volume de l'espace poral avant application du code en

7 for(i in 1:length(im1)){

im1.i <- im1[[i]]

nb.p <- c(nb.p, sum(im1.i)) }

percent_pore_befor

<-(sum(nb.p)/(nrow(im1[[1]])*ncol(im1[[1]])*length(im1)))*100

#Pourcentge du volume de l'espace poral aprés application du code

percent_pore_after

<-(nrow(afec)/(nrow(im1[[1]])*ncol(im1[[1]])*length(im1)))*100

#Application de la loi de Yound-Laplace pour vider les pores de grand rayon # Appel de la fonction YoungLaple

sigma <- 7.28 * 10^-2 # tension superficielle

resol_im #résolution des images

# Exemple resol_im <-44 pour l'image WO_MLH2046

afec_new <- YoungLaplace(P,sigma,resol_im,afec)

Etat.air <- rep(0,nrow(afec_new))

Etat.air[afec_new[,"Etat"]==0] <- 1

afec_new <- cbind(afec_new, Etat.air) # Pourcentage de la porosite remplie d'eau

(sum(afec_new[,"Etat"])/length(afec_new[,"Etat"]))*100 # Reconstruction de l'image a partir de afec_new

im1_recons_p <-list() # initialisation image reconstruite par rapport à la pression donnee

im1_recons_w <-list() # initialisation image reconstruite pore plein d'eau

im1_recons_a <-list() # initialisation image reconstruite pore plein d'air

im1_recons_r <-list() # initialisation image reconstruite par rapport au rayon

for(i in 1:length(im1)){

im1_recons_p[[i]]<-matrix(as.integer(1), nrow =

nrow(im1[[1]]),ncol=ncol(im1[[1]]))

im1_recons_w[[i]]<-matrix(as.integer(1), nrow =

nrow(im1[[1]]),ncol=ncol(im1[[1]]))

im1_recons_a[[i]]<-matrix(as.integer(1), nrow =

nrow(im1[[1]]),ncol=ncol(im1[[1]]))

im1_recons_r[[i]]<-matrix(NA, nrow = nrow(im1[[1]]),ncol=ncol(im1[[1]])) }

for(j in 1:nrow(afec)){

im1_recons_p[[afec_new[j,"z"]]][afec_new[j,"x"],afec_new[j,"y"

]]<-as.integer(255)

im1_recons_w[[afec_new[j,"z"]]][afec_new[j,"x"],afec_new[j,"y"

]]<-afec_new[j,"Etat"]*255

im1_recons_a[[afec_new[j,"z"]]][afec_new[j,"x"],afec_new[j,"y"

]]<-afec_new[j,"Etat.air"]*255

im1_recons_r[[afec_new[j,"z"]]][afec_new[j,"x"],afec_new[j,"y"

]]<-afec_new[j,"r.afec"] }

# Exportation de l'image reconstruite par rapport à la phase aqueuse de la porosité

ifelse(!dir.exists(file.path(paste("D://fdanfa//workspace//work//MOSAIC//WO

_MLH2046//Selection2//",processed.image, sep=""),

paste("res_WaterP",P,sep=""))),

8

//Selection2//",processed.image, sep=""), paste("res_WaterP",P,sep=""))),

FALSE)

for(j in 1:length(im1)){

if(j<10){

write_tif(im1_recons_w[[j]],

paste("D://fdanfa//workspace//work//MOSAIC//WO_MLH2046//Selection2//",proce

ssed.image,"//",paste("res_WaterP",P,sep=""),"//",processed.image,"_WaterP"

,P,"_00",j,".tiff", sep=""))

}

if(j>=10 & j<100){

write_tif(im1_recons_w[[j]],

paste("D://fdanfa//workspace//work//MOSAIC//WO_MLH2046//Selection2//",proce

ssed.image,"//",paste("res_WaterP",P,sep=""),"//",processed.image,"_WaterP"

,P,"_0",j,".tiff", sep=""))

}

if(j==100){

write_tif(im1_recons_w[[j]],

paste("D://fdanfa//workspace//work//MOSAIC//WO_MLH2046//Selection2//",proce

ssed.image,"//",paste("res_WaterP",P,sep=""),"//",processed.image,"_WaterP"

,P,"_",j,".tiff", sep=""))

} }

# Exportation de l'image reconstruite par raport à la phase gazeuse de la porosité

ifelse(!dir.exists(file.path(paste("D://fdanfa//workspace//work//MOSAIC//WO

_MLH2046//Selection2//",processed.image, sep=""),

paste("res_airP",P,sep=""))),

dir.create(file.path(paste("D://fdanfa//workspace//work//MOSAIC//WO_MLH2046 //Selection2//",processed.image, sep=""), paste("res_airP",P,sep=""))),

FALSE)

for(j in 1:length(im1)){

if(j<10){

write_tif(im1_recons_a[[j]],

paste("D://fdanfa//workspace//work//MOSAIC//WO_MLH2046//Selection2//",proce

ssed.image,"//",paste("res_airP",P,sep=""),"//",processed.image,"_airP",P," _00",j,".tiff", sep=""))

}

if(j>=10 & j<100){

write_tif(im1_recons_a[[j]],

paste("D://fdanfa//workspace//work//MOSAIC//WO_MLH2046//Selection2//",proce

ssed.image,"//",paste("res_airP",P,sep=""),"//",processed.image,"_airP",P," _0",j,".tiff", sep=""))

}

if(j==100){

write_tif(im1_recons_a[[j]],

paste("D://fdanfa//workspace//work//MOSAIC//WO_MLH2046//Selection2//",proce

ssed.image,"//",paste("res_airP",P,sep=""),"//",processed.image,"_airP",P," _",j,".tiff", sep=""))

} }

# Export de l'image reconstruite par rapport à la porosité totale

ifelse(!dir.exists(file.path(paste("D://fdanfa//workspace//work//MOSAIC//WO

_MLH2046//Selection2//",processed.image, sep=""),

paste("res_Ptot",sep=""))),

dir.create(file.path(paste("D://fdanfa//workspace//work//MOSAIC//WO_MLH2046 //Selection2//",processed.image, sep=""), paste("res_Ptot",sep=""))),

9 for(j in 1:length(im1)){

if(j<10){

write_tif(im1_recons_p[[j]],

paste("D://fdanfa//workspace//work//MOSAIC//WO_MLH2046//Selection2//",proce

ssed.image,"//",paste("res_Ptot",sep=""),"//",processed.image,"_Ptot_00",j,

".tiff", sep=""), bits_per_sample = 8)

}

if(j>=10 & j<100){

write_tif(im1_recons_p[[j]],

paste("D://fdanfa//workspace//work//MOSAIC//WO_MLH2046//Selection2//",proce

ssed.image,"//",paste("res_Ptot",sep=""),"//",processed.image,"_Ptot_0",j," .tiff", sep=""))

}

if(j==100){

write_tif(im1_recons_p[[j]],

paste("D://fdanfa//workspace//work//MOSAIC//WO_MLH2046//Selection2//",proce

ssed.image,"//",paste("res_Ptot",sep=""),"//",processed.image,"_Ptot_",j,". tiff", sep=""))

} }

# Exportation de l'image reconstruite par rapport au rayon attribué à chaque pixel.

ifelse(!dir.exists(file.path(paste("D://fdanfa//workspace//work//MOSAIC//WO

_MLH2046//Selection2//",processed.image, sep=""),

paste("res_Rtot",sep=""))),

dir.create(file.path(paste("D://fdanfa//workspace//work//MOSAIC//WO_MLH2046 //Selection2//",processed.image, sep=""), paste("res_Rtot",sep=""))),

FALSE)

for(j in 1:length(im1)){

if(j<10){

write_tif(im1_recons_r[[j]],

paste("D://fdanfa//workspace//work//MOSAIC//WO_MLH2046//Selection2//",proce

ssed.image,"//",paste("res_Rtot",sep=""),"//",processed.image,"_Rtot_00",j,

".tiff", sep=""), bits_per_sample = 32)

}

if(j>=10 & j<100){

write_tif(im1_recons_r[[j]],

paste("D://fdanfa//workspace//work//MOSAIC//WO_MLH2046//Selection2//",proce

ssed.image,"//",paste("res_Rtot",sep=""),"//",processed.image,"_Rtot_0",j," .tiff", sep=""))

}

if(j==100){

write_tif(im1_recons_r[[j]],

paste("D://fdanfa//workspace//work//MOSAIC//WO_MLH2046//Selection2//",proce

ssed.image,"//",paste("res_Rtot",sep=""),"//",processed.image,"_Rtot_",j,". tiff", sep=""))

} }

# Sauvegarde des images sous format tiff.

tiff(filename =

"D://fdanfa//workspace//work//MOSAIC//WO_MLH2046//Selection2//Sous_selectio n3//brute.tif")

plot(raster(im1[[10]]))

dev.off()

tiff(filename =

"D://fdanfa//workspace//work//MOSAIC//WO_MLH2046//Selection2//Sous_selectio n3//porosite.tif")

10 dev.off() tiff(filename = "D://fdanfa//workspace//work//MOSAIC//WO_MLH2046//Selection2//Sous_selectio n3//rayon.tif")

plot(raster(im1_recons_r[[10]]))

dev.off()

tiff(filename =

"D://fdanfa//workspace//work//MOSAIC//WO_MLH2046//Selection2//Sous_selectio n3//eauP023.tif")

plot(raster(im1_recons_w[[10]]))

dev.off()

# Sauvegarde des données

save(list = ls(),

file=paste("D://fdanfa//workspace//work//MOSAIC//WO_MLH2048//",processed.im

age,"//data.RData"))

# Fonction pour calculer les centres et les rayons des sphères de Delaunay #calcul du centre et du rayon d'une sphère passant par quatre points

circumcenter_radius <- function(P1,P2,P3,P4){ result<-list()

# le milieu de chaque arête du tétraèdre de points P1,P2,P3,P4

P12<-c((P1[1]+P2[1])/2,(P1[2]+P2[2])/2,(P1[3]+P2[3])/2)

P23<-c((P2[1]+P3[1])/2,(P2[2]+P3[2])/2,(P2[3]+P3[3])/2)

P34<-c((P3[1]+P4[1])/2,(P3[2]+P4[2])/2,(P3[3]+P4[3])/2)

# les coordonnées du vecteur directeur de chaque arête

V12<-c(P1[1]-P2[1],P1[2]-P2[2],P1[3]-P2[3])

V23<-c(P2[1]-P3[1],P2[2]-P3[2],P2[3]-P3[3])

V34<-c(P3[1]-P4[1],P3[2]-P4[2],P3[3]-P4[3])

#l'équation du plan qui passe par un point de coordonnées Xi,Yi,Zi;et qui est perpendiculaire

#a un vecteur de coordonnées Vx,Vy,Vz sera de la forme:

# Vx(x-Xi)+Vy(y-Yi)+Vz(z-Zi)=0

#le plan qui passe par P12 et qui es perpendiculaire au vecteur V12 aura pour équation: #V12[1](x-P12[1])+V12[2](y-P12[2])+V[3](z-P12[3])=0 (EP1) # De même on a #V23[1](x-P23[1])+V23[2](y-P23[2])+V23[3](z-P23[3])=0 (EP2) #V34[1](x-P34[1])+V34[2](y-P34[2])+V34[3](z-P34[3])=0 (EP1)

#Chacun de ces trois plans coupe la sphère en 2 parties égales, donc passe par le centre

# de la sphère. Le centre se trouve donc l'intersection de ces plans.

#On va résoudre une équation linéaire de la forme MX=b pour trouver le centre M <-matrix(c(V12[1],V12[2],V12[3],V23[1],V23[2],V23[3],V34[1],V34[2],V34[3]),nr ow=3,byrow=T) b1<-V12[1]*P12[1]+V12[2]*P12[2]+V12[3]*P12[3] b2<-V23[1]*P23[1]+V23[2]*P23[2]+V23[3]*P23[3] b3<-V34[1]*P34[1]+V34[2]*P34[2]+V34[3]*P34[3] b<-c(b1,b2,b3) if(m.inversible(M)==TRUE){ result[[1]]<-solve(M,b) result[[2]]<-sqrt((P1[1]-result[[1]][1])^2+(P1 [2]-result[[1]][2])^2+(P1[3]-result[[1]][3])^2) }else{

11 result[[2]]<- NA

}

return(result)

}

m.inversible <- function(m) class(try(solve(m),silent=T))=="matrix"

na.omit.list <- function(y) { return(y[!sapply(y, function(x)

all(is.na(x)))])

#Fonction pour appliquer Young-Laplace #P la pression exercée en pascal(Pa) #sigma la tension de surface

# resol_im la résolution de l'image en µm

# Ici l'angle de contact du liquide et le solide est supposé nul

# liste[i]=1 veut dire que le pore est plein d'eau (0 le pore est vide)

YoungLaplace=function(P,sigma,resol_im,afec){

liste<-c()

afec[,"r.afec"]<-afec[,"r.afec"]*resol_im Etat <- NA

Etat[afec[,"r.afec"]<((2*sigma)/P)*1000] <- 1

Etat[afec[,"r.afec"] >=((2*sigma)/P)*1000] <- 0

afec <- cbind(afec, Etat)

return(afec)

12

Documents relatifs