Varun Mehrotra
Varun Mehrotra

Reputation: 3

How does the item() function works when accessing pixel values?

I was reading the OpenCV python tutorials, and they said that the NumPy array function item() is the best way to access pixel values in an image, but I don't understand what it does.

import cv2
import numpy as np

img = cv2.imread('image.jpg')

print(img.item(10, 10, 2)) # I don't know what the item() function does/what it's parameters are

Upvotes: 0

Views: 883

Answers (1)

Tomas Farias
Tomas Farias

Reputation: 1343

From the docs: numpy.ndarray.item() copies an element of an array to a standard Python scalar and returns it.

To put it in other words, calling img.item(i) gets you a copy of the value represented by the index i in your array, similar to img[i] but with the difference that it returns it as a Python scalar instead of an array. Following the docs, getting a Python scalar is useful to speed up access to the elements of the array and doing arithmetic on the values taking advantage of Python's optimized math.

An example:

>>> x = np.random.randint(9, size=(3, 3))
>>> x
array([[1, 8, 4],
      [8, 7, 5],
      [2, 1, 1]])
>>> x.item((1,0))
8
>>> x[1,0] # both calls seem to return the same value, but...
8
>>> type(x[1,0]) # Numpy's own int32
<class 'numpy.int32'>
>>> type(x.item((1,0))) # Python's standard int
<class 'int'>

item takes only one parameter which can be None, which only works with single item arrays, an int_type, which works like a flat index, and a tuple of int_type, which is interpreted as a multi dimension index of the array.

Going back to your concrete question, OpenCV recommends item and itemset when working with individual pixel values, as numpy is optimized for array calculations so accessing a single item is discouraged.

So instead of doing:

import cv2
import numpy as np

img = cv2.imread('image.jpg')
img[0, 0] = 255 # Setting a single pixel
px = img[0, 0]  # Getting a single pixel

Do:

img.itemset((0, 0), 255)
px = img.item((0, 0))

Upvotes: 1

Related Questions