This article summarizes the data structures commonly used in machine vision (image processing) programming using Python, including the following:
data structure
Sequence operations: indexing, slicing, adding, multiplication (mulTIpying), etc.
List: create, list function, basic operations: assignment, deletion, shard assignment, insert, sort, etc.
Tuple: creation, tuple function, basic operation
NumPy array: create arrays, create images, get shapes, dimensions, number of elements, element types, access pixels, channel separation, use mask
1. Data Structures Data structures are collections of data elements that are organized together in a way, such as numbering elements. These data elements can be numbers or characters, or even other data structures. The most basic data structure in Python is the sequence. Each element in the sequence is assigned a sequence number—the position of the element, also known as the index. The index of the first element is 0, the second is 1, and so on.
Python contains six built-in sequences, the two most common types are: lists and tuples. The main difference between lists and tuples is that lists can be modified, and tuples cannot be modified. The basic data structure for processing images is an array. Since the built-in arrays in the Python standard library can only handle one-dimensional arrays and provide fewer functions, the NumPy module's array() array is often used to represent images when programming. Carry out various treatments.
2. General Sequence Operations All sequences can perform certain operations, including: indexing, slicing, adding, multiplication (mulTIpying), and checking whether an element belongs to a sequence member (membership). In addition, Python has built-in functions that calculate the length of the sequence and find the largest and smallest elements.
(1) Index The number of all elements in the sequence is incremented from 0.
>>>greeTIng = 'Hello'
>>>greeTIng[0]
'H'
>>>greeting[1]
'e'
All sequences can get elements in this way. The last element is numbered -1
>>>greeting[-1]
'o'
If a function call returns a sequence, you can index the return sequence directly
>>>fourth = raw_input('Year: ')[3]
Year: 2016
>>>fourth
6
(2) Sharding Use the sharding operation to position elements within a certain range. Fragmentation is achieved by separating two indexes by a colon.
>>>tag='Python web site'
>>>tag[9:30]
'"http:// '
>>>numbers=[1,2,3,4,5,6,7,8,9,10]
>>>numbers[3:6]
[4,5,6]
>>>numbers[0:1]
[1]
Note the index boundary: the element of the first index is contained in the slice, and the element of the second index is not in the slice, if the last element is to be indexed
>>>numbers[-3:]
[8,9,10]
>>>print numbers[-1:]
[10]
This method also applies to elements at the beginning of the sequence:
>>>numbers[:3]
[1,2,3]
If you need to copy the entire sequence, you can leave both indexes blank:
>>>numbers[:]
[1,2,3,4,5,6,7,8,9,10]
We can also use the third parameter to set the step size of the slice. The following code is an element from 0 to 10 with a step size of 2 from the numbers sequence.
>>>numbers[0:10:2]
[1,3,5,7,9]
If you want to extract the first of every 4 elements, you can write
>>>numbers[::4]
[1,5,9]
A step with a negative number will extract the element to the left. When using a negative number as the step size, the index of the point that begins must be greater than the index of the end point.
>>>number[8:3:-1]
[9,8,7,6,5]
>>>numbers[10:0:-2]
[10, 8, 6, 4, 2]
(3) Sequence addition Use the + operator to perform sequence connection operations:
>>>[1,2,3] + [4,5,6]
[1,2,3,4,5,6]
>>>'Hello, ' + 'world!'
'Hello, world!'
Note that sequences of the same type can be joined together, and lists and strings cannot be connected.
(4) Multiplication The multiplication of the number x by the sequence generates a new sequence. In the new sequence, the original sequence will be repeated x times
>>>'pyhton' * 5
'pyhtonpyhtonpyhtonpyhtonpyhton'
>>>[42]*10
[42,42,42,42,42,42,42,42,42,42]
(5) None empty list and initialization Empty list can be represented by two middle brackets without writing []
If you want to create a list that takes up 10 element spaces but doesn't include any useful content, you can use:
>>>[0]*10
None is a Python built-in value, and its exact meaning is that there is nothing here.
>>>[None]*10
(6) Membership in
To check if a value is in the list, you can use the in operator to return a boolean true or false:
>>>permission = 'rw'
>>>'w' in permission
True
>>>'x' in permission
False
In the example below, check the username and PIN:
Database = [
['albert', '1234'],
['dilbert', '4242'],
['smith', '7524'],
['jones', '9843']
]
Username = raw_input('User name: ')
Pin = raw_input('PIN code: ')
If [username,pin] in database:
Print 'Access granted'
operation result:
User name: jones
PIN code: 9843
Access granted
(7) length, minimum and maximum built-in functions len, min, max
>>>numbers[100,34,678]
>>>len(numbers)
3
>>>max(numbers)
678
>>>min(numbers)
34
>>>max(2,3)
3
>>>min(2,3,4,5)
2
3. List
(1) list function Because strings cannot be modified like lists, sometimes it is useful to create lists based on strings.
>>>list('Hello')
['H','e','l','l','o']
List applies to all types of sequences, not just lists.
(2) List basic operations Element assignment
>>>x=[1,1,1]
>>>x[1]=2
>>>x
[1,2,1]
Delete element
>>>x=[1,2,3]
>>>del x[1]
>>>x
[1,3]
Del can also delete other elements, even variables.
Fragment assignment
>>>name=list('Perl')
>>>name
['P', 'e', ​​'r', 'l']
>>>name[2:]=list('ar')
>>>name
['P', 'e', ​​'a', 'r']
Inserting and deleting elements by shard assignment
>>>numbers=[1,5]
>>>numbers[1:1]=[2,3,4]
>>>numbers
[1,2,3,4,5]
>>>numbers[1:4]=[]
>>>numbers
[1,5]
(3) List method Use of list method: object. method (parameter)
Append appends at the end of the list
>>>a = [1,2,3]
>>>a.append['4']
>>>a
[1,2,3,4]
Count counts the number of times an element appears in the list
>>>['to','go','will','be','to','not'].count('to')
2
Extend multiple values ​​in another sequence at the end of the list
>>>a = [1,2,3]
>>>b = [4,5,6]
>>>a.extend(b)
>>>a
[1,2,3,4,5,6]
>>>c = [1,2,3]
>>>d = [4,5,6]
>>>c+d
[1,2,3,4,5,6]
>>>c
[1,2,3]
Index Find the index position of the first match of a value from the list
>>>slogen= ['we', 'are', 'the', 'champion']
>>>slogen.index('are')
1
>>> slogen[1]
'are'
Insert inserts the object into the list
>>>numbers=[1,2,3,4,6]
>>>numbers.insert(4,'five')
>>>numbers
[1, 2, 3, 4, 'five', 6]
The first parameter is the inserted position, inserted before this index;
The second parameter is the inserted element content.
The operation of the insert method can also implement element insertion by fragmentation.
>>>numbers=[1,2,3,4,6]
>>>numbers[4:4]=['five']
>>>numbers
[1, 2, 3, 4, 'five', 6]
Pop removes an element from the list
The pop method implements a common data structure - the stack. The principle of the stack is like stacking plates. You can only put one plate on the top, and you can only take one plate from the top. The last element that was put on the stack was removed first. (This principle is called last in, first out, LIFO).
The pop() method removes the last element in the list by default and returns the value of the element.
>>>numbers=[1,2,3,4,5]
>>>numbers.pop()
5
>>> numbers
[1, 2, 3, 4]
If you want to remove the first element in the list, you can use pop(0)
>>>numbers=[1,2,3,4,5]
>>>numbers.pop(0)
1
>>> numbers
[2, 3, 4, 5]
Python does not have a push operation and can be replaced with the append method.
If you want to implement a first in first out (FIFO) queue, you can use insert(0,...) instead of the append method. Or you can use the append method, but you must replace pop() with pop(0). You can also use the deque object in the collection module.
Remove removes the first occurrence of a value in the list
>>>x=['to','go','will','be','to','not']
>>>x.remove('to')
>>>x
['go','will','be','to','not']
Reverse reverses the elements in the list
>>>x=[1,2,3]
>>>x.reverse()
>>>x
[3,2,1]
Sort sorts the list at the original position Sorting at the original position changes the original list so that the elements in it can be rearranged in a certain order, rather than simply returning a sorted list copy.
>>>x=[3,1,2,6,4,5,7,9,8]
>>>x.sort()
>>>x
[1,2,3,4,5,6,7,8,9]
Note: The sort method only changes the sorting of the original list and does not return a value. If you need a reordered copy of the list, you should do the following:
>>>x=[3,1,2,6,4,5,7,9,8]
>>>y=x[:] #Cannot directly y=x
>>>y.sort()
>>>y
[1,2,3,4,5,6,7,8,9]
>>>x
[3,1,2,6,4,5,7,9,8]
Note: y=x just lets y and x point to the same list, and y=x[:] copies the entire list of x to y.
Sorted to get a copy of the sorted list
>>>x=[3,1,2,6,4,5,7,9,8]
>>>y=sorted(x)
>>>y
[1,2,3,4,5,6,7,8,9]
>>>x
[3,1,2,6,4,5,7,9,8]
Advanced sorting of the sort method Hopefully the list elements can be sorted in a specific way (instead of the default way of the sort function, which sorts the elements in ascending order according to Python's default collation), and the comparison function can be customized by compare(x,y). The compare(x,y) function returns a positive number on xy and 0 on x=y (as defined by itself). Once the function is defined, it can be supplied as a parameter to the sort method. The built-in function cmp provides the default implementation of the comparison function:
>>>cmp(16,12)
1
>>>cmp(10,12)
-1
>>>cmp(10,10)
0
>>>numbers=[1,4,2,9]
>>>numbers.sort(cmp)
>>>numbers
[1,2,4,9]
The sort method has two other parameters to choose from, which can be specified by a name (keyword parameter):
Parameters: key
Provides a function to use in sorting. Instead of directly determining the size of the object, it creates a key for each element, and then all elements are sorted by key.
>>>x=['nor','break','if','then','present']
>>>x.sort(key=len) # Sort by string length
>>>x
['if', 'nor', 'then', 'break', 'present']
Barrier Strip Connector ,Barrier Type Terminal Block ,Dual Row Terminal Block ,Barrier Terminal
Cixi Xinke Electronic Technology Co., Ltd. , https://www.cxxinke.com