This is the third day of my participation in the August Text Challenge.More challenges in August

Basic image processing

1. Image section

We saw earlier that an image in OpencV is really just an ARRAY of NdarRay, and we’re manipulating an array of NdarRay is manipulating an image. Let’s take a look at slice lookup, which is a very common operation.

(1) Slice of one-dimensional array

Let’s look at the syntax for slicing. For a one-dimensional array we can get elements 0 through 4 as follows:

array[0:5]
Copy the code

It can be seen from the above that our slicing operation is left closed and right open. We can abbreviate the above slicing operation:

array[:5]
Copy the code

If we do not set the first value, we are slicing from scratch. Of course, we can omit the second value, which means fetching the last element, as in the following operation:

array[3:]
Copy the code

Let’s look at the slicing operation with a practical example:

import numpy as np
Create a one-dimensional 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 third to last element
print(array[3:)Copy the code

The output is as follows:

[0 1 2 3 4]
[0 1 2 3 4]
[3 4 5 6 7]
Copy the code

We can summarize the slicing operation as follows:

# Close left, open right
array[start: end-1]
Copy the code

When we start with the 0th value and get the last value, 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 arrays. Its syntax is very similar to that of a one-dimensional array. For easy understanding, we use pictures to slice directly, such as the following picture:

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

array[start:end-1, start:end-1]
Copy the code

Now we need to make it clear that the left part is the height intercept, and the right part is the width intercept. Now I want to capture the left half of the image as follows:

import cv2
# fetch image
img = cv2.imread('xyql.jpg')
Get the width of the image and divide by 2
width = img.shape[1] / /2
# Slice the image and take the left half
left = img[:, :width]
# Display image three steps
cv2.imshow('left', left)
cv2.waitKey(0)
cv2.destroyAllWindows()
Copy the code

The sliced code is:

left = img[:, :width]
Copy the code

The one on the left is intercept height, and we need to intercept all of them, so we can omit both values. On the right we only need to intercept the left half, so the left value can be omitted and the right value is the width we calculated earlier. Here’s how it looks:

One thing to notice here is that the color image is actually three-dimensional, but we’re not manipulating the third dimension.

2. Image area replacement

Now that we know how to slice, we can replace the given region. However, it is important to note that the shape of the region to be replaced is the same as that of the region to be replaced, that is, the shape property should be the same. For example, if I want to replace the 100*100 area in the upper left corner of the image 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 image
img[:100To:100] = replace_img
Copy the code

The following error will be found after running:

    img[:100To:100] = replace_img
ValueError: could not broadcast input array from shape (100.100) into shape (100.100.3)
Copy the code

It means that you can’t convert the image of (100, 100) into the image of (100, 100, 3), so the shapes don’t match. We need to pay special attention to this when substituting, and we’ll make some changes to the code above:

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

* Then we can replace it successfully, and the effect is as follows:

In fact, with the above operation we can achieve a “twin” effect, let’s take a look.

3, to achieve the “twin” effect

Let’s first prepare two pictures and make sure the camera takes two pictures without moving, such as the following two pictures:

These are the two pictures I just took, and since the camera is not moving, the background should be the same. We can combine two dreams into one image with the following code:

import cv2
# read two images
img1 = cv2.imread('mh1.JPG')
img2 = cv2.imread('mh2.JPG')
Find the median 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 the stitched image locally
cv2.imwrite('result.jpg', img1)
Copy the code

The above code is very simple, we just need to focus on the following:

img1[:, mid:] = img2[:, mid:]
Copy the code

The shape of IMG1 and IMG2 is the same, and the intercepted region is the same, so there is no problem above for replacement. Finally we passed:

cv2.imwrite('result.jpg', img1)
Copy the code

Save the picture. Since we are operating directly on IMg1, we will simply save img1. Here are our renderings:

I need to add that I have only one dream.

Numpy generates arrays

In the previous article we used the following code to generate an array:

im = np.zeros((3.3.1), dtype=np.uint8)
Copy the code

For numpy we generate an array, but for OpencV we generate an image. Let’s take a look at some of the operations numpy uses to generate arrays.

(1) np. ‘ones

We can use numpy’s ones function to generate an array of all ones, as in the following code:

np.ones((100.100), dtype=np.uint8)
Copy the code

One takes two parameters, the first parameter is the shape of the array, the second one is the type of the array elements, where Np. Uint8 represents [unsigned 8-bit integer type], that is, the range is between (0-255). We can use OpencV to display the above image:

import cv2
import numpy as np
Create a 100*100 image with each element having the value 1
img = np.ones((100.100), dtype=np.uint8)
cv2.imwrite('result.jpg', img)
Copy the code

The renderings are as follows:

Since 1 is close to 0, the color of the image is close to black. We can manipulate each element by:

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

How do we multiply by 127 once we’ve created an array of elements 1, so we can multiply by 127 for each element. Each element in the array is 127. Here’s what it looks like:

Of course our image is two-dimensional, a grayscale image for OpencV. If we want to generate a color image, we can generate a three-dimensional image, which we’ll talk about later.

(2) np. Zeros

Np. zeros does not differ from ones except that its element content is 0. Let’s take a quick look:

import numpy as np
img = np.zeros((5.5), dtype=np.uint8)
print(img)
Copy the code

To make it easier to see, we generate a simple array with the following output:

[[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]]
Copy the code

I won’t go into any more details.