Image Manipulation - Shape Condition " image[ CONDITION ] "

Asked

Viewed 892 times

3

I’m working with Python image manipulation.

I came across a line of code like:

image[dst > 0.01 * dst.max()] = [0, 0, 255]

Being "image" and "dst" images.

What is inside the brackets "[]", indexing "image" is a condition for the pixel of this image? The assignment will be effective when the condition is true?

1 answer

1


It is a little difficult to answer your question without context. However, I suppose these images are stored as numerical arrays and arrays.

If my assumption is correct, this is called "logical indexing", similar to what is offered by numpy (if it is not exactly the same thing).

I’ll explain through an example using numpy. We start by creating a vector:

>>> import numpy as np
>>> l = [1,2,3,4]
>>> l
[1, 2, 3, 4]
>>> x = np.array(l)
>>> x
array([1, 2, 3, 4])

The array and the list behave very differently. It is noteworthy the following:

>>> l>2
True
>>> x>2
array([False, False,  True,  True], dtype=bool)

When you make a comparison between a vector and a number, the result is a logical vector with the value of the comparison for each element of the vector. This is true for more complex comparisons as well:

>>> l%2==0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for %: 'list' and 'int'
>>> x%2==0
array([False,  True, False,  True], dtype=bool)

And when you use a logical vector in place of the index of another vector, the result is a vector with all the elements corresponding to positions occupied by "True" in the logical vector:

>>> x[x>2]
array([3, 4])
>>> x[x%2==0]
array([2, 4])

Finally, when you do an assignment, this applies to the elements of the original vector:

>>> x[x%2==0] *= 10
>>> x
array([ 1, 20,  3, 40])

You can assign a list, and the list values are passed in order:

>>> x[x%2==1] = [-1,-3]
>>> x
array([-1, 20, -3, 40])

But the list needs to be the right size. That is, if you have n values that satisfy the condition and you pass a list, the list needs to have exactly n values:

>>> x[x%2==1] = [-1,-3, -5]
>>> x[x%2==0] = range(10, 150, 10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: NumPy boolean array indexing assignment cannot assign 3 input values to the 2 output values where the mask is true
>>> x = np.array(range(15))
>>> x[x%2==0] = [-1,-3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: NumPy boolean array indexing assignment cannot assign 2 input values to the 8 output values where the mask is true

What your snippet of code does is equivalent to this. Putting in words: the positions of the vector image corresponding to vector positions dst where the value is greater than 1% of the maximum of this vector receive the values [0,0,255].

Since this looks like a trio of RGB values, I imagine that what is happening is not exactly the same as assigning a list to an array. More likely than the pixels of dst which satisfy the condition will correspond to blue pixels in image.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.