2024-programowanie-w-python.../zajecia7/1.ipynb
2024-12-14 01:00:42 +01:00

9.1 KiB
Raw Permalink Blame History

#!pip install opencv-python
import cv2
import matplotlib.pyplot as plt
import numpy as np

gray image

zadanie 1:

Znaleźć plik jpeg w internecie i zapisać go w zajecia7/image.jpeg

image_path = "image.jpeg"
image = cv2.imread(image_path)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[3], line 3
      1 image_path = "image.jpeg"
      2 aimage = cv2.imread(image_path)
----> 3 gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

NameError: name 'image' is not defined
gray_image
type(gray_image)
gray_image.shape
def display_image(image):
    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    plt.imshow(image_rgb)
    plt.axis('off')  # Turn off axes for better visualization
    plt.show()
display_image(gray_image)

Colour image

image_path = "image.jpeg"
image = cv2.imread(image_path)
image
type(image)
image.shape
display_image(image)

plot gray histogram

colors = ('b', 'g', 'r')
plt.figure()
plt.title('gray histogram')
plt.xlabel("Intensity")
plt.ylabel("Count")

hist = cv2.calcHist([gray_image], [0], None, [256], [0, 256])
plt.plot(hist, color='k')
plt.xlim([0, 256])

plot BGR histogram

colors = ('b', 'g', 'r')
plt.figure()
plt.title('colour histogram')
plt.xlabel("Intensity")
plt.ylabel("Count")

for i, col in enumerate(colors):
    hist = cv2.calcHist([image], [i], None, [256], [0, 256])
    plt.plot(hist, color=col)
    plt.xlim([0, 256])

Image manipulation

gray_image
gray_image_2 = gray_image.copy()
gray_image_2[:100, :100] = 0
gray_image_2[-50:, -50:] = 0
display_image(gray_image)
gray_image_3 = gray_image.copy()
binary_image = np.where(gray_image_3 > 128, 255, 0).astype(np.uint8)
display_image(binary_image)
gray_image_4 = gray_image.copy()
gray_image_4 = gray_image_4[:,::-1]
display_image(gray_image_4)
gray_image_5 = gray_image.copy()
gray_image_5 = gray_image_5[::-1,:]
display_image(gray_image_5)

Zadanie 2

Wymyślić 3 inne manipulacje ze zdjęciem za pomocą numpy (można wspomagać się internetem) i wykonać je na swoim obrazku