自定义 2D 卷积不锐化图像

问题描述 投票:0回答:1

我自己写了2D卷积函数如下

def TwoD_convolution(img, kernel):

    # Flipping the kernel by 180 degrees

    kernel = kernel[::-1]
    for i in range(len(kernel)):
        kernel[i] = kernel[i][::-1]

    # Filling the new image with zeroes

    convolve = np.zeros((img.shape[0]+len(kernel)-1, img.shape[1]+len(kernel[0])-1, 3), np.uint8)

    midx = len(kernel)//2
    midy = len(kernel[0])//2

    # Trying to fill each cell one by one, and RGB values

    for i in range(convolve.shape[0]):
        for j in range(convolve.shape[1]):
            for k in range(3):

                cur = 0 #current sum

                for x in range(-midx,midx+1):
                    for y in range(-midy,midy+1):
                        if i+x >= 0 and i+x < img.shape[0] and j+y >= 0 and j+y < img.shape[1]:

                            # going through every neighbour of the middle cell
                            cur += ((img[i+x][j+y][k])*(kernel[midx+x][midy+y]))

                convolve[i][j][k] = cur

    return convolve

为了获得锐化图像,我按如下方式调用该函数:

display_image(TwoD_convolution(img,[[-0.5,-1,-0.5],[-1,7,-1],[-0.5,-1,-0.5]]))

其中display_image定义如下:

 plt.imshow(cv.cvtColor(img, cv.COLOR_BGR2RGB))
 plt.show()

它正在显示此图像:

我确信它在某些像素上变得锐化.. 但为什么边缘的颜色如此随机?我哪里出错了?

谢谢

python numpy opencv image-processing integer-overflow
1个回答
0
投票

我使用的内核是拉普拉斯内核,通常用于锐化图像。它增强了图像中的边缘和细节。

其次,在 TwoD_卷积 的代码中我进行了以下修改:

  1. 我尝试过对内核进行归一化,确保图像的整体强度在不同区域保持相对稳定。

  2. 我已经剪裁了这些值,确保不会发生溢出,从而最大限度地减少绿点等伪影。

我已经在谷歌colab上运行了这个并且它有效

import numpy as np
from PIL import Image
import matplotlib.pyplot as plt

import requests

def TwoD_convolution(img, kernel):
    kernel = kernel[::-1]
    for i in range(len(kernel)):
        kernel[i] = kernel[i][::-1]

    convolve = np.zeros((img.shape[0]+len(kernel)-1, img.shape[1]+len(kernel[0])-1, 3), np.uint8)

    midx = len(kernel) // 2
    midy = len(kernel[0]) // 2

    # HERE I NORMALIZE THE KERNEL
    kernel_sum = np.sum(kernel)
    if kernel_sum == 0:
        kernel_sum = 1  
    normalized_kernel = kernel / kernel_sum

    for i in range(convolve.shape[0]):
        for j in range(convolve.shape[1]):
            for k in range(3):
                cur = 0 
                for x in range(-midx, midx+1):
                    for y in range(-midy, midy+1):
                        if i+x >= 0 and i+x < img.shape[0] and j+y >= 0 and j+y < img.shape[1]:
                            cur += ((img[i+x][j+y][k]) * (normalized_kernel[midx+x][midy+y]))
                convolve[i][j][k] = np.clip(cur, 0, 255)  # RANGE CHECKING

    return convolve


url = 'https://s3.amazonaws.com/eit-planttoolbox-prod/media/images/Bellis_Perennis--Morgaine--CC_BY_2.0.jpg'
image = Image.open(requests.get(url, stream=True).raw) 
img_array = np.array(image)


kernel = np.array([[0, -1, 0],
                            [-1, 5, -1],
                            [0, -1, 0]])


convolved_img = TwoD_convolution(img_array, kernel)


plt.figure(figsize=(10, 5))

plt.subplot(1, 2, 1)
plt.imshow(img_array)
plt.title('Original Image')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(convolved_img)
plt.title('Convolved Image')
plt.axis('off')

plt.show()


很抱歉,如果我将所有这些代码放在这里,但这是为了让示例清晰

© www.soinside.com 2019 - 2024. All rights reserved.