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