[computer vision processing III] basic image processing

[computer vision processing III] basic image processing

1. Image slicing

Previously, we learned that the image in opencv is actually an array of ndarray, and our operation on the array of ndarray is to operate on the image. Let's first look at slice lookup, which is a very common operation.

(1) Slice of one-dimensional array

Let's take a look at the syntax of slicing. For a one-dimensional array, we can obtain the 0th to 4th elements through the following operations:

array[0:5]

From the above, we can know that our slicing operation is closed on the left and open on the right. The above slicing operation can be abbreviated as follows:

array[:5]

If we don't set the first value, it means slicing from scratch. Of course, we can also omit the second value, which means to get the last element, such as the following operations:

array[3:]

Let's take a practical example to see the slicing operation:

import numpy as np
# Create a one-dimensional array of ndarray with data of [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 third to last element
print(array[3:])

The output contents are as follows:

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

We can summarize the slicing operation as follows:

# Left closed right open
array[start: end-1]

When we start with the 0 and end with the last, the corresponding value can be omitted.

(2) Slice of two-dimensional array

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

The syntax of two-dimensional array slice is as follows:

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

Now we need to make it clear that the left part is the interception of height and the right part is the interception of width. Now I want to intercept 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 intercept the left half
left = img[:, :width]
# Three steps for displaying images
cv2.imshow('left', left)
cv2.waitKey(0)
cv2.destroyAllWindows()

The code of the slice is:

left = img[:, :width]

On the left is the intercept 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:

It should be noted that the color image is actually three-dimensional, but we do not operate the third dimension.

2. Picture area replacement

Now that we know how to slice, we can replace the specified area. However, it should be noted that the shape of the replaced area and the replaced area should be the same, that is, the shape attribute should be the same. For example, if I want to replace the 100 * 100 area in the upper left corner of the picture with white, I can do the following:

import cv2
import numpy as np
img = cv2.imread('1.jpg')
# Create a 100 * 100 white image
replace_img = np.ones((100, 100), dtype=np.uint8)*255
# Replace the 100 * 100 area in the upper left corner of the picture
img[:100, :100] = replace_img

After running, the following errors will be found:

    img[:100, :100] = replace_img
ValueError: could not broadcast input array from shape (100,100) into shape (100,100,3)

It means that the image of (100100) cannot be converted into the image of (100100, 3), that is, the shape does not match. We need to pay special attention to this point when replacing. We will modify the above code:

import cv2
import numpy as np
img = cv2.imread('xyql.jpg')
replace_img = np.ones((100, 100, 3), dtype=np.uint8)*255
img[:100, :100] = replace_img

*In this way, we can successfully replace it. The effect is as follows:

In fact, with the above operation, we can achieve a "twin" effect. Let's have a look.

3. Achieve "twin" effect

We first prepare two pictures to ensure that the camera takes two pictures without moving, such as the following two:

These are the two pictures I just took. Because the camera doesn't move, the background should be the same. We can combine two dreams into one picture through the following code:

import cv2
# Read two pictures
img1 = cv2.imread('mh1.JPG')
img2 = cv2.imread('mh2.JPG')
# Find the middle value of the width
mid = img1.shape[1]//2
# Replace the right half of mh2 with the right half of mh1
img1[:, mid:] = img2[:, mid:]
# Save spliced pictures to local
cv2.imwrite('result.jpg', img1)

The above code is very simple. We only need to pay attention to the following sentence:

img1[:, mid:] = img2[:, mid:]

The shape s of img1 and img2 are the same, and the intercepted areas are the same, so there is no problem in replacing them. Finally, we passed:

cv2.imwrite('result.jpg', img1)

Save the picture. Because we operate on img1 directly, we just save img1 directly. Here are our renderings:

I need to say one more thing here. I only have one dream.

4. numpy generate array

In the previous article, we generated an array using the following code:

im = np.zeros((3, 3, 1), dtype=np.uint8)

For array numpy, we generate an array, but for opencv, we generate an image. Let's take a look at some operations of numpy to generate arrays.

(1)np.ones

We can generate an array with all elements of 1 through numpy's ones function, such as the following code:

np.ones((100, 100), dtype=np.uint8)

ones receives two parameters. The first parameter is the shape of the array, and the second parameter is the type of array element, where NP Uint8 means [unsigned 8-bit integer], that is, the range is between (0-255). We can use opencv to display the above picture:

import cv2
import numpy as np
# Generate a 100 * 100 picture, and the value of each element is 1
img = np.ones((100, 100), dtype=np.uint8)
cv2.imwrite('result.jpg', img)

The renderings are as follows:

Because 1 is very close to 0, the color of the picture is close to black. We can operate each element through the following operations:

import cv2
import numpy as np
img = np.ones((100, 100), dtype=np.uint8) * 127
cv2.imwrite('result.jpg', img)

After we generate an array with element 1, how to multiply 127, so that we can multiply 127 for each element. At this time, each element of the array is 127. The following is the display effect:

Of course, our picture is two-dimensional, which is a gray image for opencv. If you want to generate a color image, we can generate a three-dimensional image, which we will continue to explain later.

(2)np.zeros

np.zeros is no different 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 convenience, we directly generate a simple array, and the output results are 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 elaborate on anything else.

Posted by Jay_Seagrave on Tue, 03 May 2022 17:19:17 +0300