【发布时间】:2021-11-24 06:40:19
【问题描述】:
锐化内核 = [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] 中的像素值。
【问题讨论】: