Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中随机剪切/平铺图像_R_Image_Ggplot2_Plot_Tiling - Fatal编程技术网

在R中随机剪切/平铺图像

在R中随机剪切/平铺图像,r,image,ggplot2,plot,tiling,R,Image,Ggplot2,Plot,Tiling,有没有人有一个聪明的想法,如何从一个图像中创建n个矩形瓷砖,每个都有不同的大小,没有重叠。下面给出的解决方案仅限于n=4 p>我有点无聊,所以我试了一下。工作得很好,但我不是随机生成器方面的专家,所以很可能这段代码生成的矩形位置存在一些隐藏的偏差 更新:这真的吸引了我的注意力。我认为第一个版本实际上偏向于制作越来越小的矩形。我更新了代码,这样就不会再发生这种情况了 库(data.tree) 图书馆(tidyverse) 随机矩形是矩形瓷砖是一项要求?实际上是的…在这个平台上遇到无聊的人总是很好;

有没有人有一个聪明的想法,如何从一个图像中创建n个矩形瓷砖,每个都有不同的大小,没有重叠。下面给出的解决方案仅限于n=4


p>我有点无聊,所以我试了一下。工作得很好,但我不是随机生成器方面的专家,所以很可能这段代码生成的矩形位置存在一些隐藏的偏差

更新:这真的吸引了我的注意力。我认为第一个版本实际上偏向于制作越来越小的矩形。我更新了代码,这样就不会再发生这种情况了

库(data.tree)
图书馆(tidyverse)

随机矩形是矩形瓷砖是一项要求?实际上是的…在这个平台上遇到无聊的人总是很好;-)。。。谢谢,非常感谢
randomTiles <- function(w, h, n){

  if(sample(c(TRUE, FALSE), 1)){
    tl <- c(0, sample(10:w-10, 1), 0, sample(round(h/10):h-round(h/10), 1))
    bl <- c(0, sample(tl[2]:w-round(w/10), 1), tl[4], h)
    tr <- c(tl[2], w, 0, tl[4])
    br <- c(bl[2], w, tl[4], h)
  }else{
    tl <- c(0, sample(10:w-10, 1), 0, sample(round(h/10):h-round(h/10), 1))
    tr <- c(tl[2], w, 0, sample(tl[4]:h-round(h/10), 1))
    bl <- c(0, tl[2], tl[4], h)
    br <- c(tl[2], w, tr[4], h)
  }
  tileFrame <- data.frame(xleft = c(tl[1], bl[1], tr[1], br[1]),
                          ybottom = c(tl[3], bl[3], tr[3], br[3]),
                          xright = c(tl[2], bl[2], tr[2], br[2]),
                          ytop = c(tl[4], bl[4], tr[4], br[4]),
                          col = rgb(runif(4), runif(4), runif(4)))
  return(tileFrame)

}

h <- 100
w <- 120
n <- 4

op <- par(mfrow = c(2,2))
for(i in 1:4){
  plot(h, xlim = c(0, w), ylim = c(h, 0), type = "n", xlab = "WIDTH", ylab = "HIGHT")
  tiles <- randomTiles(w = w, h = h, n = n)
  rect(tiles[,1], tiles[,2], tiles[,3], tiles[,4], col = tiles[,5])
}
par(op)