Constant images

These methods create constant images

class machinevisiontoolbox.ImageConstants.ImageConstantsMixin[source]
classmethod Zeros(w, h=None, colororder=None, dtype='uint8')[source]

Create image with zero value pixels

Parameters
  • w (int, (int, int)) – width, or (width, height)

  • h (int, optional) – height, defaults to None

  • colororder (str) – color plane names, defaults to None

  • dtype (str, optional) – NumPy datatype, defaults to ‘uint8’

Returns

image of zero values

Return type

Image

Create a greyscale image of zero-valued pixels. If only one dimension is given the image is square.

Example:

>>> from machinevisiontoolbox import Image
>>> Image.Zeros(20)
Image: 20 x 20 (uint8)
>>> Image.Zeros(10,20)
Image: 10 x 20 (uint8)
>>> Image.Zeros(20, dtype='float', colororder="RGB") # create color image, all black
Image: 20 x 20 (float64), R:G:B
Seealso

Constant

classmethod Constant(w, h=None, value=0, colororder=None, dtype='uint8')[source]

Create image with all pixels having same value

Parameters
  • w (int, (int, int)) – width, or (width, height)

  • h (int, optional) – height, defaults to None

  • value (scalar, array_like, str) – value for all pixels, defaults to 0

  • colororder (str) – color plane names, defaults to None

  • dtype (str, optional) – NumPy datatype, defaults to ‘uint8’

Returns

image of constant values

Return type

Image

Creates a new image initialized to value. If value is iterable then the image has len(value) planes, each initialized to the corresponding element of value.

Example:

>>> from machinevisiontoolbox import Image
>>> img = Image.Constant(10, value=17)
>>> img
Image: 10 x 10 (uint8)
>>> img.image[0, 0]
17
>>> img = Image.Constant(10, 20, [100, 50, 200], colororder='RGB')
>>> img
Image: 10 x 20 (uint8), R:G:B
>>> img.image[0, 0, :]
array([100,  50, 200], dtype=uint8)
>>> img = Image.Constant(10, value=range(6), colororder='ABCDEF')
>>> img
Image: 10 x 10 (uint8), A:B:C:D:E:F
>>> img.image[0, 0, :]
array([0, 1, 2, 3, 4, 5], dtype=uint8)
>>> img = Image.Constant(10, value='cyan')
>>> img.image[0, 0, :]
array([  0, 255, 255], dtype=uint8)

Note

If len(value) == 3 and colororder is not specified then RGB is assumed.

Seealso

Zeros

classmethod String(s)[source]

Create a small image from text string

Parameters

s (str) – text string

Returns

image

Return type

Image

Creates a new image initialized to a compact representation given by a string. Each pixel is a single character in the range 0 to 9, and image rows are separated by a pipe. There are no spaces. All rows must be the same length.

Example:

>>> from machinevisiontoolbox import Image
>>> img = Image.String('01234|56789|87654')
>>> img.print()
 0 1 2 3 4
 5 6 7 8 9
 8 7 6 5 4

Note

Pixel values are determined by the unicode value of the character relative to unicode for ‘0’, so other ASCII characters (apart from pipe) can be used to obtain pixel values greater than 9. ‘Z’ is 90 and ‘z’ is 122.

Seealso

Constant

classmethod Random(w, h=None, value=0, colororder=None, dtype='uint8')[source]

Create image with random pixel values

Parameters
  • w (int, (int, int)) – width, or (width, height)

  • h (int, optional) – height, defaults to None

  • colororder (str) – color plane names, defaults to None

  • dtype (str, optional) – NumPy datatype, defaults to ‘uint8’

Returns

image of random values

Return type

Image

Creates a new image where pixels are initialized to uniformly distributed random values. For an integer image the values span the range 0 to the maximum positive value of the datatype. For a floating image the values span the range 0.0 to 1.0.

Example:

>>> from machinevisiontoolbox import Image
>>> img = Image.Random(5)
>>> img
Image: 5 x 5 (uint8)
>>> img.image
array([[247, 103,  75, 101,  51],
       [ 75, 219, 118,  26, 177],
       [199, 158, 247,  37, 249],
       [110,  55, 166,  30, 109],
       [189,  48, 119,  41,  68]], dtype=uint8)
>>> img = Image.Random(5, colororder='RGB')
>>> img
Image: 5 x 5 (uint8), R:G:B
>>> img.red().image
array([[227, 154, 174, 109,  84],
       [191, 137,  62, 203, 137],
       [233, 145,  18,  73, 201],
       [ 23,   6, 121, 113,  86],
       [ 40,   6, 202,  14, 203]], dtype=uint8)
>>> img = Image.Random(5, dtype='float32')
>>> img.image
array([[0.6782, 0.0787, 0.0379, 0.7705, 0.9555],
       [0.5675, 0.0015, 0.0271, 0.6676, 0.9444],
       [0.6872, 0.3467, 0.9621, 0.2799, 0.668 ],
       [0.9936, 0.7154, 0.4756, 0.8208, 0.8182],
       [0.7907, 0.624 , 0.0571, 0.3071, 0.6453]])
