R/im_subtract_function.R

Defines functions im_subtract

Documented in im_subtract

#' ReVuePro: im_subtract
#'
#' A function that converts RGB images to greyscale, and subtracts image values from subsequent images. 
#' This function allows one to visualize change across images. 
#' @details This function requires the EBImage package.
#' @param input.folder A path to a folder containing images for subtraction. No default.
#' @param bin An integer that designates how many images will be alloted to each folder, following completion of conversion. Default is 1000.
#' @keywords greyscale, png
#' @import BiocManager EBImage
#' @examples
#' folder = "C:/Users/YukonCornelius/Gold_Panning"
#' im_subtract(input_folder = folder, bin = 500)
#' @export

im_subtract = function(input_folder, bin = 1000){

	input.folder = input_folder
	require(EBImage)
	setwd(input.folder)

	subtract = vector('list', length(list.files(input.folder))-1) 
	all.images = c(list.files(input.folder, full.names = "TRUE"))
	across = length(all.images) - 1
	dir.create("Image Subtraction")
	setwd("./Image Subtraction")

for (i in 1:across){
	image_1 <- readImage(paste(all.images[i]))
	image_2 <- readImage(paste(all.images[i+1]))
	bw.image_1 <- channel(image_1,"gray")
	bw.image_2 <- channel(image_2,"gray")
	subtract.im = abs(round(bw.image_2@.Data - bw.image_1@.Data, 3))
	writeImage(subtract.im, paste("Test_", i, ".png", sep = ""))
}

nfolders = ceiling(length(list.files())/bin)
foldnames = c()
	
	for (i in 1:nfolders){
		foldnames[i] = paste("Part", i, sep = "_")
		}
	for (i in 1:length(foldnames)){
		dir.create(foldnames[i])
		}

	root = list.files(pattern = "\\.png$", full.names = TRUE)
	dest = list.files(pattern = "\\.png$")
	root.split = split(root, ceiling(seq_along(root)/bin))
	dest.split = split(dest, ceiling(seq_along(dest)/bin))

	dest.names = vector('list', length(foldnames))
	for (j in 1:length(foldnames)){
		for (i in 1:bin){
			dest.names[[j]][i] = 
			paste("./", foldnames[j], dest.split[[j]][i], sep ="/")
			}
		}

	for (j in 1:length(foldnames)){
		for (i in 1:bin){
			file.rename(root.split[[j]][i], dest.names[[j]][i])
			}
		}	
	return("File conversion complete.")
}
joshuakrobertson/R-Package_ReVuePro documentation built on June 2, 2020, 8:23 p.m.