英文:
For loop for image editing: use output image from the loop as input for the next iteration
问题
我试图创建一系列黑白图像的视觉噪声图像,其中每个图像与前一个图像相同,只是随机像素的比例发生了颜色交换(黑色像素变白色,白色像素变黑色)。
我可以单独生成这些图像,但现在我想将代码放入循环中,以便可以生成并保存一系列300张图像。然而,我遇到了一些问题。我的循环存在多个问题,因为我试图从堆砌在一起的其他问题和stackoverflow上的答案的组合中拼凑出来,我并不完全理解自己在做什么(我是新手)。
这是我的代码:
file_names <- c("VN1", "VN2", "VN3", "VN4", "VN5")
next_image <- c()
for (i in 1:5) {
im <- if (i == 1) {
load.image("C:/Users/Ali/Desktop/VN_images/VN.png")
} else {
load.image(next_image)
}
df <- as.data.frame(im) %>%
mutate(colour = case_when(value == 0 ~ "black", TRUE ~ "white")) %>%
group_by(colour) %>%
mutate(value = replace(value, sample(row_number(), size = ceiling(0.05 * n()), replace = FALSE), 3))
df$value[df$value == 3 & df$colour == "black"] <- 1
df$value[df$value == 3 & df$colour == "white"] <- 0
df1 <- df[, -4]
new_image <- as.cimg(df1, dims = c(150,100))
save.image(new_image, "C:/Users/Ali/Desktop/VN_images/", file_names[i], ".png")
next_image <- c(next_image, new_image)
}
我的主要问题目前似乎是保存代码生成的新图像 - 我需要每个新文件都有不同的名称,并且名称保留它们生成的顺序 - 然后在循环的下一个迭代中加载新图像。
使用上面的代码,我收到错误消息"Error in save.image(new_image, "C:/Users/Ali/Desktop/VN_images/", :
unused argument (".png")"
如果我将文件路径更改为"C:/Users/Ali/Desktop/VN_images/A.png",代码将运行一次并保存一个名为"A.png"的图像,但也会返回错误"Error in if (is.url) { : the condition has length > 1",因此在第二次迭代中似乎也存在加载新图像的问题。
我查看了几个类似问题的先前答案(例如,这里,这里,这里,这里,和这里),但我很难将答案应用到自己的问题中。
英文:
I'm trying to create a series of black and white images of visual noise, in which each image is identical to the previous one except for a proportion of random pixels which have swapped colours (black pixels turn white, and white pixels turn black).
I can generate the images individually, but now I want to put the code in a loop so I can generate and save a sequence of 300 images. However, I am having some trouble. There are multiple problems with my loop as I've tried to cobble it together from a combination of other questions and answers on stack overflow, and I don't fully understand what I'm doing (I'm new to programming).
Here's my code:
file_names <- c("VN1", "VN2", "VN3", "VN4", "VN5")
next_image <- c()
for (i in 1:5) {
im <- if (i == 1) {
load.image("C:/Users/Ali/Desktop/VN_images/VN.png")
} else {
load.image(next_image)
}
df <- as.data.frame(im) %>%
mutate(colour = case_when(value == 0 ~ "black", TRUE ~ "white")) %>%
group_by(colour) %>%
mutate(value = replace(value, sample(row_number(), size = ceiling(0.05 * n()), replace = FALSE), 3))
df$value[df$value == 3 & df$colour == "black"] <- 1
df$value[df$value == 3 & df$colour == "white"] <- 0
df1 <- df[, -4]
new_image <- as.cimg(df1, dims = c(150,100))
save.image(new_image, "C:/Users/Ali/Desktop/VN_images/", file_names[i], ".png")
next_image <- c(next_image, new_image)
}
EDIT: question edited to fix error highlighted by @nrennie below
My main problem at the moment appears to be saving the new image that is generated by the code - I need each new file to have a different name and for the names to preserve the sequence in which they were generated - and then loading the new image in the next iteration of the loop.
With the code as above I get the error message "Error in save.image(new_image, "C:/Users/Ali/Desktop/VN_images/", :
unused argument (".png")"
If I change the file path to "C:/Users/Ali/Desktop/VN_images/A.png" the code will run once and save an image named "A.png", but also return the error "Error in if (is.url) { : the condition has length > 1" so there also appears to be a problem loading the new image in the second iteration.
I've looked at several previous answers to similar questions (e.g., here, here, here,here, and here, but I'm struggling to apply the answers to my own problem.
答案1
得分: 0
我通过反复尝试和离线帮助成功使这个工作起来。以下是代码,以防对将来的其他人有帮助:
library(imager)
library(dplyr)
file_names <- c("VN1", "VN2", "VN3", "VN4", "VN5")
next_image <- NULL
for (i in 1:5) {
if (i == 1) {
im <- load.image("C:/Users/Ali/Desktop/VN_images/VN.png")
} else {
im <- next_image
}
df <- as.data.frame(im) %>%
mutate(colour = case_when(value == 0 ~ "black", TRUE ~ "white")) %>%
group_by(colour) %>%
mutate(value = replace(value, sample(row_number(), size = ceiling(0.05 * n()), replace = FALSE), 3))
df$value[df$value == 3 & df$colour == "black"] <- 1
df$value[df$value == 3 & df$colour == "white"] <- 0
df1 <- df[, -4]
new_image <- as.cimg(df1, dims = c(150, 100))
save.image(new_image, paste0("C:/Users/Ali/Desktop/VN_images/", file_names[i], ".png"))
next_image <- new_image
}
希望这对其他人有所帮助。
英文:
I managed to get this to work through trial and error and a bit of help offline. Posting the code below in case it helps anyone else in future
library(imager)
library(dplyr)
file_names <- c("VN1", "VN2", "VN3", "VN4", "VN5")
next_image <- NULL
for (i in 1:5) {
if (i == 1) {
im <- load.image("C:/Users/Ali/Desktop/VN_images/VN.png")
} else {
im <- next_image
}
df <- as.data.frame(im) %>%
mutate(colour = case_when(value == 0 ~ "black", TRUE ~ "white")) %>%
group_by(colour) %>%
mutate(value = replace(value, sample(row_number(), size = ceiling(0.05 * n()), replace = FALSE), 3))
df$value[df$value == 3 & df$colour == "black"] <- 1
df$value[df$value == 3 & df$colour == "white"] <- 0
df1 <- df[, -4]
new_image <- as.cimg(df1, dims = c(150, 100))
save.image(new_image, paste0("C:/Users/Ali/Desktop/VN_images/", file_names[i], ".png"))
next_image <- new_image
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论