Read file tps

read.tps = function(data) {
  a = readLines(data)
  LM = grep("LM", a)
  ID.ind = grep("ID", a)  
  images = basename(gsub("(IMAGE=)(.*)", "\\2", a[ID.ind - 1]))
  
  skip = LM
  nrows = as.numeric(gsub("(LM=)([0-9])", "\\2", grep("LM", a, value=T)))
  l = length(LM)
  
  landmarks = vector("list", l)
  
  for (i in 1:l) {
    landmarks[i] = list(data.frame(
      read.table(file=data, header=F, skip=LM[i],
                 nrows=nrows[i], col.names=c("X", "Y")),
      IMAGE = images[i],
      ID = read.table(file=data, header=F, skip=ID.ind[i]-1, 
                      nrows=1, sep="=", col.names="ID")[2,],
      Scale = read.table(file=data, header=F, skip=ID.ind[i],
                         nrows=1, sep="=")[,2]))
  }
  do.call(rbind, landmarks)
}

Read the file TPS and convert it to csv, delete some unnecessary information

setwd("C:/Users/Admin/Documents/Intern/Intern")
data = read.tps("totlm4_F_Juillet_SL_CLIC.TPS")
write.csv(data,file="data1.csv")

Add isLandmark column to data2.csv

data1 <- read.csv("data2.csv")
data2 <- data.matrix(data1[,c("X","Y")])
head(data2)
##         X    Y
## [1,] 1698 1279
## [2,] 2103 1223
## [3,] 2290 1110
## [4,] 2364 1024
## [5,] 2408  927
## [6,] 2311  835

Distance to the center

center <- colMeans(data2)
center
##         X         Y 
## 1535.8889  911.8175
temp = data.frame
a = data.frame()
for (i in 1:length(data2[,1])){
  #print(i)
  distance <- sqrt((data2[i,1]-center[1])^2 + (data2[i,2]-center[2])^2)
  a <- rbind(a, distance)
  #print(distance)
}
colnames(a) <- c("Distance")
X <- cbind(data1,a)
d <- X[,c("X","Y","Distance","isLandmark")]
#write.csv(d, file= "Test.csv")
d1 <- data.matrix(d[,c("X","Y","Distance","isLandmark")])
head(d1)
##      X    Y Distance isLandmark
## 1 1698 1279 401.3764          1
## 2 2103 1223 646.8768          1
## 3 2290 1110 779.7178          1
## 4 2364 1024 835.6751          1
## 5 2408  927 872.2433          1
## 6 2311  835 778.9083          1
library(shapes)
## 
## Attaching package: 'shapes'
## The following object is masked from 'package:stats':
## 
##     sigma
plotshapes(d1,joinline=1:126)

#plot(d1)

Randomize position of landmark coordinates in matrix

