简体   繁体   English

函数陷入无限循环,找不到原因

[英]Function stuck in infinite loop, cannot find reason

The code is supposed to take each pixel rgb value and store it in a 2d array (eg pixels[pixelNumber][r, g, b]), where it then gets sent to a user defined method to, in this case, blur it (using Gaussian blur). 该代码应采用每个像素的rgb值并将其存储在2d数组中(例如,pixels [pixelNumber] [r,g,b]),然后将其发送到用户定义的方法,以在这种情况下对其进行模糊处理(使用高斯模糊)。 Width and height are pixel width and height definitions taken from the image, and radius is the user defined radius of which pixels get averaged to be blurred. 宽度和高度是从图像中获取的像素宽度和高度定义,而半径是用户定义的平均像素模糊的半径。 It gave me a divide by 0 error at 它给我除以0的错误

weight_r = pixels[j][0] * weight
weight_g = pixels[j][1] * weight
weight_b = pixels[j][2] * weight

before I changed total_weight from 0 to 1 when defined just below the first for loop, if that helps 在第一个for循环下方定义时将total_weight从0更改为1之前,如果有帮助

def blur(pixels, radius, width, height):
    for i in range(len(pixels)):
        total_weight = 1
        pix_weight = 0
        r_temp = radius
        pixels_2 = copy.deepcopy(pixels)
        x = i % width
        y = (i // width) + 1
        for j in range(len(pixels)):
            x_2 = j % width
            y_2 = (j // width) + 1
            dist = math.sqrt(((x_2 - x) ** 2) + ((y_2 - y) ** 2))
            while(r_temp > 0):
                if((x_2 - radius == x or x_2 + radius == x) and (y_2 - radius == y or y_2 + radius == y) and dist != 0):
                    weight = ((math.e ** (-((dist ** 2) / (2 * (radius ** 2))))) / (2 * math.pi * (radius ** 2)))
                    total_weight = total_weight + weight
                    weight_r = pixels[j][0] * weight
                    weight_g = pixels[j][1] * weight
                    weight_b = pixels[j][2] * weight
                    pix_weight = pix_weight + weight_r + weight_g + weight_b
                r_temp = r_temp - 1
        final_blur = int((pix_weight / total_weight) / 255)
        pixels_2[i][0] = int(pixels[i][0] * final_blur)
        pixels_2[i][1] = int(pixels[i][1] * final_blur)
        pixels_2[i][2] = int(pixels[i][2] * final_blur)
    return pixels_2

Method above, pixels is a 2d array, radius, width, and height are all integers. 上面的方法,像素是2d数组,半径,宽度和高度都是整数。

I've copied your code exactly, using Python 2.7.11 with some example arguments below, and I cannot reproduce an infinite loop. 我已经使用Python 2.7.11和下面的一些示例参数精确地复制了您的代码,但无法重现无限循环。 For which arguments are you seeing infinite loop behavior? 您看到哪些参数的无限循环行为?

In [13]: p = [[1, 1, 1, 1, 1], 
              [1, 1, 1, 1, 1], 
              [1, 1, 1, 1 ,1], 
              [1, 1, 1, 1, 1], 
              [1, 1, 1, 1, 1]]

In [14]: r = 3

In [15]: w, h = 5, 5

In [16]: blur(p, r, w, h)
Out[16]: 
[[1, 1, 1, 1, 1],
 [1, 1, 1, 1, 1],
 [1, 1, 1, 1, 1],
 [1, 1, 1, 1, 1],
 [0, 0, 0, 1, 1]]

(On a separate note, it looks like your blur kernel is not treating the different image boundaries with the same edge conditions). (单独说来,您的模糊内核似乎没有以相同的边缘条件对待不同的图像边界)。

Added you mentioned in the comments you are using a 256x256 image -- and also just to check on the format I tried making an example with 256 ** 2 entries (list of all pixels), where each pixel is a 3-element list. 加上您在注释中提到的您正在使用256x256图像-并且还只是为了检查我尝试使用256 ** 2项(所有像素的列表)创建示例的格式,其中每个像素是一个3元素的列表。 I see now this is probably what you were going for. 我现在知道这可能是您想要的。

Those changes are in this example: 这些更改在此示例中:

In [19]: p = [[1, 1, 1] for _ in range(256 ** 2)]

In [20]: %timeit blur(p, 1, 256, 256)

This indeed is taking a very long time to run, I'll update with time info when it's finished. 这确实需要花费很长时间才能运行,完成后我将使用时间信息进行更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM