【问题标题】:Irregular pixels in sharpened image when using 3x3 sharpening filter使用 3x3 锐化滤镜时锐化图像中的不规则像素
【发布时间】:2021-11-24 06:40:19
【问题描述】:

我正在尝试使用 3x3 内核滤镜来锐化黑白图像:

锐化内核 = [0 -1 0; -1 5 -1; 0 -1 0]

我试图使用 CUDA 处理来并行化该过程,但过滤器的结果会产生大量具有不规则值的突然像素:

为了交叉检查,我继续使用手动串行逻辑在 Python 中计算结果,我得到了相同的结果。 但是当我使用 OpenCV 的 cv2.filter2D 函数时,它似乎可以正常工作并给出以下输出:

我在这里附上了 Python 代码的串行实现。

import cv2 as cv2
import numpy as np
# load the image into system memory
image = cv2.imread('D:/PythonLab/resources/BlackWhite.jpg', flags=cv2.IMREAD_COLOR)

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

image_sharp=np.copy(image)

imrows=image.shape[0]
imcols=image.shape[1]

#pixelindexrow pr, pixelindexcol pc

for pr in range(imrows):
    for pc in range(imcols):
        start_r=pr-1
        start_c=pc-1
        temp=0
        for i in range(3):
            for j in range(3):
                if( start_r+i>=0 and start_r+i<imrows and start_c+j>=0 and start_c+j<imcols):
                    temp=temp+image[start_r+i][start_c+j][0]*kernel[i][j]
                
        image_sharp[pr][pc][0]=temp
        image_sharp[pr][pc][1]=temp
        image_sharp[pr][pc][2]=temp

cv2.imshow('Sharpened', image_sharp)
cv2.imwrite('D:/PythonLab/resources/kernelfilter_MANUAL.jpg', image_sharp)
cv2.waitKey()
cv2.destroyAllWindows()

谁能告诉我哪里出错了?在应用锐化滤镜之前是否需要任何其他预处理步骤?我不确定如何处理未绑定在 [0,255] 中的像素值。

【问题讨论】:

    标签: python image-processing


    【解决方案1】:

    您可能希望将计算值限制为 255,即,如果它们超过该值,请将它们设置为 255:

    outputImage[...] = min(255, temp)
    

    另外,而不是在if 语句中测试 4 个条件在双嵌套循环内超过 9 个值在双嵌套循环内超过图像宽度和高度......您可以只运行从第二个像素到倒数第二个像素的循环像素并删除测试:

    for pr in range(1,imrows-1):
      for pc in range(1,imcols-1):
    

    另外,我不确定您为什么将图像读取(并保存)为彩色 - 为什么不直接使用 cv2.imread(..., cv2.IMREAD_GRAYSCALE) 加载?

    【讨论】:

    • 我的错。我试图让原件处理彩色图像。我试过BW。卷积后的像素偏差范围为(-544,1145)。这是一个相当大的值,我认为钳制这些值实际上不会使其锐化。图像看起来仍然令人反感。有没有办法我可以将值标准化为 [0,255] 以便图像看起来更接近来自 CV2 过滤器的锐化图像
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 2011-11-09
    • 2020-03-08
    • 2017-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多