Loeng 14: Teadusarvutused Pythonis, algoritm (meeldetuletus), teegid: NumPy, SciPy, matplotlib, jm., andmemassiiv numpy.ndarray, jooniste ja graafikute loomine, joonistega töötamine Spyder IDEs ja Jupyteris, näiteid¶

Viimati uuendatud 8.12.2024.

Lisa: NumPy 3D andmemassiiv ja pildifail¶

Püüame visualiseerida andmemassiivi ja harjutada massiivi indeksitega töötamist. Salvesta pildifail juulius.jpg enda töökauta.

In [1]:
from matplotlib.image import imread  # Pildi andmete lugemine.
import matplotlib.pyplot as plt
import numpy as np
%config InlineBackend.figure_format='retina'  # Vajadusel sisesta Spyderi konsooli, maagiline käsk.

img = imread('juulius.jpg')  # 0 = must, 254 = max värvi, RGB fail.
plt.imshow(img)
plt.show()

print('Tüüp: ', type(img))
print('Shape: ', np.shape(img))
print('Dimesioonaalsus: ', np.ndim(img))

im2 = img[::-1, :, :]  # img[::-1]. Rida, veerg, sügavus.

plt.imshow(im2)
plt.show()
No description has been provided for this image
Tüüp:  <class 'numpy.ndarray'>
Shape:  (225, 225, 3)
Dimesioonaalsus:  3
No description has been provided for this image
In [2]:
im2 = img[100:130, 76:125, :]  # img[100:130, 76:125]

plt.imshow(im2)
plt.show()
No description has been provided for this image
In [3]:
im2 = img[::2, ::3, :]  # img[::2, ::3]

plt.imshow(im2)
plt.show()
No description has been provided for this image
In [4]:
im2 = img.copy()  # Loome koopia, et kaitsta originaali.

im2[:, :, 0] = 0  # Red channel in RGB.
im2[:, :, 2] = 0  # Blue channel in RGB.

plt.imshow(im2)
plt.show()
No description has been provided for this image
In [5]:
im2 = img[:, ::-1, :]  # Peegelpilt.

plt.imshow(im2)
plt.show()
No description has been provided for this image
In [6]:
im2 = np.where(img > 150, 255, 0)

plt.imshow(im2)
plt.show()
No description has been provided for this image
In [7]:
im3 = img[:, :, 1].T  # Transponeerimine.

plt.imshow(im3)
plt.show()
No description has been provided for this image
In [8]:
im2 = np.sort(im3)

plt.imshow(im2)
plt.show()
No description has been provided for this image
In [9]:
im2 = np.sort(img, axis=0)

plt.imshow(im2)
plt.show()
No description has been provided for this image
In [10]:
im2 = np.sort(img, axis=1)

plt.imshow(im2)
plt.show()
No description has been provided for this image
In [11]:
im2 = np.sort(img, axis=2)

plt.imshow(im2)
plt.show()
No description has been provided for this image

















☻   ☻   ☻