dahtah / imager

R package for image processing
GNU Lesser General Public License v3.0
187 stars 43 forks source link

Can't draw (draw_circle etc) on grayscale image #163

Open adam-cornwell opened 2 years ago

adam-cornwell commented 2 years ago

in drawing.R (https://github.com/dahtah/imager/blob/master/R/drawing.R) for the methods such as draw_circle(), the color to draw in is converted to red, green, and blue values. This then is checked against the length of the spectrum for the image to draw on. If the image is grayscale, that length is 1, but the size of the vector for the color to draw in is always 3, making it impossible to draw on grayscale images, even if the requested color is white, black, or grayscale.

Wouldn't it make more sense to first check the spectrum mode of the image, and then convert the requested draw color to that? So convert the draw color to grayscale, and then draw on the grayscale image.

For now, as a workaround, to do something simple like draw a white circle on a grayscale image I first have to create an RGB image of the same dimensions and black background using imfill, draw on that dummy image, and then combine that with my actual image.

ShotaOchi commented 2 years ago

It's possible to draw on grayscale image.

library(imager)
g <- grayscale(boats)
a <- draw_circle(g, x = 50, y = 50, radius = 10, color = as.matrix(1))
plot(a)

It's inconvenient that we have to use as.matrix function. The problem is ncol function returns NULL when a non-matrix vector is given. NCOL funcion should be used instead of ncol function. I should improve draw_circle function.