如何计算图像网址列表中的图像对象?
How to calculate image objects in a list of image urls?
我正在尝试使用 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)
############################
提前致谢
查米尔·拉斯纳亚克
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)
我正在尝试使用 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)
############################
提前致谢
查米尔·拉斯纳亚克
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)