9.1 KiB
9.1 KiB
#!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)
[0;31m---------------------------------------------------------------------------[0m [0;31mNameError[0m Traceback (most recent call last) Cell [0;32mIn[3], line 3[0m [1;32m 1[0m image_path [38;5;241m=[39m [38;5;124m"[39m[38;5;124mimage.jpeg[39m[38;5;124m"[39m [1;32m 2[0m aimage [38;5;241m=[39m cv2[38;5;241m.[39mimread(image_path) [0;32m----> 3[0m gray_image [38;5;241m=[39m cv2[38;5;241m.[39mcvtColor(image, cv2[38;5;241m.[39mCOLOR_BGR2GRAY) [0;31mNameError[0m: 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