[PYTHON] Understand how to display images on Jupyter (utilization of imshow / matplotlib of OpenCV)

Introduction

This article is an article that tries to understand how to display an image on Jupyter and the function corresponding to the output image by changing the argument and list. I am a beginner in machine learning, so I would be very grateful if you could point out any opinions or mistakes.

wrap up

Display images inline

pic.ipynb



import cv2
import matplotlib.pyplot as plt

img = cv2.imread('012.JPG') #Pic any image you want to load.Put it in the same folder as ipynb.
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(1,1,1)
ax.imshow(img)
ax.axis("off")

012.png

First of all, I displayed the image basically. Here, (-0.5,719.5,719.5, -0.5) means that the lower left coordinate is (-0.5,719.5) and the upper right coordinate is (719.5, -0.5). The display of the image is different from the xy-axis direction learned in mathematics, and the origin is at the upper left, so it will be as follows.

014.png

Check the size of the image

pic.ipynb


img.shape
(720, 720, 3)

Since it represents (vertical resolution, horizontal resolution, number of channels), you can see that there are 3 channels of 720 x 720 (= RGB color). ** Note that if this is expressed in xy, it will be (y, x, number of channels). ** **

Display the coordinates of the image and the image at that position

pic.ipynb


img[0,0]
array([162, 127,  77], dtype=uint8)

It turns out that the RGB value of the origin position (0,0) is (162,127,77). Furthermore, when it is displayed, it will be as follows.

pic.ipynb



fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111)
ax.imshow(img[0:1,0:1])#Display and specify the value coordinates in the range
ax.axis("on")

015.png

Even if you want to display (0,0) coordinates, an error will occur unless you specify a range such as [0: 1,0: 1], so be careful.

Pay attention to the x and y coordinates when displaying

pic.ipynb


fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111)
ax.imshow(img[0:720,0:10])
ax.axis("on")

If you specify img [0: 720,0: 10], it will be displayed vertically because it will be displayed as y: 0 to 720 and x: 0 to 10. Note that the opposite is true if there are heads in the x and y coordinates. The image below is a vertically long image.

018.png

Mosaic using the resize module

pic.ipynb


img_re= cv2.resize(img, dsize=None, fx=0.1, fy =0.1)
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(1,1,1)
ax.imshow(img_re)
ax.axis("off")

Mosaic is applied by reducing the number of pixels to 1/10 with the cv2.resize () module. In the argument in resize, fx and fy are the magnifications of x and y. This time, by setting it to 0.1 (= 10%), the number of pixels was reduced to 1/10.

016.png

Increase transmittance

pic.ipynb


fig, ax = plt.subplots(facecolor="w")
ax.imshow(img, alpha=0.5)

plt.show()

By specifying the argument to alpha of the imshow module in the range of 0 to 1 and determining the transmittance, it is possible to change to a lighter shade.

017.png

At the end

If you want to handle and process images, this operation is basic. I thought it was very important to get a real feeling by moving my hands myself.

The full program text is stored below. https://github.com/Fumio-eisan/jupyter_pic20200313

Recommended Posts

Understand how to display images on Jupyter (utilization of imshow / matplotlib of OpenCV)
How to display images continuously with matplotlib Note
How to display multiple images of galaxies in tiles
Display PIL images on Jupyter
How to install OpenCV on Mac
How to run matplotlib on heroku
How to use Jupyter on the front end of supercomputer ITO
How to suppress display error in matplotlib
How to display emoji on Manjaro Linux
Display the graph of tensorBoard on jupyter
Jupyter Notebook Basics of how to use
How to draw OpenCV images in Pygame
[Python] How to specify the window display position and size of matplotlib
I want to display an image on Jupyter Notebook using OpenCV (mac)
How to install OpenCV on Jetson Nano Python
How to make multiple kernels selectable on Jupyter
I want to display multiple images with matplotlib.
How to solve the problem that only the process remains when you press cross on the imshow screen of OpenCV
[Python] How to install OpenCV on Anaconda [Windows]
I tried to summarize how to use matplotlib of python
How to view progress bar on Jupyter Notebook to see progress
How to use Matplotlib
How to display a specified line of a file or command result on Linux (sed, awk)
Create a function to display images like Jupyter / RStudio [Docker]
Make the display of Python module exceptions easier to understand
[Ev3dev] How to display bmp image on LCD with python
How to display a list of installable versions with pyenv
How to increase the number of machine learning dataset images
How to register a package on PyPI (as of September 2017)
How to see the contents of the Jupyter notebook ipynb file
Unable to display tensorboard in jupyter notebook on docker (solved)
When generating a large number of graphs with matplotlib, I do not want to display the graph on the screen (jupyter environment)
How to unify the bin width when displaying multiple histograms on top of each other (matplotlib)
For those of you who don't know how to set a password with Jupyter on Docker
Japanese display of matplotlib, seaborn
How to change Jupyter layout
How to register on pypi
Label images on jupyter lab
Notes on imshow () in OpenCV
How to use Jupyter Notebook
How to display a specified column of files in Linux (awk)
Day 65 I installed matplotlib to draw graphs on my Jupyter notebook.
How to install OpenCV on Cloud9 and run it in Python
Display the image of the camera connected to the personal computer on the GUI.
Resolve garbled Japanese characters in matplotlib of Jupyter Notebook on Docker
Real-time display of video acquired from webcam on Jupyter notebook (Python3)
How to run Jupyter and Spark on Mac with minimal settings
How to display PDF resolution and detailed information on Linux (pdfinfo)
How to save only a part of a long video using OpenCV
How to use machine learning for work? 01_ Understand the purpose of machine learning
How to update the python version of Cloud Shell on GCP
How to put OpenCV in Raspberry Pi and easily collect images of face detection results with Python