-
Notifications
You must be signed in to change notification settings - Fork 12
62 Image
This package is available with require("aprilann.Image")
.
Two globals are declared in this package: Image
and ImageRGB
. Both classes
represent its internal data using floats, in the case of Image
with one float,
in the case of ImageRGB
with 3 floats (Red, Green, Blue). So, usually 8 bits
per color images will be transformed to be the color 0=0.0
and the color
255=1.0
.
Images cannot be serialized as a Lua object into a .lua
filename using
util.serialize()
and util.deserialize()
functions. Instead of that, it is
possible to write images into png
, tiff
or jpeg
files by using the package
ImageIO
. This package allows to write Image
or ImageRGB
images.
A tool module has been deployed to facilitate visualization of images and
matrices. It depends on lgi
binding library for Lua 5.2, and uses
the GTK binding. In order to do the require "tools.lgi.gtk"
you need
to have the APRIL-ANN ROOT directory in your package.path
Lua variable,
or in your LUA_PATH
environment variable. By default, the current directory
is included, so if you execute the require
being your working directory
the APRIL-ANN GIT root directory, it will work. In any case, NEVER add
the april_tools directory to the LUA_PATH, because the lgi module will collide
with the tools.lgi module.
> gtk = require "tools.lgi.gtk"
> gtk.show("sheared.png")
If it doesn't work, try add the APRIL-ANN ROOT directory to the
package.path
variable.
> package.path = "YOUR_APRIL_ANN_PATH/?.lua;" .. package.path
> gtk = require "tools.lgi.gtk"
> gtk.show("sheared.png")
This function allows to visualize images using GTK library. It receives a variable number of arguments, and every argument will be displayed in a different window. Every argument could be:
-
A filename, opening the contained image.
-
A
matrix
, which needs to be bi-dimensional. It will be converted to anImage
and then showed. -
An
Image
orImageRGB
.
Note that the Lua command shell will be blocked after executing GTK main loop. Any of the created windows has an exit button to allow closing the GTK main loop.
The Image
class allows to work with gray-scale images. Every image contains
an underlying matrix
instance (floats).
The constructor receives a matrix
where the image is contained. This matrix
must be simple, that is, contiguous in memory, and in row_major
. Given a
matrix with data, it is possible to load an Image
with:
> m = matrix(100,100)
> -- make a gradient
> for sw in m:sliding_window():iterate() do sw:linear():adjust_range(0,1) end
> -- constructor for Image
> img = Image(m)
> = img
# Image 100x100+0+0 [0x2490810 data= 0x25843e0]
> -- you can see the image writing it with ImageIO or using the gtk module
> ImageIO.write(img, "gradient.png")
This method allows to select a portion of an Image
, and returns a new instance
which references the given portion. The crop is given by using a string
(like in convert command), with the following format:
<width>x<height>{+-}<x>{+-}<y>
.
> img2 = img:crop("20x20+10+10")
> = img2
# Image 20x20+10+10 [0x2490810 data= 0x25843e0]
Similar to previous one, but given the crop using four arguments.
> img2 = img:crop(20, 20, 10, 10)
> = img2
# Image 20x20+10+10 [0x2490810 data= 0x25843e0]
This method returns the underlying matrix
. Be careful with this method,
the memory is shared between the Image
object and the returned matrix
object.
> m2 = img:matrix()
> = m2
...
# Matrix of size [100,100] in row_major [0x2490810 data= 0x25843e0]
> = m -- the matrix references the original
...
# Matrix of size [100,100] in row_major [0x2490810 data= 0x25843e0]
This method returns the geometry information of the image.
> = img2:geometry()
20 20 10 10
This method returns the given position (x,y) pixel value.
This method assigns the given value at the given (x,y) pixel position.
This method returns a deep-copy of the caller object.
This method returns the horizontal projection of the caller object.
This method returns the vertical projection of the caller object.
This method returns an Image
which is a
shear transformation with the
given angle
. Optionally, a second argument could be given indicating with
the string rad
, deg
or grad
the angle unit, by default it is rad
. The
thrird argument is also optional, and it indicates which pixel value is
taken as white color, by default is 0.0
.
> img_sh = img:shear_h(0.1)
> ImageIO.write(img_sh, "sheared.png")
This method applies the same transformation as the img:shear_h(...)
, but
instead of returning a new Image
, the transformation is performed
in-place.
This method returns the bounding box of the caller object, using threshold
for deciding when a value is considered as background or not.
This method copies the given Image
in the given (dest_x,dest_y) position
of the caller object.
This method applies image substraction.
This method transforms in-place the caller object with the given range for black/white thresholding.
This method returns a new Image
which is a rotation of 90º in clock-wise
direction (if param=1
) or in counter-clock-wise (if param=-1
) of the
caller object.
This method returns an image with the colors inverted.
This class is an instantiation of the same C++ template used for Image
class.
Both classes has similar methods.
ImageRGB
class could be loaded from a matrix
with 3 dimensions. The last
dimension of the matrix
has size 3 for the componentes Red, Green, Blue.
Be careful with this constructor, the memory is shared between the
ImageRGB
object and the given matrix
object.
> img_rgb = ImageRGB(matrix(100,100,3):linear())
> = img_rgb
# ImageRGB 100x100+0+0 [0x2535700 data= 0x251fc40]
The following methods are shared between both classes, and their has the same interface:
-
ImageRGB = img_rgb:crop(string)
-
ImageRGB = img_rgb:crop(width, height, offsetx, offsety)
-
width,height,offsetx,offsety = img_rgb:geometry()
-
r,g,b = img_rgb:getpixel(x,y)
-
img_rgb:putpixel(x,y, r,g,b)
-
ImageRGB = img_rgb:clone()
-
ImageRGB = img_rgb:shear_h(angle, [, units="rad" [, WHITE=0.0 ] ] )
-
img_rgb:shear_h_inplace(angle, [, units="rad" [, WHITE=0.0 ] ] )
-
img_rgb:copy(ImageRGB, dest_x, dest_y)
-
ImageRGB = img_rgb:rotate90cw(param)
-
ImageRGB = img_rgb:invert_colors()
-
ImageRGB = img_rgb:convolution5x5(table [, r,g,b ])
-
ImageRGB = img_rgb:resize(x,y)
-
ImageRGB,x,y = img_rgb:affine_transform(AffineTransform2D, r,g,b)
This method returns the underlying matrix
.
Be careful with this method, the memory is shared between the
ImageRGB
object and the returned matrix
object.
This method returns an instance of Image
, transforming the 3 color components
into a gray component.
A usual situation is to load a matrix
from a PNG image (in RGB color, with
0=BLACK, and 1=WHITE), but transform it to train ANNs in gray-scale with 1=BLACK
and 0=WHITE. This could be done by the following code:
> img_matrix = ImageIO.read(filename):to_grayscale():invert_colors():matrix()