如何计算图像网址列表中的图像对象?

时间:2018-04-05 22:46:47

标签: r image image-processing webimage

我试图使用EBImage包计算图像中的对象。以下代码有效。但是,因为我有一个相对较大的数据集(大约1000个图像)重复这段代码1000次可能真的很累。我需要使代码更有效(通过最小化工作量),我想知道你是否可以帮我简化流程。我的具体问题是,我是否可以仅使用命令处理所有图像(对于所有图像)并获得显示" max"的结果的数字列表。对于每个图像?

 library(EBImage)

#Reading Images 
a <- readImage("https://scontent.cdninstagram.com/vp/8b4999eb71b5381b5b68d4bef635630e/5B735DE7/t51.2885-15/sh0.08/e35/p640x640/29415832_1622274641183922_9158751921719214080_n.jpg")
b <- readImage("https://scontent.cdninstagram.com/vp/2aa22b810fabd2478065503c9a921daf/5B64459B/t51.2885-15/e35/29739757_359646237856246_7485780478239178752_n.jpg")
c <- readImage("https://scontent.cdninstagram.com/vp/796f4ee2e02b012969d778f5fda4510e/5B569B51/t51.2885-15/s640x640/sh0.08/e35/29416798_1877911555555364_9109288458408427520_n.jpg")
d <- readImage("https://scontent.cdninstagram.com/vp/ae53b00be6dd5babfc1c02322d3be640/5B4EC8A5/t51.2885-15/s640x640/sh0.08/e35/29739069_266004773938331_5594342353562238976_n.jpg")

a = channel(a, "gray")
at = a > 0.55
labelsat = bwlabel(at)
max(labelsat) 

b = channel(b, "gray")
bt = b > 0.55
labelsbt = bwlabel(bt)
max(labelsbt)

c = channel(c, "gray")
ct = c > 0.55
labelsct = bwlabel(ct)
max(labelsct)

d = channel(d, "gray")
dt = d > 0.55
labelsdt = bwlabel(dt)
max(labelsdt)
############################

提前致谢

Chamil Rathnayake

1 个答案:

答案 0 :(得分:0)

urls <- c(
  "https://scontent.cdninstagram.com/vp/8b4999eb71b5381b5b68d4bef635630e/5B735DE7/t51.2885-15/sh0.08/e35/p640x640/29415832_1622274641183922_9158751921719214080_n.jpg",
  "https://scontent.cdninstagram.com/vp/2aa22b810fabd2478065503c9a921daf/5B64459B/t51.2885-15/e35/29739757_359646237856246_7485780478239178752_n.jpg",
  "https://scontent.cdninstagram.com/vp/796f4ee2e02b012969d778f5fda4510e/5B569B51/t51.2885-15/s640x640/sh0.08/e35/29416798_1877911555555364_9109288458408427520_n.jpg",
  "https://scontent.cdninstagram.com/vp/ae53b00be6dd5babfc1c02322d3be640/5B4EC8A5/t51.2885-15/s640x640/sh0.08/e35/29739069_266004773938331_5594342353562238976_n.jpg"
)

# load the images once and save them
images <- lapply(urls, readImage)
# if there are a lot, consider save(images, file="images.rda") or something similar

myfunc <- function(img) {
    a = channel(img, "gray")
    at = a > 0.55
    labelsat = bwlabel(at)
    return(max(labelsat))
}
sapply(images, myfunc)