How to download short book videos? One trick to get the video and audio download tutorial

"
Insert picture description here
Before we learned that the image in opencv is actually an ndarray array, and we operate on the ndarray array to operate on the image. Let's first look at the slice search, which is a very common operation we use.

(1) Slicing of one-dimensional arrays
Let’s take a look at the syntax of slicing. For one-dimensional arrays, we can get the 0th to 4th elements through the following operations:

array[0:5]
From the above, we can know that our slicing operation is left closed and right opened. We can abbreviate the above slicing operation:

array[:5]
If we don't set the first value, it means slicing from the beginning. Of course, we can also omit the second value, which means to get the last element, such as the following operation:

array[3:]
Let's use a practical example to see the slice operation:

import numpy as np

Create a one-dimensional ndarray array with data [0, 1, 2, 3, 4, 5, 6, 7]

array = np.array([0, 1, 2, 3, 4, 5, 6, 7])

Take 0 to 4 elements

print(array[0:5])
print(array[:5])

Take the 3rd to the last element

print(array[3:])
输出内容如下:

[0 1 2 3 4]
[0 1 2 3 4]
[3 4 5 6 7]`

We can summarize the slicing operation as:

Close left and open right

array[start: end-1]
When we start with the 0th and get the end with the last, the corresponding value can be omitted.

(2) Slicing of two-dimensional arrays
In image processing, we pay more attention to the slicing of two-dimensional arrays. Its syntax is very similar to one-dimensional arrays. In order to facilitate understanding, we directly use pictures to slice, such as the following picture:

null

The syntax for slicing a two-dimensional array is as follows:

array[start:end-1, start:end-1]

Now we need to be clear, the left part is the interception of height, and the right part is the interception of width. So now I want to capture the left half of the picture as follows:

import cv2

Read picture

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

Get the width of the picture and divide by 2

width = img.shape[1]//2

Slice the picture and take the left half

left = img[:, :width]

Three steps to display images

cv2.imshow('left', left)
cv2.waitKey(0)
cv2.destroyAllWindows()

The code for slicing is:

left = img[:, :width]

The one on the left is the interception height, we need to intercept all, so both values ​​can be omitted. On the right, we only need to intercept the left half, so the value on the left can be omitted, and the value on the right is the width we calculated earlier. The following is the rendering:
Insert picture description here

(2) np.zeros
np.zeros does not have the above difference from ones, except that the content of its element is 0. Let's take a brief look:

import numpy as np
img = np.zeros((5, 5), dtype=np.uint8)
print(img)

For the convenience of viewing, we directly generate a simple array, the output result is as follows:

[[0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]]

I won’t go into details anymore.

Guess you like

Origin blog.csdn.net/qq_45984336/article/details/110825114