numpy 我有一个永无止境的调试,不知道是什么问题,程序必须对图像应用双边滤波器

mqxuamgl  于 2023-08-05  发布在  其他
关注(0)|答案(1)|浏览(65)
import numpy as np
import cv2
path="C:\\Users\\NONSTOP.DESKTOP-I55D60P\\Desktop\\labs\\practice\\Image.jpg"
def bilateral_filter(image, d, sigmaColor, sigmaSpace):
    filtered_image = np.zeros_like(image, dtype=np.float32)  
    image = image.astype(np.float32) / 255.0    
    height, width = image.shape
    for i in range(height):
        for j in range(width):
            cpixel = image[i, j]
            total_weight = 0.0
            filtered_pixel = np.zeros_like(cpixel)
            for s in range(-d, d+1):
                for t in range(-d, d+1):
                    if i + s >= 0 and i + s < height and j + t >= 0 and j + t < width:
                        spixel = image[i + s, j + t]                   
                        I = np.linalg.norm(spixel - cpixel)
                        a = np.exp(-(I**2) / (2 * (sigmaColor**2)))
                        b = np.exp(-((s**2 + t**2) / (2 * (sigmaSpace**2))))
                        weight = a * b
                        filtered_pixel += spixel * weight
                        total_weight += weight
            
    filtered_image = filtered_image.astype(np.uint8)
    return filtered_image
image= cv2.imread(path,cv2.IMREAD_GRAYSCALE)
cv2.imshow('Image',image)
cv2.waitkey(5000)
cv2.destoryAllWindows()
filtered_image=bilateral_filter(image,d=9,sigmaColor=75,sigmaSpace=75)
cv2.imshow('FilteredImage',filtered_image)
cv2.waitKey(5000)
cv2.destroyAllWindows()

字符串
该程序不输出带有双边滤波器的图像。如果我试图显示原始图像,它会显示,然后python会关闭。

yqkkidmi

yqkkidmi1#

你犯了一些错误。我还认为你的图像太大,或者你的算法太慢。这就是为什么会出现not responding错误。这很容易验证。请继续读下去。

错别字

cv2.waitkey(5000)

字符串
应该是

cv2.waitKey(5000)


如果你问我

cv2.waitkey()


请参阅文档。
另一个typo:

cv2.destoryAllWindows()


应该是

cv2.destroyAllWindows()


然后,对于实际的事情。你没有告诉我们你想做什么,但我的猜测是,你已经实现了某种过滤器比你应用于图像。您希望将滤镜应用于图像,然后显示原始图像和滤镜图像。

建议

在main函数中移动所有内容。让你的代码更快。
下一次当您发布问题时,请与我们分享您正在使用的图像。使测试更容易。
我做了这个图像来测试你的代码:
x1c 0d1x的数据
在我修复了错别字之后,看起来你的代码中有一些“真实的的”错误。函数bilaterar_filter实际上 * 总是 * 返回黑色图像。在for循环中根本不需要编辑filtered_image。我很难猜出你想让你的函数做什么,但我猜这就是你想要的:

def bilateral_filter(image, d, sigmaColor, sigmaSpace):
    filtered_image = np.zeros_like(image, dtype=np.float32)  
    image = image.astype(np.float32) / 255.0    
    height, width = image.shape

    for i in range(height):
        for j in range(width):
            cpixel = image[i, j]
            total_weight = 0.0
            filtered_pixel = np.zeros_like(cpixel)

            for s in range(-d, d+1):
                for t in range(-d, d+1):
                    if i + s >= 0 and i + s < height and j + t >= 0 and j + t < width:
                        spixel = image[i + s, j + t]                   
                        I = np.linalg.norm(spixel - cpixel)
                        a = np.exp(-(I**2) / (2 * (sigmaColor**2)))
                        b = np.exp(-((s**2 + t**2) / (2 * (sigmaSpace**2))))
                        weight = a * b
                        filtered_pixel += spixel * weight
                        total_weight += weight
            filtered_image[i,j] = filtered_pixel
    filtered_image = filtered_image.astype(np.uint8)
    return filtered_image


这是使用上面函数的最终代码:

def main():
    image= cv2.imread(path,cv2.IMREAD_GRAYSCALE)
    filtered_image=bilateral_filter(image,d=9,sigmaColor=75,sigmaSpace=75)
    cv2.imshow('Image+Filtered',cv2.hconcat([image, filtered_image]))
    cv2.waitKey()
    cv2.destroyAllWindows()
    print("Exiting successfully")

if __name__=="__main__":
    main()


结果是:



如果你的代码仍然挂起,请注意,对于一张1920 x1080的图片,你的算法运行了4个for-循环1920*1080*20*20=829'440'000次。这就是为什么我用一张53 x80的小图片进行测试。但是优化代码超出了这个问题的范围。
请使用本文中的代码和图片测试您的代码,并验证您的算法是否按预期工作。

相关问题