code python cho Kernel, Sobel, Laplacian, Canny, chuyển động mờ Blur, Enbossing
- User
code python cho Kernel, Sobel, Laplacian, Canny, chuyển động mờ Blur, Enbossing
- Assistant
Dưới đây là đoạn mã Python với sự sử dụng của thư viện OpenCV để thực hiện các phép biến đổi hình ảnh như Kernel, Sobel, Laplacian, Canny, Motion Blur và Embossing:
Đảm bảo bạn đã cài đặt OpenCV trước khi chạy đoạn mã này, bạn có thể cài đặt nó sử dụng pip:
pip install opencv-pythonSau đây là các đoạn mã Python:
import cv2 import numpy as np # Đọc hình ảnh img = cv2.imread('your_image.jpg', cv2.IMREAD_GRAYSCALE) # Kernel (custom filtering) kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]) sharpened_img = cv2.filter2D(img, -1, kernel) # Sobel operator sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3) sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3) # Laplacian laplacian = cv2.Laplacian(img, cv2.CV_64F) # Canny edge detection canny_edges = cv2.Canny(img, threshold1=100, threshold2=200) # Motion Blur size = 15 # Tạo motion blur kernel motion_blur_kernel = np.zeros((size, size)) motion_blur_kernel[int((size-1)/2), :] = np.ones(size) motion_blur_kernel = motion_blur_kernel / size # Áp dụng kernel trên hình ảnh motion_blurred = cv2.filter2D(img, -1, motion_blur_kernel) # Embossing embossing_kernel = np.array([[-2, -1, 0], [-1, 1, 1], [0, 1, 2]]) embossed_img = cv2.filter2D(img, -1, embossing_kernel) # Hiển thị hình ảnh cv2.imshow('Sharpened Image', sharpened_img) cv2.imshow('Sobel X', sobelx) cv2.imshow('Sobel Y', sobely) cv2.imshow('Laplacian', laplacian) cv2.imshow('Canny Edges', canny_edges) cv2.imshow('Motion Blur', motion_blurred) cv2.imshow('Embossed Image', embossed_img) cv2.waitKey(0) cv2.destroyAllWindows() - User
ảnh cố định rồi nhưng ảnh to quá là nó thiếu
- Assistant
Nếu bạn nhận thấy rằng ảnh hiển thị quá lớn và không vừa với màn hình, bạn có thể tự định cỡ lại hình ảnh trước khi hiển thị. Dưới đây là đoạn mã được sửa đổi để thêm chức năng này:
import cv2 import numpy as np # Hàm để định cỡ lại ảnh cho phù hợp với màn hình def resize_with_aspect_ratio(image, width=None, height=None, inter=cv2.INTER_AREA): dim = None (h, w) = image.shape[:2] if width is None and height is None: return image # Không cần thay đổi kích thước if width is None: # Tính toán tỷ lệ của chiều cao mới đối với chiều cao gốc r = height / float(h) dim = (int(w * r), height) else: # Tính toán tỷ lệ của chiều rộng mới đối với chiều rộng gốc r = width / float(w) dim = (width, int(h * r)) # Định cỡ lại hình ảnh resized = cv2.resize(image, dim, interpolation=inter) return resized # Đọc hình ảnh img = cv2.imread('your_image.jpg', cv2.IMREAD_GRAYSCALE) # Làm nhỏ hình ảnh nếu cần với chiều rộng là 600 pixels resized_img = resize_with_aspect_ratio(img, width=600) # Các phép biến đổi (đã trình bày ở trên, sử dụng `resized_img` thay vì `img`) # ... # Hiển thị hình ảnh (sử dụng `resized_img` thay vì `img`) cv2.imshow('Resized Image', resized_img) # ... cv2.waitKey(0) cv2.destroyAllWindows() - User
cập nhật chức năng # Kernel (custom filtering) kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]) sharpened_img = cv2.filter2D(img, -1, kernel)
Sobel operator
sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3) sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3)
Laplacian
laplacian = cv2.Laplacian(img, cv2.CV_64F)
Canny edge detection
canny_edges = cv2.Canny(img, threshold1=100, threshold2=200)
Motion Blur
size = 15
Tạo motion blur kernel
motion_blur_kernel = np.zeros((size, size)) motion_blur_kernel[int((size-1)/2), :] = np.ones(size) motion_blur_kernel = motion_blur_kernel / size
Áp dụng kernel trên hình ảnh
motion_blurred = cv2.filter2D(img, -1, motion_blur_kernel)
Embossing
embossing_kernel = np.array([[-2, -1, 0], [-1, 1, 1], [0, 1, 2]]) embossed_img = cv2.filter2D(img, -1, embossing_kernel)
- Assistant
Như bạn đã yêu cầu, dưới đây là phần cập nhật của mã sử dụng hình ảnh đã định cỡ. Ta sẽ thay
imgbằngresized_imgtrong mỗi phép biến đổi để áp dụng các bộ lọc trên hình ảnh đã được điều chỉnh kích thước:# ... (phần mã trước đó để đọc và định cỡ lại hình ảnh) # Kernel (custom filtering) kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]) sharpened_img = cv2.filter2D(resized_img, -1, kernel) # Sobel operator sobelx = cv2.Sobel(resized_img, cv2.CV_64F, 1, 0, ksize=3) sobely = cv2.Sobel(resized_img, cv2.CV_64F, 0, 1, ksize=3) # Laplacian laplacian = cv2.Laplacian(resized_img, cv2.CV_64F) # Canny edge detection canny_edges = cv2.Canny(resized_img, threshold1=100, threshold2=200) # Motion Blur size = 15 motion_blur_kernel = np.zeros((size, size)) motion_blur_kernel[int((size-1)/2), :] = np.ones(size) motion_blur_kernel = motion_blur_kernel / size motion_blurred = cv2.filter2D(resized_img, -1, motion_blur_kernel) # Embossing embossing_kernel = np.array([[-2, -1, 0], [-1, 1, 1], [0, 1, 2]]) embossed_img = cv2.filter2D(resized_img, -1, embossing_kernel) # Hiển thị hình ảnh (sử dụng các biến mới để hiển thị) cv2.imshow('Sharpened Image', sharpened_img) cv2.imshow('Sobel X', sobelx) cv2.imshow('Sobel Y', sobely) cv2.imshow('Laplacian', laplacian) cv2.imshow('Canny Edges', canny_edges) cv2.imshow('Motion Blur', motion_blurred) cv2.imshow('Embossed Image', embossed_img) cv2.waitKey(0) cv2.destroyAllWindows()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.