This is the 8th day of my participation in Gwen Challenge

Introduced what is computer vision, OpenCV provides us with what convenience to process pictures and its main application, finally introduced the image representation, and how to install

What is computer vision

First of all, computer vision is a discipline. It’s the study of how to make machines see. So what is seeing? That is to observe the surrounding environment to carry out image classification, target recognition, tracking and measurement of machine vision tasks.

Computer vision is a scientific discipline that studies related theories and techniques in an attempt to build artificial intelligence systems that can extract ‘information’ from images or multidimensional data.

What is the OpenCV

OpenCV stands for Open Source Computer Vision Library, which is a cross-platform Computer Vision Library. OpenCV was initiated and co-developed by Intel and is free to use in business and research.

  • cross-platform
  • Supports C, C++, and Python
  • The open source project
  • Easy to install

OpenCV can be used to develop real-time image processing, computer vision, and pattern recognition.

How does a computer represent an image

Today’s computers typically use 32 bits to represent colors, which are divided between four components, or 8 bits per component. So Red Red, Green Green, Blue Blue, each of them has 8 bits of space to represent them, so there’s 2 to the eighth possibilities which is 256 possibilities. Image we see is A plane, width and height, through A wide high position can represent A pixel position, the pixels are R, G and B channels can also be understood as A depth, then the image is actually made up of 3 d matrix, so said image is more simple and crude, actually does not contain any semantic information, So it makes the task of the computer more difficult.

Install OpenCV

You need to install Python (Python 3.6 or later is recommended) and run the following command

pip install opencv-python
Copy the code
Recommended installation

You are advised to install The Conda environment first, and then install Python openCV in the Conda environment.

Opencv reads the image

In OpenCV we operate more Matrix (Matrix) to achieve the image modification. In fact, we see a picture is a two-dimensional matrix composed of rows and columns of pixels, and each pixel contains CHANNELS such as R, G and B to store the picture information.

When it comes to matrices, we can’t help but mention Numpy, the nifty Python library for manipulating arrays. All OpenCV array structure data can be converted to the array structure provided by Numpy.

Read the pictures

You can download pictures from official download resources.

In fact, there is a story behind this Lena image as an image processing industry standard. Today, this is not the point. The reason for choosing this image is that it has a moderate mix of details, smooth areas, shadows and textures, which can be a good test of various image processing algorithms.

cv2.imread('lena.jpg',0)
Copy the code

The first parameter passes the image to be read, and the second parameter controls the mode in which the image is loaded

  • Cv2.imread_color (1) indicates the reserved color of the loaded image
  • Cv2. IMREAD_GRAYSCALE (0) indicates that only grayscale is reserved for loading images
  • Cv2.imread_unchanged (-1) loaded images remain transparent channels
img = cv2.imread('lena.jpg',0)
print(img)
Copy the code
[[163 162 161... 170 154 130] [162 162 162... 173 155 126] [162 162 163... 170 155 128] [43 42 51... 103 101 99] [41 42 55... 103 105 106] [42 44 57... 102 106 109]Copy the code

When the imread method does not find the image file, it returns None instead of an array

OpenCV reads the image

cv2.imshow('image',img)
cv2.waitKey(1)
cv2.destroyWindows()
Copy the code

You can also use the imshow method to display the image on the screen, but just imshow will flash the image, you can use waitKey to add the image until the user clicks the button, The method parameter 5000 indicates that the image window will close after 5 seconds if the user does nothing.

Write image

cv2.imwrite('lena_copy.png',img)
Copy the code