rand.rowx <- d1[sample(nrow(d1)),]
rand <- write.csv(rand.rowx,file= "ranData.csv",quote=F,row.names=F,col.names=T)
## Warning in write.csv(rand.rowx, file = "ranData.csv", quote = F, row.names
## = F, : attempt to set 'col.names' ignored
rand.rowx
##        X    Y   Distance isLandmark
## 106 1501  514  399.34442          0
## 4   2364 1024  835.67514          1
## 89  2142 1217  678.60671          0
## 102 1807  563  441.78598          0
## 103 1691  536  406.56884          0
## 7   2155  726  646.39516          1
## 126 1004 1086  559.68326          0
## 75   490 1109 1064.31411          0
## 35  2276 1016  747.40783          0
## 25   616  776  929.86125          0
## 70  2245  839  712.84006          0
## 32  2181  834  649.78758          0
## 116 2424  895  888.27033          0
## 124 1097  954  440.91136          0
## 51  1585  518  396.86785          0
## 86  1899 1270  510.04354          0
## 107 1712  539  412.32024          0
## 23   538  834 1000.91847          0
## 112 2231 1238  767.83755          0
## 93  2374  869  839.20413          0
## 79  1001 1198  606.63545          0
## 9   1725  552  406.48692          1
## 17   506 1018 1035.34818          0
## 78   886 1174  700.78189          0
## 45  1166 1230  487.91179          0
## 96  2226  763  705.97449          0
## 72  1298 1024  263.01339          0
## 27  1410  572  362.38642          0
## 122 1697 1147  285.07476          0
## 41  1488 1022  120.13966          0
## 110  229  953 1307.53760          0
## 10  1242  847  300.95179          1
## 80  1089 1217  541.15253          0
## 90  2340 1066  818.75939          0
## 77   793 1158  782.61724          0
## 65  1815 1203  403.34884          0
## 14   881  944  655.67917          1
## 5   2408  927  872.24326          1
## 95  2266  789  740.36907          0
## 29  1777  557  428.98718          0
## 22   572  836  966.86611          0
## 91  2398  981  864.88253          0
## 117 2288  806  759.51857          0
## 56  1083  594  553.27776          0
## 55  1179  568  495.56042          0
## 52  1542  520  391.86511          0
## 121 1405 1057  195.47345          0
## 120 1491  882   53.88964          0
## 94  2347  852  813.31382          0
## 16   786 1092  771.23219          1
## 54  1284  547  443.32809          0
## 44   575 1127  984.68827          0
## 50  1864  586  462.40017          0
## 119 1923  658  462.90206          0
## 118 2101  722  596.13860          0
## 82  1430 1266  369.67246          0
## 105 1561  517  395.61521          0
## 64  1735 1204  353.57583          0
## 100 1907  602  483.43595          0
## 97  2199  737  685.76781          0
## 98  2126  705  625.30359          0
## 92  2401  895  865.27456          0
## 59   881  676  696.05268          0
## 39  1561  937   35.56302          0
## 125 1093  991  449.91159          0
## 30  1693  848  169.57762          0
## 101 1989  633  532.02336          0
## 85  1830 1282  472.79642          0
## 28  1699  563  385.06994          0
## 11  1218 1041  343.13478          1
## 62   456  845 1081.95406          0
## 123 1048  914  487.89377          0
## 24   546  808  995.31808          0
## 67  1785 1209  387.78062          0
## 46  1620 1288  385.47112          0
## 36  2021 1013  495.55090          0
## 40  1653  931  118.67174          0
## 47  1965 1264  555.12961          0
## 20   567  915  968.89412          0
## 115 2502  990  969.26941          0
## 37  1719 1018  211.67052          0
## 42  1403  523  410.89959          0
## 63   343  884 1193.21319          0
## 69  1989 1051  474.00576          0
## 58   951  650  640.81463          0
## 104 1643  525  401.37331          0
## 68  1635 1243  345.69479          0
## 71  2012  729  510.00394          0
## 1   1698 1279  401.37642          1
## 61   733  733  822.56079          0
## 73   302 1079 1245.16336          0
## 111 1882 1287  510.44573          0
## 31  1783  842  256.78469          0
## 66  1801 1214  401.99277          0
## 18   517  992 1022.03904          0
## 8   1947  626  500.70347          1
## 81  1308 1251  408.62959          0
## 53  1472  515  401.92771          0
## 12  1491 1135  227.65206          1
## 83  1532 1280  368.20308          0
## 48  2195 1195  717.37006          0
## 74   429 1101 1122.93947          0
## 57  1021  617  593.31939          0
## 49  2047  657  571.10989          0
## 33  2160  934  624.50520          0
## 21   585  876  951.56322          0
## 87  2049 1249  613.98296          0
## 34  2339  929  803.29490          0
## 99  2087  679  598.27036          0
## 76   683 1143  883.66556          0
## 3   2290 1110  779.71783          1
## 88  2260 1148  761.65550          0
## 38  1530  937   25.86193          0
## 2   2103 1223  646.87679          1
## 84  1764 1283  435.67322          0
## 113 2393 1145  888.26435          0
## 114 2461 1067  938.03635          0
## 108 1604  517  400.64941          0
## 19   600  963  937.28740          0
## 26  1445  699  231.41318          0
## 6   2311  835  778.90831          1
## 60   798  710  764.99039          0
## 43  1377  526  417.25387          0
## 13   831  906  704.91289          1
## 109  223  979 1314.60668          0
## 15   877  985  662.94061          1