1

I am taking a basic CS class and in it we have a project where we have to write a code for 2D convolution in python. I have placed the code I have written below:

def Convolve2D(image1, K, image2):
    #iterate over all rows (ignore 1-pixel borders) 
    for v in range(1, image2.getHeight()-1):
        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, image2.getWidth()-1):
            # 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):                                                                                            
                    # compute output image pixel
                   image2.setPixel(u,v,graphics.color_rgb((image2.getPixel(u,v)[0] + image1.getPixel(u-1+i,v-1+j)[0]*K[i][j]),
                                    (image2.getPixel(u,v)[1] + image1.getPixel(u-1+i,v-1+j)[1]*K[i][j]), (image2.getPixel(u,v)[2]+image1.getPixel(u-1+i,v-1+j)[2]*K[i][j])))

for the last step I have to clamp and set the output image. By clamp I am being asked to use a clamp function that was previously defined as:

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

but I can't get this last step to work. Any advice would be appreciated.

5
  • 1
    Maybe I missed it. I don't see where you're calling Clamp(). Are you getting any errors? If so, please post the full traceback. Thank you. Commented Mar 25, 2012 at 0:19
  • I have been getting errors everytime I try to use Clamp. Commented Mar 25, 2012 at 0:21
  • @KirstenLuethy Posting the actual errors would be helpful Commented Mar 25, 2012 at 0:24
  • So far I have tried pix=image2.getPixel(u,v) and then Clamp(image2.setPixel(u,v,rbg_color(pix[0],pix[1],pix[2])) since that was how we were shown to set individual pixels for a function. However, I get the error bad operand type for abs():string and I have also gotten the same error but with list at the end Commented Mar 25, 2012 at 0:24
  • 1
    Then you must be passing Clamp a string, when it needs a number. Commented Mar 25, 2012 at 0:27

1 Answer 1

1

As prelic said, then you are passing Clamp a string, when it needs a number.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.