Python bug只会出现在使用Numpy的浮雕图像上

时间:2013-11-18 01:35:39

标签: python image-processing numpy ppm emboss

该程序的目的是拍摄ppm图像并对其进行浮雕。 (可以找到整个项目的详细信息here)我正在帮助分配作业,似乎无法找到学生的错误。

我使用的原始图像如下所示: enter image description here

结果应如下所示:

enter image description here

以下是整个程序(围绕问题行的评论):

# making an image embossed 

import numpy 
def clamp(color):
    if color<0:
        return 0
    elif color>255:
        return 255
    else:
        return color
def get_num(jen):
    variable = ''
    ch = gwen.read(1)
    while ch.startswith('#'):
        while ch!='\n':

            ch=gwen.read(1)
        while ch.isspace():
            ch = gwen.read(1)
    while ch.isspace():
        ch = gwen.read(1)
    while ch.isdigit():

        variable = variable + ch
        ch=gwen.read(1)
    if ch.startswith('#'):
        while ch!='\n':

            ch=gwen.read(1)

    return int(variable)

def emboss(x,y):
    d=numpy.empty((h,w*3),numpy.uint8)
    print "len x[0]=",len(x[0])
    print "len y=", len(y)
    print "len y[0]=", len(y[0])
    for i in xrange(len(x)):
        for j in xrange(0,len(x[0]),3):
            for k in xrange(3): #r,g,b loop
                #if the next line is used a correct (but not embosed) image results
                #d[i][j+k] = x[i][j+k]
                sum = 0
                for l in xrange(0,3,1):
                    for m in xrange(0,3,1):
                        #the next line embosses but causes a triple image in the process
                        sum = sum + ((x[(i+(l-1))%h][((j+k)+((m-1)*3))%w]) * y[l][m])

                #the line below adjusts an embossed images brightness
                #if not embossing comment out this line
                d[i][j+k]=clamp(sum+127)


    return d


name=raw_input('Please enter input name: ')
output= raw_input('Please enter output name: ')
gwen=open(name,"rb")

ch=gwen.read(1)
if ch=='P':
    print ('This is P')
else:
    print('Error in Header')
ch=gwen.read(1)
if ch=='6':
    print ('This is 6')
else:
    print('Error in Header')
jen=''

w=get_num(jen)
w=int(w)
print w 
h=get_num(jen)
h=int(h)
print h
value=get_num(jen)
value=int(value)
print value

joe=open(output,"wb")
joe.write('P6'+' '+str(w)+' '+str(h)+' '+str(value)+'\n')



a=numpy.fromfile(gwen,numpy.uint8, w*h*3,'')
c=numpy.reshape(a,(h,w*3))
d=numpy.array([[1,1,1],[0,0,0],[-1,-1,-1]])
new=emboss(c,d)

for i in xrange(h):
    for j in xrange(0,w*3,3):
        r_value = new[i][j]
        r=int(clamp(r_value))
        g_value = new[i][j+1]
        g=int(clamp(g_value)) 
        b_value = new[i][j+2]
        b=int(clamp(b_value))
        joe.write('%c%c%c'%(r,g,b))

gwen.close()
joe.close()

我觉得问题出在浮雕方法中,但似乎无法修复它。因此,我将其全部包含在过滤掉ppm标题注释的部分中。

就像现在一样,它会浮雕,但会这样做三重图像。 当压花线被移除时,三重图像会消失。

如果您想亲自尝试,我正在测试file

有什么建议我应该更改以修复错误?

2 个答案:

答案 0 :(得分:4)

这是一个更清洁版的浮雕功能

# renamed
# x -> im (the input image numpy array)
# y -> kernel (the emboss kernel)
# i -> y (the y coordinate)
# j -> x (the x coordinate)
# d -> output (the output numpy array)
# k -> color (the number of the color channel 0-2)
# sum -> sum_ (sum is a built-in, so we shouldn't use that name)
def emboss(im,kernel):
    output=numpy.empty((h,w*3),numpy.uint8)
    print "len im[0]=",len(im[0])
    print "len kernel=", len(kernel)
    print "len kernel[0]=", len(kernel[0])
    for y in xrange(len(im)):
        for x in xrange(0,len(im[0]),3):
            for color in xrange(3): #r,g,b loop
                #if the next line is used a correct (but not embosed) image results
                #output[y][x+color] = im[y][x+color]
                sum_ = 0
                for l in xrange(0,3,1):
                    for m in xrange(0,3,1):
                        #the next line embosses but causes a triple image in the process
                        sum_ += (im[(y+(l-1))%h][((x+color)+((m-1)*3))%w]) * kernel[l][m]

                #the line below adjusts an embossed images brightness
                #if not embossing comment out this line
                output[y][x+color]=clamp(sum_+127)
    return output

该错误似乎就在这一行

sum_ += (im[(y+(l-1))%h][((x+color)+((m-1)*3))%w]) * kernel[l][m]

x-coord modw编辑的位置(图像宽度,以像素为单位)。 x-coord在0-1920之间变化(由于3通道颜色),而图像宽度仅为640px。 mod(w * 3)应该纠正问题。

以下是原始代码的修复:

sum = sum + ((x[(i+(l-1))%h][((j+k)+((m-1)*3))%(w*3)]) * y[l][m])

答案 1 :(得分:3)

代码需要很多改进,但对于错误:

sum = sum + ((x[(i+(l-1))%h][((j+k)+((m-1)*3))%(w*3)]) * y[l][m]) # %(w*3)