Reputation: 425
I want to train a neural network to count circles on an image by generating thousands of examples. So I need a function to generate a picture with a random number of circles in it.
I have been playing with plots but I guess there must be a better way directly manipulating pixel matrices. Any ideas?
Upvotes: 1
Views: 96
Reputation: 1545
You can use the Bioconductor package EBImage to draw circles on pixel matrices and save them as PNG files, as demonstrated below.
library("EBImage")
set.seed(1)
images = 10 # number of images to generate
size = 500 # X-Y dimensions of images
circles = 50 # max number of circles
rmin = 5
rmax = 25 # max circle radius
xy = size - 2*rmax
rr = rmax - rmin
n = sample.int(100, images)
for (i in 1:length(n)) {
img = Image(1L, c(size, size))
x = sample.int(xy, n[i]) + rmax
y = sample.int(xy, n[i]) + rmax
r = sample.int(rr, n[i], replace = TRUE)
for (k in 1:n[i]) {
img = drawCircle(img, x[k], y[k], r[k], col=0)
}
writeImage(img, sprintf("img%.5d.png", i))
}
In case you need filled circles, you can provide the argument fill = TRUE
to the function drawCircle
. You can also easily change the image background in the call to Image
, or vary the circle color with the argument col
to drawCircle
. For example, use
...
img = Image(0L, c(size, size))
...
img = drawCircle(img, x[k], y[k], r[k], fill = TRUE, col = runif(1, .1))
...
for grey circles on black background.
Upvotes: 1
Reputation: 263382
Might as well throw it out as an answer:
set.seed(123)
n= sample(500, 1) # picks 1 number in range of 1 to 500
plot( runif(n), runif(n), cex=runif(n,1,6), # Varies size and location
main=bquote( .(n)~ Random~Circles) )
Upvotes: 1