[PYTHON] [OpenCV] About the array returned by imread

1. Background

When I was writing a face recognition program using deep learning, I had to prepare an image showing only the face. As a method, a face was detected from an image showing a person, and that part was cut out. At that time, I cut it using the array returned by the imread method of OpenCV. In this article, I will explain the array returned by imread.

2. What is imread

It is a function that can read an image from a file by specifying the image to be read as an argument, such as image = imread ('sample.jpg'). At this time, the pixel value (RGB value) of each pixel of sample.jpg is stored in image by imread. Actually, execute the following program and check it.

imread_test.py


import cv2

#Load image
image = cv2.imread('sample.jpg')

#Output the array returned by imread
print(image)
	
#Display the loaded image
cv2.imshow('img',image)
cv2.waitKey(0)
cv2.destroyAllWindows()

The output is as follows. 2020-02-16 (2).png In this way, it can be seen that the three-dimensional array is output. Let this array be an A × B × 3D array. A is the number of rows of pixels and B is the number of columns of pixels (variables A and B are used because the size of the matrix changes depending on the size of the image to be read). 3 is the brightness of RGB. In the image above, a lot of brightness is lined up vertically, but this is They are arranged in the order of [[[Brightness of 0th row and 0th column] ~ [Brightness of 0th row and Bth column]] ~ [[Brightness of A row and 0th column] ~ [Brightness of A row and Bth column]]]. .. (The 0th row and 0th column in the image is the upper left) Therefore, the array returned by imread stores the brightness of the pixels in the order of the matrix.

3. Digression (how to cut the image)

In the background of Chapter 1, as explained, the face image was cut out using the array returned by imread. First, the upper left position and the lower right position of the detected face image are acquired. Then, it was possible to cut out by extracting only the part of the face image (the brightness of the block matrix from the upper left matrix of the face image to the lower right matrix) of the array returned by imread. I will explain the details in another article.

Recommended Posts

[OpenCV] About the array returned by imread
About Opencv ③
About Opencv ①
Linux: Fix the address returned by malloc
Check the operation of OpenCV3 installed by Anaconda
About the test
Sort the elements of the array by specifying the conditions
About the queue
The avi file output by OpenCV cannot be played
Optical Flow, the dynamics of images captured by OpenCV
Read the OpenCV documentation
Summary about Python3 + OpenCV3
About the Unfold function
About the service command
About the confusion matrix
About the Visitor pattern
Explain the associative array
About the number (section number) in () displayed by the Linux man command