Python slice operators ([],[:],[:],[:])
[]: An element in the access sequence, such as str_list[3], means accessing the fourth element in the str_list sequence.
[:]: Accessing an element in a sequence, such as str_list[1:4], means accessing the second to fourth elements in the str_list sequence.
If no index value is provided, the default starts at 0. str_list[:4] denotes access to the first to fourth elements of the sequence, and str_list[4:] denotes access to the fifth to last elements.
[:: -1]: Elements of a flip sequence, such as str_list [:-1] denote elements in a flip str_list sequence.
[:: 2]: Take one every other element.
#!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import print_function from __future__ import division import os import sys import cv2 import numpy as np import matplotlib.pyplot as plt sys.path.append(os.path.dirname(os.path.abspath(__file__))) current_directory = os.path.dirname(os.path.abspath(__file__)) print(16 * "++--") print("current_directory:", current_directory) print(16 * "++--") str_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G'] print("str_list:", str_list) print("str_list[3]:", str_list[3]) print("str_list[1:4]:", str_list[1:4]) print("str_list[:4]:", str_list[:4]) print("str_list[4:]:", str_list[4:]) print("str_list[::-1]:", str_list[::-1]) print("str_list[::2]:", str_list[::2])
/usr/bin/python3.5 /home/strong/sunergy_moonergy/object_counter/yongqiang.py ++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++-- current_directory: /home/strong/sunergy_moonergy/object_counter ++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++-- str_list: ['A', 'B', 'C', 'D', 'E', 'F', 'G'] str_list[3]: D str_list[1:4]: ['B', 'C', 'D'] str_list[:4]: ['A', 'B', 'C', 'D'] str_list[4:]: ['E', 'F', 'G'] str_list[::-1]: ['G', 'F', 'E', 'D', 'C', 'B', 'A'] str_list[::2]: ['A', 'C', 'E', 'G'] Process finished with exit code 0
The prototype of the slice operator in Python is [start:stop:step], [start index: end index: step value].
Start Index: Start at 0. In the left-to-right direction of the sequence, the index of the first value is 0 and the last value is -1.
End Index: The slice operator will take the index until it contains no value of the index.
Step size: The default is to cut one after another. If it is 2, it means to take one operation at a time. The value of step size is timed to indicate that it is taken from left to right. If it is negative, it means to take it from right to left. Step size should not be 0.
[:] Reproduces a list as it is.
Microsoft Windows [Version 6.1.7601] //Copyright (c) Microsoft Corporation, 2009. All rights reserved. C:\Users\foreverstrong>python Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> name_string = "yongqiang" >>> >>> name_string 'yongqiang' >>> >>> name_string[::-1] 'gnaiqgnoy' >>> >>> name_string[::-2] 'gaqny' >>> >>> name_string[::-3] 'gin' >>> >>> name_string[::3] 'yga' >>> >>> name_string[::2] 'ynqag' >>> >>> name_string[::1] 'yongqiang' >>> >>> exit() C:\Users\foreverstrong>
There are two commas and four colons in parentheses.
The first colon takes all the rows of the image.
The second colon takes all columns of the image.
The third and fourth colons go through all the channels of the image, - 1 is the reverse value.
Color image loaded by OpenCV is in BGR mode.
After executing image [:,:::::::::: -1], the row and column remain unchanged, and the channel number direction is changed from B, G, R to R, G, B.
Color image loaded by OpenCV is in BGR mode.
The first channel is B.
The second channel is G.
The third channel is R.
Img [:,:, 2] represents the R channel, that is, the red component image.
Img [:,:, 1] represents the G channel, that is, the green component image.
Img [:,:, 0] represents channel B, which is the blue component image.
#!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import print_function from __future__ import division import os import sys import cv2 import numpy as np sys.path.append(os.path.dirname(os.path.abspath(__file__))) current_directory = os.path.dirname(os.path.abspath(__file__)) print(16 * "++--") print("current_directory:", current_directory) print(16 * "++--") a = np.arange(27).reshape(3, 3, 3) print(a) ''' [[[ 0 1 2] [ 3 4 5] [ 6 7 8]] [[ 9 10 11] [12 13 14] [15 16 17]] [[18 19 20] [21 22 23] [24 25 26]]] ''' print(16 * "++--") b = a[:, :, ::-1] print(b) ''' [[[ 2 1 0] [ 5 4 3] [ 8 7 6]] [[11 10 9] [14 13 12] [17 16 15]] [[20 19 18] [23 22 21] [26 25 24]]] '''
/usr/bin/python2.7 /home/strong/sunergy_moonergy/object_counter/yongqiang.py ++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++-- current_directory: /home/strong/sunergy_moonergy/object_counter ++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++-- [[[ 0 1 2] [ 3 4 5] [ 6 7 8]] [[ 9 10 11] [12 13 14] [15 16 17]] [[18 19 20] [21 22 23] [24 25 26]]] ++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++-- [[[ 2 1 0] [ 5 4 3] [ 8 7 6]] [[11 10 9] [14 13 12] [17 16 15]] [[20 19 18] [23 22 21] [26 25 24]]] Process finished with exit code 0