CV001: Image reading from file

Import

Firstly, importing three things before beginning. They are cv2, mataplotlib.pyplot and numpy.

cv001_1

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.

cv001_2

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).

cv001_3

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()).

cv001_4

cv001_5

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’).


cv001_6


cv001_7


cv001_8

One more thing, there is a method called figure, it can change the size of image.

cv001_9

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.

cv001_10

cv001_11

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