【发布时间】:2019-08-26 23:27:09
【问题描述】:
我是 python 新手。
我编写了一些代码尝试将图片与新图片混合。
我完成了,但是太浪费时间了。
所以我尝试使用 Numba 让代码在我的 gpu 上运行。 但遇到一些警告和错误
操作系统 Ubuntu 16
pyhton 3.5 版
for i in piclist:
img = cv2.imread(str(filepath)+'/'+str(i))
print (i)
b,g,r = cv2.split(img)
bcolorlist.append(b)
gcolorlist.append(g)
rcolorlist.append(r)
@jit(nopython = True)
def select(bcolorlist,gcolorlist,rcolorlist):
for i in range(len(bcolorlist)):
b = bcolorlist[i].reshape(2073600,1)
g = gcolorlist[i].reshape(2073600,1)
r = rcolorlist[i].reshape(2073600,1)
bcolorlist[i] = b
gcolorlist[i] = g
rcolorlist[i] = r
bbb = np.concatenate(bcolorlist, axis=1)
ggg = np.concatenate(gcolorlist, axis=1)
rrr = np.concatenate(rcolorlist, axis=1)
newb = []
newg = []
newr = []
for line in bbb:
newb.append(np.argmax(np.bincount(line)))
newb = np.array(newb)
for line in ggg:
newg.append(np.argmax(np.bincount(line)))
newg = np.array(newg)
for line in rrr:
newr.append(np.argmax(np.bincount(line)))
newr = np.array(newr)
return newb,newg,newr
没有@jit也能正常工作
但是@jit 会显示下面的消息
test.py:61: NumbaWarning: 编译正在回退到启用循环提升的对象模式,因为函数“选择”失败了类型推断,原因是:无效使用具有类型参数的 Function(): (reflected list(array(uint8, 2d, C) ),axis=Literalint)
现在根据建议更新代码
for f in files:
if is_img((os.path.splitext(f)[1])):
piclist.append(f)
start = time.time()
for i in piclist:
img = cv2.imread(str(filepath)+'/'+str(i))
b,g,r = cv2.split(img)
bcolorlist.append(b)
gcolorlist.append(g)
rcolorlist.append(r)
bcolorlist = array(bcolorlist)
gcolorlist = array(gcolorlist)
rcolorlist = array(rcolorlist)
@jit(nopython = True)
def select(bcolorlist,gcolorlist,rcolorlist):
r_res = np.zeros((len(bcolorlist), 2073600, 1), dtype=np.uint8)
g_res = np.zeros((len(gcolorlist), 2073600, 1), dtype=np.uint8)
b_res = np.zeros((len(rcolorlist), 2073600, 1), dtype=np.uint8)
for i in range(len(bcolorlist)):
r_res[i] = rcolorlist[i].reshape(2073600,1)
g_res[i] = gcolorlist[i].reshape(2073600,1)
b_res[i] = bcolorlist[i].reshape(2073600,1)
r_res = r_res.reshape(2073600, len(rcolorlist))
g_res = g_res.reshape(2073600, len(gcolorlist))
b_res = b_res.reshape(2073600, len(bcolorlist))
newr = np.zeros((2073600), dtype=np.uint8)
newg = np.zeros((2073600), dtype=np.uint8)
newb = np.zeros((2073600), dtype=np.uint8)
for i in range(b_res[:,0].size):
newr[i] = np.argmax(np.bincount(r_res[i,:]))
newg[i] = np.argmax(np.bincount(g_res[i,:]))
newb[i] = np.argmax(np.bincount(b_res[i,:]))
return newb, newg, newr
b, g, r = select(bcolorlist,gcolorlist,rcolorlist)
newb = b.reshape(1920,1080)
newg = g.reshape(1920,1080)
newr = r.reshape(1920,1080)
img = cv2.merge([newb, newg, newr])
end = time.time()
print (end - start)
正常工作!!!
但它不会输出图像.....
能不能麻烦你给点建议?
【问题讨论】:
-
你的输入看起来怎么样?
-
您好,我的输入如下" for i in piclist: img = cv2.imread(str(filepath)+'/'+str(i)) b,g,r = cv2.split( img) bcolorlist.append(b) gcolorlist.append(g) rcolorlist.append(r)"
-
更新我的代码,感谢您的回复。
-
哈哈好吧,我认为我的回答有点过于复杂,现在我明白了你想要做什么。将在一分钟内发布更新。