1

我需要通过获取内核K并对二维数组中的值进行平均并将中心值设置为K. 这是我为此编写的代码...

def Clamp(pix):
    pix = abs(pix)
    if pix > 255:
        pix = 255
    return pix

def Convolve2D(image1, K, image2):
    img = graphics.Image(graphics.Point(0, 0), image1)
    img.save(image2)
    secondimage=graphics.Image(graphics.Point(0,0),image2)
    h = img.getHeight()
    w = img.getWidth()
    A = [[0]*h for y in range(w)]
    B = [[0]*w for x in range(h)]
    #iterate over all rows (ignore 1-pixel borders) 
    for v in range(1, h-3):
        graphics.update() # this updates the output for each row
        # for every row, iterate over all columns (again ignore 1-pixel borders)
        for u in range(1, w-3):
            #A[u][v] = 0
            #B[u][v] = 0
            # for every pixel, iterate over region of overlap between
            #   input image and 3x3 kernel centered at current pixel
            for i in range (0, 3):
                for j in range (0, 3):
                    A[u][v] = A[u][v] + B[v+i][u+j] * K[i][j]
            r, g, b = img.getPixel(u, v)
            if (r * A[u][v] >= 255):
                Clamp(r)
            else:
                r = r * A[u][v]
            if (g * A[u][v] >= 255):
                Clamp(g)
            else:
                g = g * A[u][v]
            if (b * A[u][v] >= 255):
                Clamp(b)
            else:
                b = b * A[u][v]
            newcolor = graphics.color_rgb(r, g, b)
            secondimage.setPixel(u, v , newcolor)
    print("Not yet implemented") # to be removed
    secondimage.save(image2)
    secondimage.move(secondimage.getWidth()/2, secondimage.getHeight()/2)
    win = graphics.GraphWin(secondimage, secondimage.getWidth(), secondimage.getHeight())
    secondimage.draw(win)


def Blur3(image1, image2):
    K = [[1/9, 1/9, 1/9], [1/9, 1/9, 1/9], [1/9, 1/9, 1/9]]
    return Convolve2D(image1, K, image2)

这是我试图模糊的图像

在此处输入图像描述

这就是我的代码

在此处输入图像描述

是否可能是我的 if 和 else 语句以及正在执行此操作的钳位函数?我只是想要一个模糊的图像像这样出来

在此处输入图像描述

4

2 回答 2

0

do this :

for v in range(h):
        graphics.update() # this updates the output for each row
        for u in range(w):
             for i in range (0, 3):
                for j in range (0, 3):
                    if v-i>=0 and u-j>=0 and v+i<=256 and u+j<=256  :
                        img[u][v] = img[u][v] + img[v-i][u-j] * K[i][j]

this should work!

Can you please tell me why you have two images A, B and blur image A using B? I mean :

                    A[u][v] = A[u][v] + B[v+i][u+j] * K[i][j]

Here I add a code which works for gray scale image,you can expand it for your need!

import matplotlib.image as mpimg

Img=mpimg.imread('GrayScaleImg.jpg')
kernel=towDimGuassKernel(size)

def conv2D(I,kernel):

    filterWidth=kernel.shape[0]
    half=filterWidth/2


    bluredImg=np.zeros(I.shape)
    for imgRow in range(I.shape[0]):
        print imgRow
        for imgCol in range(I.shape[1]):
            for filterRow in range(filterWidth):
                for filterCol in range(filterWidth):
                    if imgRow-filterRow>=0 and imgCol-filterCol>=0 and imgRow+filterRow<=256 and imgCol+filterCol<=256  :
                        bluredImg[imgRow,imgCol]+=I[imgRow-filterRow,imgCol-filterCol]*kernel[filterRow,filterCol]



    return bluredImg
于 2013-04-16T21:12:49.553 回答
0

您已经初始化AB清空了大小为0. 您需要将它们初始化为图像的大小,在两个维度上。

A = [[0]*w for y in range(h)]

编辑:你的第二个问题是你定义的内核1/9是一个整数除法 yield 0

于 2013-04-16T20:26:47.517 回答