从R中的图像获取数据作为日期和小时

时间:2015-11-16 22:01:12

标签: r image date analysis

我正在对R中的图像进行一些数据分析,以了解森林中的结构复杂性主题。我们设置一些相机在一段时间内每小时拍照。这些图像是JPEG格式 问题是我需要一个功能,除了我正在处理的其他参数之外,还要帮助我获取时间和日期并将其保存在每张照片的csv文件中。

images.folder = "C:/Users/abc/Desktop/OSA/BSCAM11" 
results.file = "BSCAM11.csv" 

if (file.exists(results.file)) {
    existing = read.csv(results.file)
    start = max(existing$i)+1
} else {
    start = 1
}

files = dir(images.folder)
nbins = 15 # see MIG algorithm
nb.photos = length(files)



    for (i in start:nb.photos) {
            file.to.analyze = files[[i]]
            path = paste(images.folder,file.to.analyze,sep="/")

            # Load 3 rasters from the target image, one for red, one for green and one for the blue channel
            R = raster(path, band=1)
            G = raster(path, band=2)
            B = raster(path, band=3)

            # Combine the RGB channels to create a grayscale image
            RGB = brick(R,G,B) 
            r.grey = mean(RGB)
            #     
            #     # Convert RGB bands to HSV channels
            #     HSV = rgb2hsv(getValues(R),getValues(G),getValues(B))   
            #     r.H = r.S = r.V = raster(ncols = ncol(R), nrows = nrow(R))
            #     extent(r.H) = extent(r.S) =extent(r.V) =extent(R)
            #     values(r.H) = HSV[1,]
            #     values(r.S) = HSV[2,]
            #     values(r.V) = HSV[3,]  
            #     
            #     # On the four channels, find either right (1), diagonal (2) or below (3) neighbors for histogram calculations
            #     
            v.grey.1 = getImagePixels(r.grey, side = 1)
            #     v.H.1 = getImagePixels(r.H, side = 1)
            #     v.S.1 = getImagePixels(r.S, side = 1)
            #     v.V.1 = getImagePixels(r.V, side = 1)
            #     
            v.grey.2 = getImagePixels(r.grey, side = 2)
            #     v.H.2 = getImagePixels(r.H, side = 2)
            #     v.S.2 = getImagePixels(r.S, side = 2)
            #     v.V.2 = getImagePixels(r.V, side = 2)
            #     
            v.grey.3 = getImagePixels(r.grey, side = 3)
            #     v.H.3 = getImagePixels(r.H, side = 3)
            #     v.S.3 = getImagePixels(r.S, side = 3)
            #     v.V.3 = getImagePixels(r.V, side = 3)
            #     
            #     # Calculate histograms from neighbor vectors
            prob.grey.1 = calculateHisto(reference_vector = v.grey.1$reference_vector,
                                         neighbour_vector = v.grey.1$neighbour_vector, nbins = nbins)
            #     prob.H.1 = calculateHisto(reference_vector = v.H.1$reference_vector,
            #                               neighbour_vector = v.H.1$neighbour_vector, nbins = nbins)
            #     prob.S.1 = calculateHisto(reference_vector = v.S.1$reference_vector,
            #                               neighbour_vector = v.S.1$neighbour_vector, nbins = nbins)
            #     prob.V.1 = calculateHisto(reference_vector = v.V.1$reference_vector,
            #                               neighbour_vector = v.V.1$neighbour_vector, nbins = nbins)
            #     
            prob.grey.2 = calculateHisto(reference_vector = v.grey.2$reference_vector,
                                         neighbour_vector = v.grey.2$neighbour_vector, nbins = nbins)
            #     prob.H.2 = calculateHisto(reference_vector = v.H.2$reference_vector,
            #                               neighbour_vector = v.H.2$neighbour_vector, nbins = nbins)
            #     prob.S.2 = calculateHisto(reference_vector = v.S.2$reference_vector,
            #                               neighbour_vector = v.S.2$neighbour_vector, nbins = nbins)
            #     prob.V.2 = calculateHisto(reference_vector = v.V.2$reference_vector,
            #                               neighbour_vector = v.V.2$neighbour_vector, nbins = nbins)
            #     
            prob.grey.3 = calculateHisto(reference_vector = v.grey.3$reference_vector,
                                         neighbour_vector = v.grey.3$neighbour_vector, nbins = nbins)
            #     prob.H.3 = calculateHisto(reference_vector = v.H.3$reference_vector,
            #                               neighbour_vector = v.H.3$neighbour_vector, nbins = nbins)
            #     prob.S.3 = calculateHisto(reference_vector = v.S.3$reference_vector,
            #                               neighbour_vector = v.S.3$neighbour_vector, nbins = nbins)
            #     prob.V.3 = calculateHisto(reference_vector = v.V.3$reference_vector,
            #                               neighbour_vector = v.V.3$neighbour_vector, nbins = nbins)

            # Calculate the green index
            #    r.green = getBinaryVegetationMask(RGB)

            # Write to the CSV file after every image is analysed
            write.table(
                    data.frame(
                            i = i,
                            ID = file.to.analyze,

                            MIG.grey = meanInformationGain(prob.grey.2),
                            Aniso.grey = meanInformationGain(prob.grey.1) / meanInformationGain(prob.grey.3),
                            Timestamp = Sys.time()
                    ),
                    file=results.file,
                    append=i!=1,
                    col.names = i==1,
                    row.names = FALSE,
                    sep=","
            )   
    }

}

3 个答案:

答案 0 :(得分:0)

我希望这会给你一个起点

在脚本

中创建“path”变量后添加此行
file_ctime <- file.info(path)$ctime 

请注意,它会根据您的需要提供结果,例如解析

"2015-10-30 20:52:00 GMT"

答案 1 :(得分:0)

使用专业工具来完成这项工作可能更容易,例如

library(abbyyR)
# Visit http://ocrsdk.com/plans-and-pricing/ 
# open account, set up an app and submit its credentials:  
setapp(c("appname", "app_password"))
processRemoteImage(img_url="http://i.stack.imgur.com/n7xQz.jpg")
Sys.sleep(5) # wait a bit
tasklist <- listTasks()
(res <- readLines(as.character(tasklist[1, 7]), encoding = "UTF-8"))
# [1] "2015-01-01 16:30:32 " # <-- seems to work

enter image description here

答案 2 :(得分:0)

您可能想要提取存储在exif元数据中的图片的实际日期和时间,而不是文件修改日期。目前在R中没有直接的方法。

但是,根据您的系统,可以使用命令行工具 exiftool 。要在directory中提取所有图像的日期/时间,您可以编写如下内容:

# extract exif metadata for images in the specified directory
dir = "/path/to/directory/"
info = system(
  sprintf("exiftool -T -FileName -DateTimeOriginal %s", dir),
  intern = TRUE
)

# read into a data.frame
df <- read.delim(
  textConnection(info),
  stringsAsFactors = FALSE,
  header = FALSE,
  col.names = c("Filename", "DateTime")
)

# convert to a formal Date-Time Class
df$DateTime = as.POSIXct(df$DateTime, format = "%Y:%m:%d %H:%M:%S")