Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python错误,仅出现在使用Numpy的浮雕图像上_Python_Image Processing_Numpy_Ppm_Emboss - Fatal编程技术网

Python错误,仅出现在使用Numpy的浮雕图像上

Python错误,仅出现在使用Numpy的浮雕图像上,python,image-processing,numpy,ppm,emboss,Python,Image Processing,Numpy,Ppm,Emboss,该程序的目的是拍摄ppm图像并压印它。(可以找到整个项目的详细信息)我正在帮助评分作业,似乎找不到学生的错误 我使用的原始图像如下所示: 结果应该如下所示: 以下是整个计划(问题行周围有注释): #制作浮雕图像 进口numpy def夹(颜色): 如果颜色为255: 返回255 其他: 返回颜色 def get_num(jen): 变量=“” ch=gwen.read(1) 而ch.startswith(“#”): 而ch!='\n': ch=gwen.read(1) 而ch.isspace

该程序的目的是拍摄ppm图像并压印它。(可以找到整个项目的详细信息)我正在帮助评分作业,似乎找不到学生的错误

我使用的原始图像如下所示:

结果应该如下所示:

以下是整个计划(问题行周围有注释):

#制作浮雕图像
进口numpy
def夹(颜色):
如果颜色为255:
返回255
其他:
返回颜色
def get_num(jen):
变量=“”
ch=gwen.read(1)
而ch.startswith(“#”):
而ch!='\n':
ch=gwen.read(1)
而ch.isspace():
ch=gwen.read(1)
而ch.isspace():
ch=gwen.read(1)
而ch.isdigit():
变量=变量+ch
ch=gwen.read(1)
如果ch.startswith(“#”):
而ch!='\n':
ch=gwen.read(1)
返回int(变量)
def浮雕(x,y):
d=numpy.empty((h,w*3),numpy.uint8)
打印“len x[0]=”,len(x[0])
打印“len y=”,len(y)
打印“len y[0]=”,len(y[0])
对于x范围内的i(len(x)):
对于x范围内的j(0,len(x[0]),3):
对于x范围内的k(3):#r,g,b循环
#如果使用了下一行,则会产生正确(但未包含)的图像结果
#d[i][j+k]=x[i][j+k]
总和=0
对于X范围内的l(0,3,1):
对于X范围内的m(0,3,1):
#下一行压花,但会在过程中产生三重图像
sum=sum+((x[(i+(l-1))%h][((j+k)+((m-1)*3))%w])*y[l][m])
#下面的行调整浮雕图像的亮度
#如果没有压花,请注释这一行
d[i][j+k]=钳位(和+127)
返回d
名称=原始输入('请输入输入名称:')
输出=原始输入('请输入输出名称:')
gwen=开放(名称“rb”)
ch=gwen.read(1)
如果ch=='P':
打印('这是P')
其他:
打印('标题中有错误')
ch=gwen.read(1)
如果ch=='6':
打印('这是6')
其他:
打印('标题中有错误')
珍=“”
w=获取数量(jen)
w=int(w)
打印w
h=获取数量(jen)
h=int(h)
打印h
value=get_num(jen)
value=int(值)
打印值
joe=打开(输出,“wb”)
joe.write('P6'+''+str(w)+''+str(h)+''+str(value)+'\n'))
a=numpy.fromfile(gwen,numpy.uint8,w*h*3,,)
c=整型(a,(h,w*3))
d=numpy.array([[1,1,1],[0,0,0],-1,-1,-1]]
新=浮雕(c,d)
对于X范围内的i(h):
对于X范围内的j(0,w*3,3):
r_值=新的[i][j]
r=int(钳位(r_值))
g_值=新[i][j+1]
g=int(钳位(g_值))
b_值=新[i][j+2]
b=int(钳位(b_值))
joe.write(“%c%c%c%”(r,g,b))
关上
乔·克洛斯()
在我看来,问题在于浮雕方法,但我似乎无法解决它。因此,我包括了所有内容,甚至包括过滤掉ppm标题注释的部分

就像现在一样,它浮雕,但这样做的三重形象。 去除压花线后,三重图像消失

这是我正在测试的,如果你想自己试试的话


有什么建议我应该修改什么来修复这个错误吗?

这里有一个更干净的浮雕函数版本

# 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坐标由
w
编辑(图像宽度以像素为单位)。
x-coord
的变化范围为0-1920(由于3个通道的颜色),而图像宽度仅为640px<代码>修改ing by(w*3)应该可以解决问题

以下是原始代码的修复程序:

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

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

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