classmethod Squares(number, size=256, fg=1, bg=0, dtype='uint8')[source]

Create image containing grid of squares

Parameters
  • number (int) – number of squares horizontally and vertically

  • size (int, optional) – image width and height, defaults to 256

  • fg (int, float, optional) – pixel value of the squares, defaults to 1

  • bg (int, optional) – pixel value of the background, defaults to 0

  • dtype (str, optional) – NumPy datatype, defaults to ‘uint8’

Returns

grid of squares

Return type

Image

Example:

>>> from machinevisiontoolbox import Image
>>> img = Image.Squares(2, 14, bg=1, fg=9)
>>> img.A
array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 9, 9, 9, 9, 9, 1, 9, 9, 9, 9, 9, 1],
       [1, 1, 9, 9, 9, 9, 9, 1, 9, 9, 9, 9, 9, 1],
       [1, 1, 9, 9, 9, 9, 9, 1, 9, 9, 9, 9, 9, 1],
       [1, 1, 9, 9, 9, 9, 9, 1, 9, 9, 9, 9, 9, 1],
       [1, 1, 9, 9, 9, 9, 9, 1, 9, 9, 9, 9, 9, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 9, 9, 9, 9, 9, 1, 9, 9, 9, 9, 9, 1],
       [1, 1, 9, 9, 9, 9, 9, 1, 9, 9, 9, 9, 9, 1],
       [1, 1, 9, 9, 9, 9, 9, 1, 9, 9, 9, 9, 9, 1],
       [1, 1, 9, 9, 9, 9, 9, 1, 9, 9, 9, 9, 9, 1],
       [1, 1, 9, 9, 9, 9, 9, 1, 9, 9, 9, 9, 9, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], dtype=uint8)

Note

Image is square.

classmethod Circles(number, size=256, fg=1, bg=0, dtype='uint8')[source]

Create image containing grid of circles

Parameters
  • number (int) – number of circles horizontally and vertically

  • size (int, optional) – image width and height, defaults to 256

  • fg (int, float, optional) – pixel value of the circles, defaults to 1

  • bg (int, optional) – pixel value of the background, defaults to 0

  • dtype (str, optional) – NumPy datatype, defaults to ‘uint8’

Returns

grid of circles

Return type

Image

Example:

>>> from machinevisiontoolbox import Image
>>> img = Image.Circles(2, 14, bg=1, fg=9)
>>> img.A
array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 9, 1, 1, 1, 1, 1, 9, 1, 1, 1],
       [1, 1, 1, 9, 9, 9, 1, 1, 1, 9, 9, 9, 1, 1],
       [1, 1, 9, 9, 9, 9, 9, 1, 9, 9, 9, 9, 9, 1],
       [1, 1, 1, 9, 9, 9, 1, 1, 1, 9, 9, 9, 1, 1],
       [1, 1, 1, 1, 9, 1, 1, 1, 1, 1, 9, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 9, 1, 1, 1, 1, 1, 9, 1, 1, 1],
       [1, 1, 1, 9, 9, 9, 1, 1, 1, 9, 9, 9, 1, 1],
       [1, 1, 9, 9, 9, 9, 9, 1, 9, 9, 9, 9, 9, 1],
       [1, 1, 1, 9, 9, 9, 1, 1, 1, 9, 9, 9, 1, 1],
       [1, 1, 1, 1, 9, 1, 1, 1, 1, 1, 9, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], dtype=uint8)

Note

Image is square.

classmethod Ramp(size=256, cycles=2, dir='x', dtype='float32')[source]

Create image of linear ramps

Parameters
  • dir (str, optional) – ramp direction: ‘x’ [default] or ‘y’

  • size (int, optional) – image width and height, defaults to 256

  • cycles (int, optional) – Number of complete ramps, defaults to 2

  • dtype (str, optional) – NumPy datatype, defaults to ‘float32’

Returns

intensity ramps

Return type

Image

The ramps span the range:

  • float image: 0 to 1

  • int image: 0 to maximum positive value of the integer type

Example:

