Import
Firstly, importing three things before beginning. They are cv2, mataplotlib.pyplot and numpy.
Image reading
It has method called imread, the first parameter is the path of image, second one is 1 or 0. 1 is represented as color picture and 0 is black and white.
And shape method will show the data of this image, it return a tuple and first element is the height of this image, second is the width and third one is represented as the numbers of pixel channel. For example, (100,100,3) is 100px height, 100px width and have three channels (RGB).
Tips: add a ‘?’ after method in Jupyter notebook and run, you will see the parameter needed and deo. And another way, move cursor on the method and press TAB+SHIFT.
Image show
There are two ways to show a image.
CV2
Using imshow method, which has two parameter, first one is the name of image and second one is the variable of image. But it will be an ERROR if we just use imshow. Define a variable called key, which is represented as the value of keyboard input via waitKey method. And when key is 27 (ESC, mostly use), shut down any windows (cv2.destroyAllWindows()).
As you see, it is a little bit complicate and not convenient to see the image.
mataplotlib.pyplot
It is a easier way to do it, just use imshow method and show method. But, carefully we need to convert the color space from BGR to RGB if it is a color image. If it is a black and white one, we need add a parameter called cmap (cmap=’gray’).
One more thing, there is a method called figure, it can change the size of image.
This way is easier and more convenient to display the image (in Jupyter notebook), so recommended it to use. Simply, we can define a function to expression it and use it fixed after.
Click this link to get the code in my github, you can download and import it if you need. And everything above is tested via Jupyter notebook.
End
Illustrator / Cagy
Text / Cagy
Editor / Cagy
Design / Cagy