Converting Images to Grayscale

It is convenient to invert the colors, since we are going to integrate out values and darker pixels are closer to value zero. Moreover, the each image should be transformed to gray scale.

In Ubuntu:

  1. First, we converted all the tiff files to grayscale using the following command line in Ubuntu's terminal.

for img in $(find . -iname '*.tiff'); do echo -n "Converting $img"; convert -colorspace GRAY $img $img && echo ' [Done]'; done

  1. Invert the colors of the images to replace the white areas with black.

for img in $(find . -iname '*.tiff'); do echo -n "Inverting $img"; convert -negate $img $img && echo ' [Done]'; done

  1. Remove ruler. The original images obtained with the shoe scanner are 1965x4563. The resulting images after croppig the ruler out are 1645x4243.

for img in $(find . -iname '*.tiff'); do echo -n "Cropping $img"; convert -crop 1645x4243+160+160 $img $img && echo ' [Done]'; done

Resize Image

library(solefinder)
library(tidyverse)

image(lena, col = grey.colors(128))
image(t(lena[512:1, ]), col = grey.colors(128))

resize_image(lena, 1/4)

##
summary(c(lena))

expand.grid(x = 1:512, y = 512:1) %>%
  mutate(pixel = c(t(lena))) %>%
  ggplot(aes(x, y)) +
  geom_point(aes(color = pixel)) + 
  scale_color_gradient(low = "black", high = "white")

expand.grid(x = 1:512, y = 512:1) %>%
  mutate(pixel = c(t(lena))) %>%
  ggplot(aes(x, y)) +
  geom_raster(aes(fill = pixel)) + 
  scale_fill_gradient(low = "black", high = "white")

EBImage

if (!require(EBImage)) {
  source("http://bioconductor.org/biocLite.R")
  biocLite("EBImage")
  library("EBImage")
}

library(solefinder)

microbenchmark::microbenchmark(
  EPImage = display(t(lena), method = "raster"),
  Base = image(t(lena)[, 512:1]),
  times = 10)

Adding boots to package

```{R, eval = FALSE}

Load packages to be used

library(tiff) library(EBImage) library(tidyverse)

Setting images directory, reading image names and writing image

adresses.

img_dir <- c("/home/guillermo/Desktop/tmp/") img_dir <- c("/home/guillermo/Desktop/Shoe_data_everspry/")

img_filenames <- list.files(img_dir, pattern = ".tiff") img_adr <- paste0(img_dir, img_filenames)

Read only onge image

boot1 <- readTIFF(img_adr[1]) boot2 <- readTIFF(img_adr[2])

Display image

display(t(boot1), method = "raster") display(t(boot2), method = "raster")

boot1 <- img_adr[1] %>% readTIFF() %>% .[-c(1:160, nrow(.) - 1:160 + 1), -c(1:160, ncol(.) - 1:160 + 1)]

boot1[boot1 < 0.3] <- 0

boot1 %>% display(method = "raster") boot1 %>% flip() %>% display(method = "raster") boot1 %>% flop() %>% display(method = "raster") boot1 %>% rotate(30) %>% display(method = "raster") boot1 %>% rotate(135) %>% display(method = "raster") boot1 %>% EBImage::transpose() %>% display(method = "raster")

save(boot1, file="boot1.rda") save(boot2, file="boot2.rda")

```r

## Crop images

## Check if EBImage package is installed, if not, it will install it.
if (!require(EBImage)) {
  source("http://bioconductor.org/biocLite.R")
  biocLite("EBImage")
  library("EBImage")
}

## Load other packages
library(dplyr)
library(tiff)

## Setting images directory, reading image names and writing image
## adresses.  
img_dir <- c("/home/guillermo/Desktop/shoes_copy/") 

## Get filenames and create image addresses
img_filenames <- list.files(img_dir, pattern = ".tiff")
img_adr <- paste0(img_dir, img_filenames)

## Show the first image files
head(img_filenames)

## Read sample shoe
shoe <- readImage(img_adr[20])
display(shoe, method = "raster")

## ## Change to gray scale
## shoe %>% channel(mode = "gray") %>% display(method = "raster", all = TRUE)

## Crop ruler
shoe <- shoe %>%
    .[-c(1:160, nrow(.) - 1:160 + 1),
     -c(1:160, ncol(.) - 1:160 + 1)] 
display(shoe, method = "raster")


## Remove some noise
threshold <- 0.8
shoe[shoe > threshold] <- threshold
shoe <- shoe %>%
    {(. - min(.))/(max(.) - min(.))}
display(shoe, method = "raster")




crop_and_reduce_noise <- function (img_adress) {
    ## Read sample shoe
    shoe <- readImage(img_adress)

## ## Change to gray scale
    ## shoe %>% channel(mode = "gray") %>% display(method = "raster", all = TRUE)

    ## Crop ruler
    shoe <- shoe %>%
        .[-c(1:160, nrow(.) - 1:160 + 1),
          -c(1:160, ncol(.) - 1:160 + 1)] 

    ## Remove some noise
    threshold <- 0.8
    shoe[shoe > threshold] <- threshold
    shoe <- shoe %>%
        {(. - min(.))/(max(.) - min(.))}

    write
}


CSAFE-ISU/solefinder documentation built on May 23, 2019, 10:31 p.m.