>>> from machinevisiontoolbox import Image
>>> Image.Ramp(10, 2).image
array([[0.  , 0.25, 0.5 , 0.75, 1.  , 0.  , 0.25, 0.5 , 0.75, 1.  ],
       [0.  , 0.25, 0.5 , 0.75, 1.  , 0.  , 0.25, 0.5 , 0.75, 1.  ],
       [0.  , 0.25, 0.5 , 0.75, 1.  , 0.  , 0.25, 0.5 , 0.75, 1.  ],
       [0.  , 0.25, 0.5 , 0.75, 1.  , 0.  , 0.25, 0.5 , 0.75, 1.  ],
       [0.  , 0.25, 0.5 , 0.75, 1.  , 0.  , 0.25, 0.5 , 0.75, 1.  ],
       [0.  , 0.25, 0.5 , 0.75, 1.  , 0.  , 0.25, 0.5 , 0.75, 1.  ],
       [0.  , 0.25, 0.5 , 0.75, 1.  , 0.  , 0.25, 0.5 , 0.75, 1.  ],
       [0.  , 0.25, 0.5 , 0.75, 1.  , 0.  , 0.25, 0.5 , 0.75, 1.  ],
       [0.  , 0.25, 0.5 , 0.75, 1.  , 0.  , 0.25, 0.5 , 0.75, 1.  ],
       [0.  , 0.25, 0.5 , 0.75, 1.  , 0.  , 0.25, 0.5 , 0.75, 1.  ]],
      dtype=float32)
>>> Image.Ramp(10, 3, dtype='uint8').image
array([[  0, 109, 218,  71,  72, 182,  35,  36, 145, 254],
       [  0, 109, 218,  71,  72, 182,  35,  36, 145, 254],
       [  0, 109, 218,  71,  72, 182,  35,  36, 145, 254],
       [  0, 109, 218,  71,  72, 182,  35,  36, 145, 254],
       [  0, 109, 218,  71,  72, 182,  35,  36, 145, 254],
       [  0, 109, 218,  71,  72, 182,  35,  36, 145, 254],
       [  0, 109, 218,  71,  72, 182,  35,  36, 145, 254],
       [  0, 109, 218,  71,  72, 182,  35,  36, 145, 254],
       [  0, 109, 218,  71,  72, 182,  35,  36, 145, 254],
       [  0, 109, 218,  71,  72, 182,  35,  36, 145, 254]], dtype=uint8)
classmethod Sin(size=256, cycles=2, dir='x', dtype='float32')[source]

Create image of sinusoidal intensity pattern

Parameters
  • dir (str, optional) – sinusoid direction: ‘x’ [default] or ‘y’

  • size (int, optional) – image width and height, defaults to 256

  • cycles (int, optional) – Number of complete cycles, defaults to 2

  • dtype (str, optional) – NumPy datatype, defaults to ‘float32’

Returns

sinusoidal pattern

Return type

Image

The sinusoids are offset to have a minimum value of zero, and span the range:

  • float image: 0 to 1

  • int image: 0 to maximum positive value of the integer type

Example:

>>> from machinevisiontoolbox import Image
>>> Image.Sin(10, 2).image
array([[0.5   , 0.9755, 0.7939, 0.2061, 0.0245, 0.5   , 0.9755, 0.7939,
        0.2061, 0.0245],
       [0.5   , 0.9755, 0.7939, 0.2061, 0.0245, 0.5   , 0.9755, 0.7939,
        0.2061, 0.0245],
       [0.5   , 0.9755, 0.7939, 0.2061, 0.0245, 0.5   , 0.9755, 0.7939,
        0.2061, 0.0245],
       [0.5   , 0.9755, 0.7939, 0.2061, 0.0245, 0.5   , 0.9755, 0.7939,
        0.2061, 0.0245],
       [0.5   , 0.9755, 0.7939, 0.2061, 0.0245, 0.5   , 0.9755, 0.7939,
        0.2061, 0.0245],
       [0.5   , 0.9755, 0.7939, 0.2061, 0.0245, 0.5   , 0.9755, 0.7939,
        0.2061, 0.0245],
       [0.5   , 0.9755, 0.7939, 0.2061, 0.0245, 0.5   , 0.9755, 0.7939,
        0.2061, 0.0245],
       [0.5   , 0.9755, 0.7939, 0.2061, 0.0245, 0.5   , 0.9755, 0.7939,
        0.2061, 0.0245],
       [0.5   , 0.9755, 0.7939, 0.2061, 0.0245, 0.5   , 0.9755, 0.7939,
        0.2061, 0.0245],
       [0.5   , 0.9755, 0.7939, 0.2061, 0.0245, 0.5   , 0.9755, 0.7939,
        0.2061, 0.0245]], dtype=float32)
>>> Image.Sin(10, 2, dtype='uint8').image
array([[127, 248, 202,  52,   6, 127, 248, 202,  52,   6],
       [127, 248, 202,  52,   6, 127, 248, 202,  52,   6],
       [127, 248, 202,  52,   6, 127, 248, 202,  52,   6],
       [127, 248, 202,  52,   6, 127, 248, 202,  52,   6],
       [127, 248, 202,  52,   6, 127, 248, 202,  52,   6],
       [127, 248, 202,  52,   6, 127, 248, 202,  52,   6],
       [127, 248, 202,  52,   6, 127, 248, 202,  52,   6],
       [127, 248, 202,  52,   6, 127, 248, 202,  52,   6],
       [127, 248, 202,  52,   6, 127, 248, 202,  52,   6],
       [127, 248, 202,  52,   6, 127, 248, 202,  52,   6]], dtype=uint8)