machinevisiontoolbox.Image.choose

Image.choose(image2, mask)

Pixel-wise image merge

Parameters:
  • image2 (Image, array_like(3), str) – second image

  • mask (ndarray(H,W)) – image mask

Raises:
  • ValueError – image and mask must be same size

  • ValueError – image and image2 must be same size

Returns:

merged images

Return type:

Image

Return an image where each pixel is selected from the corresponding pixel in self or image2 according to the corresponding pixel values in mask. If the element of mask is zero/false the pixel value from self is selected, otherwise the pixel value from image2 is selected:

\[\begin{split}\mathbf{Y}_{u,v} = \left\{ \begin{array}{ll} \mathbf{X}_{1:u,v} & \mbox{if } \mathbf{M}_{u,v} = 0 \\ \mathbf{X}_{2:u,v} & \mbox{if } \mathbf{M}_{u,v} > 0 \end{array} \right.\end{split}\]

If image2 is a scalar or 1D array it is taken as the pixel value, and must have the same number of elements as the channel depth of self. If image2 is a string it is taken as a colorname which is looked up using name2color.

Example:

>>> from machinevisiontoolbox import Image
>>> img1 = Image.Constant(3, value=10)
>>> img2 = Image.Constant(3, value=80)
>>> img = Image([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> img1.choose(img2, img >=5).image
array([[10, 10, 10],
       [10, 80, 80],
       [80, 80, 80]], dtype=uint8)
>>> img1 = Image.Constant(3, value=[0,0,0])
>>> img1.choose('red', img>=5).red().image
array([[  0,   0,   0],
       [  0, 255, 255],
       [255, 255, 255]], dtype=uint8)
Note:
  • If image and image2 are both greyscale then the result is greyscale.

  • If either of image and image2 are color then the result is color.

  • If one image is double and the other is integer, then the integer image is first converted to a double image.

  • image2 can contain a color descriptor which is one of: a scalar value corresponding to a greyscale, a 3-vector corresponding to a color value, or a string containing the name of a color which is found using name2color.

Seealso:

name2color opencv.bitwise_and