from PIL import Image import matplotlib.pyplot as plt import numpy as np from scipy import ndimage from skimage import filters # Đọc ảnh img = Image.open("test.jpg") # ========================= # 1. Vẽ Histogram # ========================= gray = img.convert("L") gray_array = np.array(gray) plt.figure("Histogram") plt.hist(gray_array.ravel(), bins=256, range=[0,256]) plt.title("Histogram") plt.xlabel("Muc xam") plt.ylabel("So pixel") plt.show() # ========================= # 2. Tăng độ sáng # MSSV: 2023606332 # 7 số cuối: 3606332 # Tổng = 23 # Giá trị tăng = 26 + 23 = 49 # ========================= img_array = np.array(img) bright = img_array + 49 bright = np.clip(bright, 0, 255) bright_img = Image.fromarray(bright.astype(np.uint8)) plt.figure("Tang do sang") plt.imshow(bright_img) plt.axis("off") plt.show() # ========================= # 3. Phép đóng ảnh (CLOSE) # dilation -> erosion # ========================= binary = gray_array > 128 dilation = ndimage.binary_dilation(binary) closing = ndimage.binary_erosion(dilation) plt.figure("CLOSE") plt.imshow(closing, cmap="gray") plt.axis("off") plt.show() # ========================= # 4. Phát hiện biên Sobel # ========================= sobel = filters.sobel(gray_array) plt.figure("Sobel") plt.imshow(sobel, cmap="gray") plt.axis("off") plt.show()