'what is difference between [::2] and [:,:,2]

returns are different.

import cv2 as cv

img = cv.imread('/home/berkay/PycharmProjects/opencv/advanced lane line/lena.jpg')
hls_s = cv.cvtColor(img, cv.COLOR_BGR2HLS)[:,:,2]
print(hls_s)

and

import cv2 as cv

img = cv.imread('/home/berkay/PycharmProjects/opencv/advanced lane line/lena.jpg')
hls_s = cv.cvtColor(img, cv.COLOR_BGR2HLS)[::2]
print(hls_s)

what is difference between [::2] and [:,:,2] and for what purpose are they used?

thanks in advance.



Solution 1:[1]

The argmument in the [] is passed to the __getitem__ method on the object, with : or :: being parsed as a slice object. When you index with :,:,2 you are creating a tuple containing two slices and an int.

For your example obj[::2] calls obj.__getitem__(Slice(None, None, 2)) whereas obj[:,:,2] calls obj.__getitem__((Slice(None, None, None), Slice(None, None, None), 2)]

For numpy arrays, I think the multiple slices are used for slicing different dimensions.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1