{"cells":[{"metadata":{"_uuid":"a9f7a43e1b20bf49bb9146dd920b8076f9d8817d","_cell_guid":"c9b0228d-4cdf-4891-a04b-f7e74925a588"},"cell_type":"markdown","source":"## Read Data"},{"metadata":{"_uuid":"a4f497d9fc5e386746b3ce0ee7ec72d1bc3a060c","_cell_guid":"9f5edd4e-4128-4b7e-87af-406bb63ac126"},"cell_type":"markdown","source":"### This is a simple data exploratory based on [Exploratory Analysis](https://www.kaggle.com/jerrythomas/exploratory-analysis)  by Jerry Thomas ,  [Exploratory Analysis - image stats](https://www.kaggle.com/etheleon/exploratory-analysis-image-stats) by Wesley Goi and [Data visualization and analysis](https://www.kaggle.com/pudae81/data-visualization-and-analysis) bypudae\n### I am completely new to image process and computer version, I am just try to play with R.  Any suggestion and comments are very welcome."},{"metadata":{"_uuid":"b5a096209fcfc06b7b46185bb95d14a3248b1bdd","trusted":true,"_cell_guid":"a47937c4-3a8f-4bcf-9a3b-87ea600c0a4d"},"cell_type":"code","source":"library(keras)\nlibrary(tidyverse)\nlibrary(EBImage)\nlibrary(dplyr)\nlibrary(gridExtra)\nlibrary(ggplot2)\nlibrary(lattice)\nlibrary(ggpubr)\noptions(EBImage.display = \"raster\") ","execution_count":1,"outputs":[]},{"metadata":{"_uuid":"86b115a9ace0b38f2cec2fe568e077494a3746dd","trusted":true,"_cell_guid":"34021fdc-b80f-4f13-8e5e-523f4f6f3ab6"},"cell_type":"code","source":"TRAIN_PATH <- '../input/stage1_train/'\nTEST_PATH <-'../input/stage1_test/'\nOUTPUT_PATH <-'/'","execution_count":2,"outputs":[]},{"metadata":{"_uuid":"0dc59f7d606b24f2ad86f170fbd685c64e9ac32f","trusted":true,"_cell_guid":"45c25dab-ad93-4af7-8400-472a4101bd3e"},"cell_type":"code","source":"labels.df<-suppressMessages(read_csv(\"../input/stage1_train_labels.csv\"))\nhead(labels.df,n=2)","execution_count":3,"outputs":[]},{"metadata":{"_uuid":"bf046083eb67036cad5fb9496dd5510fff21d786","trusted":true,"_cell_guid":"509d8d73-001b-43ab-8d48-15a47167770c"},"cell_type":"code","source":"train_id <- list.files(TRAIN_PATH)\ntest_id <- list.files(TEST_PATH)","execution_count":4,"outputs":[]},{"metadata":{"_uuid":"7d782a05717e1d2a6c279e0d7ebc0d918c579ab4","trusted":true,"_cell_guid":"4b6f0e7e-20e4-48d3-9e27-f599e23d2b8a"},"cell_type":"code","source":"\ntrain.data.df <- data.frame(id=train_id,train_test=rep('train',length(train_id)))\ntest.data.df <- data.frame(id=test_id,train_test=rep('test',length(test_id)))\ntotal.data.df <- rbind(train.data.df,test.data.df)\nhead(total.data.df)","execution_count":5,"outputs":[]},{"metadata":{"_uuid":"30d860622590e00b59f4608b455c965882dd7653","trusted":true,"_cell_guid":"5f6e6f39-0c21-4d15-afc6-18aabfb89cee"},"cell_type":"code","source":"total.data.sum <- total.data.df %>% group_by(train_test) %>% summarize(id = n())\ntotal.data.sum","execution_count":6,"outputs":[]},{"metadata":{"_uuid":"cb8870ca227498d793a8a4f4bf5ea002c28f3a5b","trusted":true,"_cell_guid":"dbedd4b5-3f23-4438-a6e0-1a1475a9be16"},"cell_type":"code","source":"total.data.df <- total.data.df %>% mutate(path=paste('../input/stage1_',train_test,'/',id,'/images/',id,'.png',sep = \"\"))\ntotal.data.df<-total.data.df %>% mutate(mask_path=paste('../input/stage1_train/',id,'/masks',sep = \"\"))\ntail(total.data.df,n=2)","execution_count":7,"outputs":[]},{"metadata":{"_uuid":"17283400d7334a89732ba9eca5b616cd893bb57e","trusted":true,"_cell_guid":"4bac4532-b0bf-4cce-8662-ea14fb54d760"},"cell_type":"code","source":"library(cluster)\nk.means.cluster <- function(imx,title){\n    d <- dist(imx, method=\"euclidian\")   \n    kfit <- kmeans(d, 30)   \n    clusplot(as.matrix(d), kfit$cluster, color=T, shade=T, labels=20, lines=0,main = title,xlab = 'X',ylab = 'Y')  \n}\n","execution_count":8,"outputs":[]},{"metadata":{"_uuid":"54dee1bf29cf4c5a45c0291a9ba78ee76d13fb69","trusted":true,"_cell_guid":"0d6b8f76-e186-434f-9029-847d4e35f3d9"},"cell_type":"code","source":"img <- readImage(total.data.df$path[4])\ndisplay(img)\nstr(img)\n# dim(img.mtx) <- dim(img)\nimageData(img)[1:6, 1:8,2]","execution_count":9,"outputs":[]},{"metadata":{"_uuid":"31c4cfbc239aab94a5df8808438528b33e7d014b","trusted":true,"_cell_guid":"3b400160-668d-4a27-b7c7-609a7d4e5e31"},"cell_type":"code","source":"hist(img)\nrangeVal = range(img)\nmessage(sprintf(\"Ranges from %s to %s\", rangeVal[[1]] , rangeVal[[2]]))","execution_count":10,"outputs":[]},{"metadata":{"_uuid":"badb044eb89480af195b71437e097167957fb6e3","trusted":true,"_cell_guid":"701aef81-c947-4901-abe5-29988d302ae6"},"cell_type":"code","source":"# k.means.cluster(imageData(img)[1:320, 1:256,1])\n# par(mfrow = c(1, 2)) \nimg.dim <- dim(img)\n\nimx1 <- imageData(img)[1:img.dim[1], 1:img.dim[2],1]\nk.means.cluster(imx1,\"Chennel1\")\nimx2 <- imageData(img)[1:img.dim[1], 1:img.dim[2],2]\nk.means.cluster(imx1,\"Chennel2\")\n\nimx2 <- imageData(img)[1:img.dim[1], 1:img.dim[2],3]\nk.means.cluster(imx1,\"Chennel3\")\n\n\n","execution_count":11,"outputs":[]},{"metadata":{"_uuid":"322699219fcc2e98020671ae4fe0e74c8aa5cb13","_cell_guid":"b939aee6-f3cc-4e83-bf74-2c8ed882b314"},"cell_type":"markdown","source":"##  Linear filters"},{"metadata":{"_uuid":"aae45799cc475159cd9740ea06b6dd841ee98063","trusted":true,"_cell_guid":"486674af-f5f2-4f7f-987f-5b935de866dc"},"cell_type":"code","source":"w = makeBrush(size = 31, shape = 'gaussian', sigma = 5)\nplot(w[(nrow(w)+1)/2, ], ylab = \"w\", xlab = \"\", cex = 0.7)","execution_count":12,"outputs":[]},{"metadata":{"_uuid":"6b4fa78756aa8dc83a16560104e218d9f09c32cb","trusted":true,"_cell_guid":"bd6d391d-21cd-45e2-842f-e65ea2ea6f6f"},"cell_type":"code","source":"img <- readImage(total.data.df$path[3])\nimg_flo = filter2(img, w)\ndisplay(img_flo)","execution_count":13,"outputs":[]},{"metadata":{"_uuid":"c2fd99b0971deea4d82c7267a01b7a2f5fdb9339","_cell_guid":"8df90cc4-59c1-43bb-b78d-174e487ab020"},"cell_type":"markdown","source":"## Gray scale"},{"metadata":{"_uuid":"217162fea7720a00c56a7cf08b67023eea6d3b6a","trusted":true,"_cell_guid":"0736803e-db73-445b-99c7-9fdd759681ba"},"cell_type":"code","source":"img <- readImage(total.data.df$path[4])\ncolorMode(img) = Grayscale\ndisplay(img, all=TRUE)","execution_count":14,"outputs":[]},{"metadata":{"_uuid":"ef7bad3f7a72bef3c0d41f856969c2210e7a19bf","_cell_guid":"5376aa1f-541f-4980-86b4-9eaa5967747c"},"cell_type":"markdown","source":"## Laplacian filter"},{"metadata":{"_uuid":"b03818510e84b4fef92edd137ef945865bf91460","trusted":true,"_cell_guid":"f37e9cc8-2e87-47bb-ae38-645d1f763a91"},"cell_type":"code","source":"img <- readImage(total.data.df$path[1])\nfhi = matrix(1, nrow = 3, ncol = 3)\nfhi[2, 2] = -2\ncolorMode(img) = Grayscale\nimg_fhi = filter2(img, fhi)\ndisplay(img,all = TRUE)\ndisplay(img_fhi,all = TRUE)","execution_count":15,"outputs":[]},{"metadata":{"_uuid":"9da6f67b28dab0d1e6b5c5aa0fc2b1c8d2cb7942","_cell_guid":"fb898f2e-6702-433b-94b1-154e73580c92"},"cell_type":"markdown","source":"> ## negative image"},{"metadata":{"_uuid":"00d5042f6aace0bd366e08d81c08a735b0e5a5a6","trusted":true,"_cell_guid":"f02ca9b7-ec2d-4ce1-b98e-b4aeecb1d591"},"cell_type":"code","source":"img_neg = max(img) - img\ndisplay( img,all = TRUE)\ndisplay( img_neg,all = TRUE)","execution_count":16,"outputs":[]},{"metadata":{"_uuid":"442661866aa5cfe000e5ce07f0de6d9a7b136248","_cell_guid":"de8d9cad-0712-49af-89fd-ada6e44f6851"},"cell_type":"markdown","source":"## crop and threshold images"},{"metadata":{"_uuid":"865de86b9d137051762eec692f28475d3629892c","trusted":true,"_cell_guid":"d84802ad-f4db-438b-aa16-a025fe28457f"},"cell_type":"code","source":"img <- readImage(total.data.df$path[3])\nimg_thresh = img > .2\ndisplay(img_thresh,all = TRUE)","execution_count":17,"outputs":[]},{"metadata":{"_uuid":"605c7cfd3be72c3427aa294af376871d47de781d","_cell_guid":"373f0cbe-3ba1-4dae-844c-e53f824a5024"},"cell_type":"markdown","source":"## Voronoi-Based Segmentation"},{"metadata":{"_uuid":"0561171d1148dc1de641ab9ef17d3132d425e384","trusted":true,"_cell_guid":"548f0164-fe88-4959-9716-e6ad04bda37d"},"cell_type":"code","source":"img <- readImage(total.data.df$path[1])\nnmask = thresh(img, w=10, h=10, offset=0.05)\nctmask = opening(img>0.1, makeBrush(5, shape='disc'))\ncmask = propagate(img, seeds=nmask, mask=ctmask)\ndisplay(ctmask, all=TRUE)\ndisplay(nmask, all=TRUE)\ndisplay(img)","execution_count":18,"outputs":[]},{"metadata":{"_uuid":"5bacb439e6e6c238c2d70c0886df66bf738e3a9a","trusted":true,"_cell_guid":"3a1336d6-9d20-4b84-9e3f-584bb182a493"},"cell_type":"code","source":"dim(total.data.df)","execution_count":19,"outputs":[]},{"metadata":{"_uuid":"42f0e9a14c847137eac178b65ed7576f04884ede","trusted":true,"_cell_guid":"d9786308-e79c-4afa-aaf5-d41a4241df2c"},"cell_type":"code","source":"length(total.data.df$path)","execution_count":20,"outputs":[]},{"metadata":{"_uuid":"badc9930f0cbd341d98d95b762c1164af1f6d3fb","trusted":true,"_cell_guid":"1ef6fff8-06f1-4a89-a41a-2280a5d763c3"},"cell_type":"code","source":"basic_feature <- function(path_list,mask_path){\n    width <- c()\n    hight <- c()\n    area <- c()\n    nuclei_cnt<-c()\n    len = length(path_list)\n    for (i in 1:len){\n        img <- readImage(path_list[i])\n        w <- dim(img)[1]\n        h <- dim(img)[2]\n        a <- w * h\n        width <- c(width,w)\n        hight <- c(hight,h)\n        area <- c(area,a)  \n        \n        cnt <- length(list.files(mask_path[i]))\n        nuclei_cnt <-c(nuclei_cnt,cnt)\n        \n    } \n    print(length(nuclei_cnt))\n\n    data.frame(width=width,hight=hight,area=area,nuclei_cnt=nuclei_cnt)\n}","execution_count":21,"outputs":[]},{"metadata":{"_uuid":"250a91466150be6cd96cad4a1f7cf5d93e1c42db","trusted":true,"_cell_guid":"d02fb352-1cc6-4011-abf8-54f04f61e764"},"cell_type":"code","source":"bf <- basic_feature(total.data.df$path,total.data.df$mask_path)","execution_count":22,"outputs":[]},{"metadata":{"_uuid":"58c1132528d356e68b1d4dcba8a8c5a35d00322b","trusted":true,"_cell_guid":"96aeddd1-584e-4c1a-982b-1868cf9df57b"},"cell_type":"code","source":"tail(bf)","execution_count":23,"outputs":[]},{"metadata":{"_uuid":"5125c7eb55f50ba9ba93e63eb66d212574048139","trusted":true,"_cell_guid":"11ff77f0-5148-43a0-b380-41855ce4097a"},"cell_type":"code","source":"head(bf)","execution_count":24,"outputs":[]},{"metadata":{"_uuid":"44b4d6097c7775e039028f95bfe0c9e87904f563","trusted":true,"_cell_guid":"24df5582-5131-4c85-afed-fed33ba829e9"},"cell_type":"code","source":"total.data.df <- cbind(total.data.df,bf)\ntotal.data.df<-total.data.df %>% select(id, width, hight, area,nuclei_cnt,train_test,path,mask_path)\nhead(total.data.df)","execution_count":25,"outputs":[]},{"metadata":{"_uuid":"67f3030f9f4bfa57e5267e0fe6cbfa610304eea0","trusted":true,"_cell_guid":"4e8378df-05d5-434c-9aad-2f965985d334"},"cell_type":"code","source":"dim(total.data.df)","execution_count":26,"outputs":[]},{"metadata":{"scrolled":true,"_uuid":"cf8475d76b6006ae71ef1d8db3473aec50ef5134","trusted":true,"_cell_guid":"01a8e76e-7457-4627-a83c-c0a7926cc4c6"},"cell_type":"code","source":"g.area <- ggplot(total.data.df, aes(area, fill = train_test, colour = train_test)) +\n  geom_density(alpha = 0.5) \ng.hight<- ggplot(total.data.df, aes(hight, fill = train_test, colour = train_test)) +\n  geom_density(alpha = 0.5) \n\ng.width<- ggplot(total.data.df, aes(width, fill = train_test, colour = train_test)) +\n  geom_density(alpha = 0.5) \ng.wh<-qplot(width,hight,data = total.data.df,color = train_test, geom = c(\"point\",\"smooth\"))\nggarrange(g.width,g.hight,g.wh,g.area,ncol = 2, nrow = 2)","execution_count":27,"outputs":[]},{"metadata":{"_uuid":"c4dd4afa45d4dd688604908e778468e1ab0a4a07","trusted":true,"_cell_guid":"dd8da906-1541-4624-a73e-4d519162796c"},"cell_type":"code","source":"train.df <- total.data.df %>% filter(train_test=='train')\ng.nuclei.cnt<- ggplot(train.df, aes(nuclei_cnt, fill = train_test, colour = train_test)) +\n  geom_density(alpha = 0.5) \ng.acnt<- qplot(area,nuclei_cnt,data = total.data.df,color = train_test, geom = c(\"point\",\"smooth\"))\nggarrange(g.nuclei.cnt,g.acnt,ncol = 1, nrow = 1)","execution_count":28,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"R","language":"R","name":"ir"},"language_info":{"mimetype":"text/x-r-source","name":"R","pygments_lexer":"r","version":"3.4.2","file_extension":".r","codemirror_mode":"r"}},"nbformat":4,"nbformat_minor":1}