Step03.R 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. setwd("D:/Cocorobo/test/RobustVideoMatting/test/")
  2. library(jpeg)
  3. library(plotrix)
  4. xstart=0; xend=90;
  5. ystart=0; yend=55;
  6. plot(NA,NA,xaxt='n', yaxt='n', xlim=c(xstart,xend),ylim=c(ystart,yend),xlab=NA, ylab=NA)
  7. final <- c()
  8. for (pic_input in 1001:1030){
  9. top_ratio = 0.2; bottom_ratio = 0.7
  10. #####第1步,打开指定图片,并进行裁剪
  11. {
  12. #####模拟化JPG图片,横坐标150,纵坐标按比例
  13. pic1 <- readJPEG(paste("02Shape/",pic_input,".jpg",sep=""))
  14. # pic1 <- pic1[,1:1400,]
  15. top = floor(top_ratio*nrow(pic1)); bottom = ceiling(bottom_ratio*nrow(pic1))
  16. pic2 <- pic1[,,1]*0.299 + pic1[,,2]*0.587 + pic1[,,3]*0.114;
  17. pic2[pic2 > 0.985] = 1
  18. pic3 <- (1-pic2)
  19. tmp = pic3[bottom,]; tmp[tmp > 0.01] = 1; tmp[tmp <= 0.01] = 0
  20. names(tmp) = 1:ncol(pic3)
  21. left = max(1,min(as.numeric(names(tmp[tmp == 1]))) - 100)
  22. right = min(max(as.numeric(names(tmp[tmp == 1]))) + 100,ncol(pic1))
  23. # left = as.numeric(names(tmp[tmp > 1][1]))
  24. # right = as.numeric(names(tmp[tmp > 1][length(tmp[tmp > 1])]))
  25. pic1 <- pic1[top:bottom,left:right,]
  26. wi = ceiling(1:ncol(pic1)/(ncol(pic1)/200))
  27. hi = ceiling(1:nrow(pic1)/(ncol(pic1)/200))
  28. npic1 <- array(NA, dim = c(max(hi),200,3))
  29. for (i in 1:200){
  30. for (j in 1:max(hi)){
  31. npic1[j,i,1] = mean(pic1[which(hi == j),which(wi == i),1])
  32. npic1[j,i,2] = mean(pic1[which(hi == j),which(wi == i),2])
  33. npic1[j,i,3] = mean(pic1[which(hi == j),which(wi == i),3])
  34. }
  35. }
  36. tmp = array(1,dim = c(nrow(npic1)+6,6+ncol(npic1),3))
  37. tmp[4:(3+nrow(npic1)),4:(3+ncol(npic1)),] <- npic1
  38. npic1 <- tmp;
  39. #####模拟化黑白图片
  40. pic2 <- pic1[,,1]*0.299 + pic1[,,2]*0.587 + pic1[,,3]*0.114
  41. npic2 <- array(NA, dim = c(max(hi),200))
  42. for (i in 1:200){
  43. for (j in 1:max(hi)){
  44. npic2[j,i] = mean(pic2[which(hi == j),which(wi == i)])
  45. }
  46. }
  47. tmp = array(1, dim = c(nrow(npic2)+6,6+ncol(npic2)))
  48. tmp[4:(3+nrow(npic2)),4:(3+ncol(npic2))] <- npic2
  49. npic2 <- tmp
  50. rm(tmp,wi,hi,i,j,left,right,pic2)
  51. print(paste("第",pic_input-1000,"帧,分析中..",sep=""))
  52. }
  53. #####第2步,黑白化并进行边缘化修整
  54. {
  55. #####黑白二维
  56. npic3 <- 1-npic2;
  57. npic3[npic3 < 0.01] = 0
  58. npic3[npic3 >= 0.01] = 1
  59. npic2 <- npic2*npic3
  60. print(paste("第",pic_input-1000,"帧,分析中...",sep=""))
  61. }
  62. ####第3步:确定平滑边缘
  63. {
  64. ####第三步,两轮裁剪
  65. {
  66. input = npic3
  67. for (n_round in 1:5){
  68. ####rounding
  69. {
  70. tmp <- as.data.frame(input)
  71. dim(tmp)
  72. ####left
  73. {
  74. picl <- cbind(tmp, rep(0,nrow(input)))
  75. picl <- picl[,2:ncol(picl)]
  76. dim(picl)
  77. picl <- as.matrix(picl)
  78. picll <- abs(input - picl)
  79. }
  80. ####right
  81. {
  82. picr <- cbind(tmp,rep(0,nrow(input)))
  83. picr <- picr[,c(ncol(picr),1:(ncol(picr)-2))]
  84. dim(picr)
  85. picr <- as.matrix(picr)
  86. picrr <- abs(input - picr)
  87. }
  88. ###bottom
  89. {
  90. picb <- rbind(tmp,rep(1,ncol(input)))
  91. picb <- picb[2:nrow(picb),]
  92. dim(picb)
  93. picb <- as.matrix(picb)
  94. picbb <- abs(input - picb)
  95. }
  96. ###top
  97. {
  98. pict <- rbind(tmp,rep(0,ncol(input)))
  99. pict <- pict[c(nrow(pict),1:(nrow(pict)-2)),]
  100. dim(pict)
  101. pict <- as.matrix(pict)
  102. pictt <- abs(input - pict)
  103. }
  104. tmp = picll+picrr+picbb+pictt
  105. tmp[tmp == 0] = 5
  106. tmp[tmp == 2] = 0
  107. tmp[tmp == 3] = 0
  108. tmp[tmp == 4] = 0
  109. tmp[tmp == 5] = 1
  110. tmp2 = input*tmp
  111. input <- tmp2
  112. }
  113. output2 <- tmp2
  114. npic1[,,1] <- npic1[,,1]*output2;
  115. npic1[,,2] <- npic1[,,2]*output2;
  116. npic1[,,3] <- npic1[,,3]*output2;
  117. npic3 <- npic3*output2
  118. ####确定surface
  119. {
  120. bk <- output2
  121. tmp = bk[nrow(bk)-3,]; names(tmp) = 1:ncol(bk)
  122. x_start = 1; tmp2 = as.numeric(names(tmp[tmp == 1]));
  123. for (i in 1:length(tmp2)){
  124. if (sum((bk[nrow(bk)-3,(tmp2[i]):(tmp2[i]+29)])) == 30){
  125. x_start = tmp2[i]; break;
  126. }
  127. }
  128. y_start = nrow(bk)-3
  129. surface <- data.frame()
  130. surface <- rbind(surface, c(x_start, y_start))
  131. colnames(surface) <- c("xpos","ypos")
  132. x_input = x_start; y_input = y_start;
  133. bk2 <- cbind(bk, rep(0,nrow(bk)))
  134. bk2 <- bk2[,c(ncol(bk2),1:ncol(bk2))]
  135. bk2 <- rbind(bk2, rep(1,ncol(bk2)))
  136. bk2 <- rbind(bk2, rep(0,ncol(bk2)))
  137. bk2 <- bk2[c(nrow(bk2),1:(nrow(bk2)-1)),]
  138. for (num in 1:10000){
  139. pos <- data.frame(xpos = x_input + c(-1,-1,-1,0,0,1,1,1),
  140. ypos = y_input + c(1,0,-1,1,-1,1,0,-1))
  141. pos <- pos[(pos$xpos >= 1) & (pos$xpos <= ncol(bk)) & (pos$ypos >= 1) & (pos$ypos <= nrow(bk)),]
  142. pos <- pos[!(paste(pos$xpos,pos$ypos) %in% paste(surface$xpos,surface$ypos)),]
  143. target <- data.frame()
  144. for (i in 1:nrow(pos)){
  145. if (bk[pos$ypos[i],pos$xpos[i]] == 1){
  146. tpos <- data.frame(xpos = pos$xpos[i] + c(-1,0,1,0),
  147. ypos = pos$ypos[i] + c(0,-1,0,1))
  148. # tpos <- data.frame(xpos = pos$xpos[i] + c(-1,-1,-1,0,0,1,1,1),
  149. # ypos = pos$ypos[i] + c(1,0,-1,1,-1,1,0,-1))
  150. va <- c()
  151. for (j in 1:nrow(tpos)){
  152. va = c(va, bk2[tpos$ypos[j]+1, tpos$xpos[j]+ 1])
  153. }
  154. if (length(va[va==0])>0){
  155. target <- rbind(target, c(pos$xpos[i],pos$ypos[i]))
  156. }
  157. }
  158. }
  159. if (nrow(target) < 1){ break; }
  160. colnames(target) = c("xpos","ypos")
  161. ####最短距离
  162. distance = sqrt((target$xpos - x_input)**2 + (target$ypos - y_input)**2)
  163. surface <- rbind(surface, c(target$xpos[order(distance)[1]],
  164. target$ypos[order(distance)[1]]))
  165. x_input = target$xpos[order(distance)[1]];
  166. y_input = target$ypos[order(distance)[1]];
  167. }
  168. if (length(surface$ypos[1:20][surface$ypos[1:20] == (nrow(npic3) - 3)]) > 10){
  169. surface <- data.frame(xpos = rev(surface$xpos), ypos = rev(surface$ypos))
  170. }
  171. surface2 <- surface[surface$ypos != (nrow(npic3) - 3),]
  172. judge1 = nrow(surface2[(surface2$xpos < 100) & (surface2$ypos < nrow(npic3)) & (surface2$ypos > (nrow(npic3)-20)),])
  173. judge2 = nrow(surface2[(surface2$xpos > 100) & (surface2$ypos < nrow(npic3)) & (surface2$ypos > (nrow(npic3)-20)),])
  174. }
  175. xmiddle = mean(surface2$xpos)
  176. if ((judge1 > 10) & (judge2 > 10)){
  177. break;
  178. }
  179. input <- output2
  180. }
  181. }
  182. ######
  183. {
  184. #####
  185. surface_left = data.frame()
  186. for (j in 1:nrow(surface)){
  187. if (surface$xpos[j] > xmiddle){break;}
  188. surface_left <- rbind(surface_left, c(surface$xpos[j], surface$ypos[j]))
  189. }
  190. #####
  191. surface_right <- data.frame()
  192. surface_right <- surface[(j+1):nrow(surface),]
  193. }
  194. if ((judge1 > 10) & (judge2 > 10) & (nrow(surface_left) > 100) & (nrow(surface_right) > 100)){
  195. colnames(surface_left) = c("xpos","ypos")
  196. surface_left <- data.frame(xpos = rev(surface_left$xpos), ypos = rev(surface_left$ypos))
  197. colnames(surface_right) = c("xpos","ypos")
  198. #####第五步,计算差异点
  199. {
  200. tmp1 = array(1,dim = c(nrow(npic1),ncol(npic1)))
  201. tmp1[1:(nrow(tmp1)-1),] <- npic1[2:nrow(npic1),,1]
  202. tmp2 = array(1,dim = c(nrow(npic1),ncol(npic1)))
  203. tmp2[1:(nrow(tmp2)-1),] <- npic1[2:nrow(npic1),,2]
  204. tmp3 = array(1,dim = c(nrow(npic1),ncol(npic1)))
  205. tmp3[1:(nrow(tmp3)-1),] <- npic1[2:nrow(npic1),,3]
  206. tmp = abs(npic1[,,1] - tmp1) + abs(npic1[,,2] - tmp2) + abs(npic1[,,3] - tmp3)
  207. tmp[tmp >= 0.15] = 1
  208. tmp[tmp < 1] = 0
  209. ###去掉边缘点
  210. target <- data.frame()
  211. for (i in 3:(nrow(tmp)-3)){
  212. for (j in 3:(ncol(tmp)-3)){
  213. if (tmp[i,j] == 1){
  214. tmp2 = c(npic3[i-1,j+1],npic3[i-1,j],npic3[i-1,j-1],
  215. npic3[i,j+1],npic3[i,j-1],
  216. npic3[i+1,j+1],npic3[i+1,j],npic3[i+1,j-1]);
  217. if (length(tmp2[tmp2 == 0]) == 0){
  218. target <- rbind(target, c(i,j))
  219. }
  220. }
  221. }
  222. }
  223. colnames(target) <- c("ypos","xpos")
  224. test = table(target$ypos)
  225. candidate <- as.numeric(names(test[test >= 10]))
  226. rm(i,j,test,tmp,tmp1,tmp2,tmp3)
  227. print(paste("第",pic_input-1000,"帧,分析中.....",sep=""))
  228. }
  229. if (length(candidate) > 0){ #####第六步,找到脖子曲线对应的边缘曲线
  230. {
  231. #####
  232. pointl <- c(); pointr <- c()
  233. for (i in 1:length(candidate)){
  234. for (j in 1:nrow(surface_left)){
  235. if (surface_left$ypos[j] >= candidate[i]){ break; }
  236. }
  237. partl <- surface_left[j:nrow(surface_left),]
  238. if (nrow(partl) > 40){
  239. dis1 = (partl$xpos[5] - partl$xpos[10:40]);
  240. dis2 = sqrt((partl$xpos[5] - partl$xpos[10:40])**2 + (partl$ypos[5] - partl$ypos[10:40])**2);
  241. angle1 = mean(180*acos(dis1/dis2)/pi)
  242. if ((angle1 < 70) & (candidate[i] >= min(surface$ypos))){
  243. pointl <- c(pointl, candidate[i])
  244. }
  245. }
  246. for (j in 1:nrow(surface_right)){
  247. if (surface_right$ypos[j] >= candidate[i]){ break; }
  248. }
  249. partr <- surface_right[j:nrow(surface_right),]
  250. if (nrow(partr) > 40){
  251. dis1 = (partr$xpos[5] - partr$xpos[10:40]);
  252. dis2 = sqrt((partr$xpos[5] - partr$xpos[10:40])**2 + (partr$ypos[1] - partr$ypos[10:40])**2);
  253. angle1 = mean(180*acos(dis1/dis2)/pi)
  254. if ((angle1 > 110) & (candidate[i] >= min(surface$ypos))){
  255. pointr <- c(pointr, candidate[i])
  256. }
  257. }
  258. }
  259. points <- intersect(pointl, pointr)
  260. if (length(points) > 1){
  261. points <- points[points < (min(surface$ypos)+100)]
  262. points <- sort(points, decreasing = T)
  263. point_tmp <- data.frame()
  264. for (i in 1:(length(points)-1)){
  265. for (j in 1:length(points)){
  266. if ((i + j) > length(points)){break;}
  267. if ((points[i] - j) != points[i + j]){ break;}
  268. }
  269. point_tmp <- rbind(point_tmp, c(points[i],j))
  270. }
  271. colnames(point_tmp) <- c("v1","v2")
  272. if (nrow(point_tmp[(point_tmp$v2 > 1),]) > 1){
  273. bottom = point_tmp[(point_tmp$v2 > 1),]$v1[1];
  274. top = point_tmp[(point_tmp$v2 > 1),]$v1[1] - point_tmp[(point_tmp$v2 > 1),]$v2[1] + 1;
  275. } else{
  276. top = point_tmp$v1[1];
  277. bottom = point_tmp$v1[1];
  278. }
  279. #####partl
  280. {
  281. for (j in 1:nrow(surface_left)){
  282. if (surface_left$ypos[j] >= top){ break; }
  283. }
  284. partl <- surface_left[j:nrow(surface_left),]
  285. }
  286. #####partr
  287. {
  288. for (j in 1:nrow(surface_right)){
  289. if (surface_right$ypos[j] >= top){ break; }
  290. }
  291. partr <- surface_right[j:nrow(surface_right),]
  292. }
  293. #####
  294. dis1 = xmiddle - min(partl[(partl$ypos <= floor(top + 30)),]$xpos)
  295. dis2 = max(partr[(partr$ypos <= floor(top + 30)),]$xpos) - xmiddle;
  296. leftmin = xmiddle - floor(0.8*min(dis1,dis2));
  297. rightmax = xmiddle + floor(0.8*min(dis1,dis2));
  298. #####partl2
  299. {
  300. for (j in 1:nrow(partl)){
  301. if (partl$xpos[j] < leftmin){ break; }
  302. }
  303. partl2 <- partl[1:j,]
  304. }
  305. #####partr2
  306. {
  307. for (j in 1:nrow(partr)){
  308. if (partr$xpos[j] > rightmax){ break; }
  309. }
  310. partr2 <- partr[1:j,]
  311. }
  312. #############
  313. target2 <- target[(target$xpos > partl$xpos[1]) & (target$xpos < partr$xpos[1]) & (target$ypos >= top) & (target$ypos <= bottom),]
  314. xpos <- as.numeric(names(table(target2$xpos)))
  315. ypos <- c()
  316. for (i in 1:length(xpos)){
  317. ypos <- c(ypos, mean(target2$ypos[which(target2$xpos == xpos[i])]))
  318. }
  319. xposl <- as.numeric(names(table(partl2$xpos)))
  320. yposl <- c()
  321. for (i in 1:length(xposl)){
  322. yposl <- c(yposl, mean(partl2$ypos[which(partl2$xpos == xposl[i])]))
  323. }
  324. #####
  325. xposr <- as.numeric(names(table(partr2$xpos)))
  326. yposr <- c()
  327. for (i in 1:length(xposr)){
  328. yposr <- c(yposr, mean(partr2$ypos[which(partr2$xpos == xposr[i])]))
  329. }
  330. ###############
  331. xpos2 <- c(xposl,xpos,xposr); ypos2 <- c(yposl, ypos, yposr)
  332. input <- data.frame(xpos = xpos2, ypos = ypos2)
  333. loessMod10 <- loess(ypos ~ xpos, data=input, span=0.3)
  334. smoothedleft <- predict(loessMod10, newdata = (xmiddle - 10:floor(0.8*min(dis1,dis2))))
  335. smoothedright<- predict(loessMod10, newdata = (xmiddle + 10:floor(0.8*min(dis1,dis2))))
  336. RES <- data.frame(dis = 2*(10:floor(0.8*min(dis1,dis2))),
  337. left = smoothedleft, right = smoothedright)
  338. angle = 0;
  339. for (i in 1:nrow(RES)){
  340. if ((!is.na(RES$left[i])) & (!is.na(RES$right[i]))){
  341. ang = atan(abs(RES$left[i] - RES$right[i])/RES$dis[i])*180/pi
  342. # print(ang)
  343. if (ang > angle){ angle = ang}
  344. }
  345. }
  346. } else{
  347. #####
  348. for (j in 1:nrow(surface_left)){
  349. if (surface_left$ypos[j] > (min(surface$ypos) +30)){ break; }
  350. }
  351. partl <- surface_left[1:j,]
  352. dis1 = xmiddle - partl$xpos[j]
  353. #####
  354. for (j in 1:nrow(surface_right)){
  355. if (surface_right$ypos[j] > (min(surface$ypos) +30)){ break; }
  356. }
  357. partr <- surface_right[1:j,]
  358. dis2 = partr$xpos[j] - xmiddle;
  359. leftmin = xmiddle - floor(0.8*min(dis1,dis2));
  360. rightmax = xmiddle + floor(0.8*min(dis1,dis2));
  361. ####
  362. xposl <- as.numeric(names(table(partl$xpos)))
  363. yposl <- c()
  364. for (i in 1:length(xposl)){
  365. yposl <- c(yposl, mean(partl$ypos[which(partl$xpos == xposl[i])]))
  366. }
  367. #####
  368. xposr <- as.numeric(names(table(partr$xpos)))
  369. yposr <- c()
  370. for (i in 1:length(xposr)){
  371. yposr <- c(yposr, mean(partr$ypos[which(partr$xpos == xposr[i])]))
  372. }
  373. ###############
  374. xpos2 <- c(xposl,xposr); ypos2 <- c(yposl, yposr)
  375. input <- data.frame(xpos = xpos2, ypos = ypos2)
  376. loessMod10 <- loess(ypos ~ xpos, data=input, span=0.3)
  377. smoothedleft <- predict(loessMod10, newdata = (xmiddle - 10:floor(0.8*min(dis1,dis2))))
  378. smoothedright<- predict(loessMod10, newdata = (xmiddle + 10:floor(0.8*min(dis1,dis2))))
  379. RES <- data.frame(dis = 2*(10:floor(0.8*min(dis1,dis2))),
  380. left = smoothedleft, right = smoothedright)
  381. angle = 0;
  382. for (i in 1:nrow(RES)){
  383. if ((!is.na(RES$left[i])) & (!is.na(RES$right[i]))){
  384. ang = atan(abs(RES$left[i] - RES$right[i])/RES$dis[i])*180/pi
  385. # print(ang)
  386. if (ang > angle){ angle = ang}
  387. }
  388. }
  389. }
  390. }} else{
  391. #####
  392. for (j in 1:nrow(surface_left)){
  393. if (surface_left$ypos[j] > (min(surface$ypos) +30)){ break; }
  394. }
  395. partl <- surface_left[1:j,]
  396. dis1 = xmiddle - partl$xpos[j]
  397. #####
  398. for (j in 1:nrow(surface_right)){
  399. if (surface_right$ypos[j] > (min(surface$ypos) +30)){ break; }
  400. }
  401. partr <- surface_right[1:j,]
  402. dis2 = partr$xpos[j] - xmiddle;
  403. leftmin = xmiddle - floor(0.8*min(dis1,dis2));
  404. rightmax = xmiddle + floor(0.8*min(dis1,dis2));
  405. ####
  406. xposl <- as.numeric(names(table(partl$xpos)))
  407. yposl <- c()
  408. for (i in 1:length(xposl)){
  409. yposl <- c(yposl, mean(partl$ypos[which(partl$xpos == xposl[i])]))
  410. }
  411. #####
  412. xposr <- as.numeric(names(table(partr$xpos)))
  413. yposr <- c()
  414. for (i in 1:length(xposr)){
  415. yposr <- c(yposr, mean(partr$ypos[which(partr$xpos == xposr[i])]))
  416. }
  417. ###############
  418. xpos2 <- c(xposl,xposr); ypos2 <- c(yposl, yposr)
  419. input <- data.frame(xpos = xpos2, ypos = ypos2)
  420. loessMod10 <- loess(ypos ~ xpos, data=input, span=0.3)
  421. smoothedleft <- predict(loessMod10, newdata = (xmiddle - 10:floor(0.8*min(dis1,dis2))))
  422. smoothedright<- predict(loessMod10, newdata = (xmiddle + 10:floor(0.8*min(dis1,dis2))))
  423. RES <- data.frame(dis = 2*(10:floor(0.8*min(dis1,dis2))),
  424. left = smoothedleft, right = smoothedright)
  425. angle = 0;
  426. for (i in 1:nrow(RES)){
  427. if ((!is.na(RES$left[i])) & (!is.na(RES$right[i]))){
  428. ang = atan(abs(RES$left[i] - RES$right[i])/RES$dis[i])*180/pi
  429. # print(ang)
  430. if (ang > angle){ angle = ang}
  431. }
  432. }
  433. }
  434. } else{
  435. angle = 0
  436. }
  437. }
  438. #####第七步,输出角度
  439. {
  440. print(paste("第",pic_input-1000,"帧,偏离角度为",angle,sep=""))
  441. }
  442. #####输出波形图
  443. {
  444. dev.off()
  445. xs = 0; xe = 100; width = xe - xs;
  446. ys = 0; ye = (xe-xs)*(dim(npic1)[1]/dim(npic1)[2]); height = ye - ys;
  447. xstart=0; xend=width;
  448. ystart=0; yend=height;
  449. plot(NA,NA,xaxt='n', yaxt='n', xlim=c(xstart,xend),ylim=c(ystart,yend),xlab=NA, ylab=NA)
  450. rasterImage(npic2, xleft = 0, xright = 100, ytop = ye, ybottom = ys)
  451. for (i in 1:nrow(surface)){
  452. draw.ellipse(x = width*surface$xpos[i]/ncol(npic3),
  453. y = ye - height*surface$ypos[i]/nrow(npic3), a = 0.2, b = 0.2, lwd = 1, border = "cyan", col = "cyan")
  454. }
  455. lines( ye - height*smoothedleft/nrow(npic3), x=width*(xmiddle - 10:floor(0.8*min(dis1,dis2)))/ncol(npic3), col="purple", lwd = 10)
  456. lines( ye - height*smoothedright/nrow(npic3), x=width*(xmiddle + 10:floor(0.8*min(dis1,dis2)))/ncol(npic3), col="purple", lwd = 10)
  457. segments(x0 = width*xmiddle/ncol(npic3), y0 = ys, y1 = ye, lwd = 10, col = "white")
  458. text(x = 2, y = height - 5, labels = paste("第",pic_input-1000,"帧"), font = 2, cex = 2, adj = 0, col = "white")
  459. text(x = 2, y = height - 15, labels = paste("偏离角度",signif(angle, digits = 3)), font = 2, cex = 2, adj = 0, col = "white")
  460. }
  461. final <- c(final, angle)
  462. }
  463. #####
  464. {
  465. dev.off()
  466. xstart=0; xend=100;
  467. ystart=0; yend=100;
  468. plot(NA,NA,xaxt='n', yaxt='n', xlim=c(xstart,xend),ylim=c(ystart,yend),xlab=NA, ylab=NA)
  469. text(x = 50, y = 60, labels = paste("输出波形图"), font = 2, cex = 4, col = "lightpink4")
  470. Sys.sleep(1)
  471. }
  472. All <- data.frame()
  473. for (i in 1:23){
  474. file = paste("final/final",i,".out", sep = "")
  475. tmp = read.csv(file, header = T);
  476. All <- rbind(All, tmp$x)
  477. }
  478. value = colMeans(All)
  479. #####输出波形图
  480. {
  481. dev.off()
  482. xstart=0; xend=90;
  483. ystart=0; yend=55;
  484. jpeg("波形图.jpg", width=(xend-xstart), height=(yend-ystart), unit="cm", res=100)
  485. plot(NA,NA,xaxt='n', yaxt='n', xlim=c(xstart,xend),ylim=c(ystart,yend),xlab=NA, ylab=NA)
  486. input = read.csv("final.out", header = T)
  487. ymax = 15; ymin = -1; bwidth = 80; bheight = 40;
  488. segments(x0 = 10, x1 = 10 + bwidth, y0 = 10 + bheight*(5-ymin)/(ymax-ymin), lwd = 5, col = "grey")
  489. segments(x0 = 10, x1 = 10 + bwidth, y0 = 10 + bheight*(10-ymin)/(ymax-ymin), lwd = 5, col = "grey")
  490. segments(x0 = 10, x1 = 10 + bwidth, y0 = 10 + bheight*(15-ymin)/(ymax-ymin), lwd = 5, col = "grey")
  491. segments(x0 = 10, x1 = 10 + bwidth, y0 = 10 + bheight*(20-ymin)/(ymax-ymin), lwd = 5, col = "grey")
  492. xpos = 10 + bwidth*(1:30)/31;
  493. ypos = 10 + bheight*(value - ymin)/(ymax - ymin)
  494. lines(xpos, ypos, lwd = 3, col = "cornflowerblue")
  495. xpos = 10 + bwidth*(1:30)/31;
  496. ypos = 10 + bheight*(final - ymin)/(ymax - ymin)
  497. lines(xpos, ypos, lwd = 5, col = "red")
  498. count = 0;
  499. for (i in 1:30){
  500. if ((final[i] > value[i]) & (final[i] > 5)){
  501. count = count + 1
  502. }
  503. }
  504. rate = signif(1.4*100*count/30, digits = 2)
  505. segments(x0 = 10 + bwidth*c(1,5,10,15,20,25,30)/31, y0 = 10, y1 = 9, lwd = 5)
  506. text(x = 10 + bwidth*c(1,5,10,15,20,25,30)/31, y = 7, labels = c(1,5,10,15,20,25,30), font = 2, cex = 2)
  507. # text(x = 10 + bwidth/2, y = 5, labels = "帧数", font = 2, cex = 2)
  508. rect(xleft = 10, xright = 10 + bwidth, ytop = 10 + bheight, ybottom = 10, lwd = 5)
  509. segments(x0 = 10, x1 = 9, y0 = 10 + bheight*(c(0,5,10,15,20,25,30) - ymin)/(ymax - ymin), lwd = 5)
  510. text(x = 8, y = 10 + bheight*(c(0,5,10,15,20,25,30) - ymin)/(ymax - ymin), labels = c(0,5,10,15,20,25,30), font = 2, cex = 2, adj = 1)
  511. text(x = 2, y = 10 + bheight/2, labels = "偏离角度", font = 2, cex = 2, srt = 90)
  512. text(x = 10 + bwidth/2, y = 10 + bheight + 3, labels = paste("侧凸概率 ",rate,"%",sep = ""), font = 2, cex = 3)
  513. segments(x0 = 10, x1 = 20, y0 = 2, col = "red", lwd = 5)
  514. text(x = 21, y = 2, labels = "受检者侧凸曲线", font = 2, cex = 2, adj = 0)
  515. segments(x0 = 50, x1 = 60, y0 = 2, col = "cornflowerblue", lwd = 5)
  516. text(x = 61, y = 2, labels = "标准曲线", font = 2, cex = 2, adj = 0)
  517. dev.off()
  518. }