Trăn nhân gaussian 2d

Và chức năng tích chập 2d thực hiện tích chập 2D giữa hình ảnh và bộ lọc, hình ảnh là hình ảnh 2D

def myconv2[image, filt]:
    # INPUTS
    # @ image         : 2D image, as numpy array of size mxn
    # @ filt          : 1D or 2D filter of size kxl
    # OUTPUTS
    # img_filtered    : 2D filtered image, of size [m+k-1]x[n+l-1]

    m, n = image.shape
    k, l = filt.shape
    offsety = k // 2
    offsetx = l // 2
    img_filtered = np.zeros[[m+k-1, n+l-1], "double"]
    image = np.pad[image, [[offsety,offsety],[offsetx, offsetx]], mode='constant']
    
    for i in range[offsety, m+offsety]:
        for j in range[offsetx, n+offsetx]:
            box_vals = image[ i - offsety : i + offsety+1, j-offsetx: j+offsetx+1]
            new_val = np.sum[ filt *  box_vals]
            img_filtered[i][j] = np.sum[new_val]
    return img_filtered

Một bản trình bày đơn giản về cách thức hoạt động của chức năng đối với hình ảnh đầu vào 5x5 và nhân bộ lọc 3x3

Với gaussian 1d sau đây và chuyển vị của nó, tôi gọi hàm

import cv2
import numpy as np

image = cv2.imread['test.jpg']

# Print error message if image is null
if image is None:
    print['Could not read image']

# Apply identity kernel
kernel1 = np.array[[[0, 0, 0],
                    [0, 1, 0],
                    [0, 0, 0]]]

identity = cv2.filter2D[src=image, ddepth=-1, kernel=kernel1]

cv2.imshow['Original', image]
cv2.imshow['Identity', identity]
    
cv2.waitKey[]
cv2.imwrite['identity.jpg', identity]
cv2.destroyAllWindows[]

# Apply blurring kernel
kernel2 = np.ones[[5, 5], np.float32] / 25
img = cv2.filter2D[src=image, ddepth=-1, kernel=kernel2]

cv2.imshow['Original', image]
cv2.imshow['Kernel Blur', img]
    
cv2.waitKey[]
cv2.imwrite['blur_kernel.jpg', img]
cv2.destroyAllWindows[]
0

sigma = 3
filter_length = 5

gauss = gauss1d[sigma, filter_length].reshape[1,filter_length]
guass
array[[[0.18073067, 0.20897821, 0.22058223, 0.20897821, 0.18073067]]] 

gauss_t = np.transpose[gauss]
gauss_t 
array[[[0.18073067],
       [0.20897821],
       [0.22058223],
       [0.20897821],
       [0.18073067]]]

myconv2[gauss, guass_t]
array[[[0.        , 0.        , 0.        , 0.        , 0.        ],
       [0.        , 0.        , 0.        , 0.        , 0.        ],
       [0.03986597, 0.04609688, 0.04865652, 0.04609688, 0.03986597],
       [0.        , 0.        , 0.        , 0.        , 0.        ],
       [0.        , 0.        , 0.        , 0.        , 0.        ]]]

Như bạn có thể thấy nó không thực sự là nhân gaussian 2d và một số giá trị bị thiếu. Tôi không biết mình đang thiếu gì và tôi nên xem xét điều gì trong mã của mình để đạt được mục tiêu. Cảm ơn

Bài viết này sẽ giới thiệu cách sử dụng tích chập trong OpenCV và một số bộ lọc cơ bản để thực hiện bộ lọc hình ảnh ví dụ như làm mờ hoặc tăng độ nét cho ảnh gốc

Bài viết bao gồm các nội dung chính sau

1. Giới thiệu về mặt nạ tích chập [convolution kernel] trong xử lý ảnh

2. Sử dụng mặt nạ để làm mờ hoặc tăng độ nét của ảnh

3. Sử dụng mặt nạ đồng nhất [Identity Kernel] trong OpenCV

4. Làm mờ ảnh [làm mờ] sử dụng mặt nạ biến 2D tùy biến

5. Làm mờ ảnh sử dụng hàm có sẵn trong OpenCV

6. Áp dụng bộ lọc Gauss để làm mờ ảnh

7. Áp dụng bộ lọc trung vị [Median] để làm mờ ảnh

8. Tăng độ nét [Sharpening] của ảnh bằng mặt nạ chụp tùy biến 2D

9. Áp dụng phương thức lọc bài hát [Bộ lọc song phương] trong OpenCV

Trước khi bắt đầu vào từng phần cụ thể, hãy quan sát đoạn mã mẫu dùng để lọc ảnh. Chi tiết chức năng từng mã đoạn sẽ được giới thiệu ở các phần tương ứng

con trăn

import cv2
import numpy as np

image = cv2.imread['test.jpg']

# Print error message if image is null
if image is None:
    print['Could not read image']

# Apply identity kernel
kernel1 = np.array[[[0, 0, 0],
                    [0, 1, 0],
                    [0, 0, 0]]]

identity = cv2.filter2D[src=image, ddepth=-1, kernel=kernel1]

cv2.imshow['Original', image]
cv2.imshow['Identity', identity]
    
cv2.waitKey[]
cv2.imwrite['identity.jpg', identity]
cv2.destroyAllWindows[]

# Apply blurring kernel
kernel2 = np.ones[[5, 5], np.float32] / 25
img = cv2.filter2D[src=image, ddepth=-1, kernel=kernel2]

cv2.imshow['Original', image]
cv2.imshow['Kernel Blur', img]
    
cv2.waitKey[]
cv2.imwrite['blur_kernel.jpg', img]
cv2.destroyAllWindows[]

C++

// Import dependencies
#include 
#include 

// Using namespaces to nullify use of c::function[]; syntax and std::function[]; syntax
using namespace std;
using namespace cv;

int main[]
{
    // Read Image
    Mat image = imread["test.jpg"];

    // Print Error message if image is null
    if [image.empty[]] 
        {
            cout 

Chủ Đề