improt Numba@jit meet警告消息,@jit(nopython=True)将sh

2024-09-30 20:17:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一条新Python。在

我写了一些代码尝试混合图片到一个新的图片。在

我做完了,但浪费了太多时间。在

所以我试着用Numba让代码在我的gpu上工作。 但是遇到了一些警告和错误

os Ubuntu 16

pyhton version 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: Compilation is falling back to object mode WITH looplifting enabled because Function "select" failed type inference due to: Invalid use of Function() with argument(s) of type(s): (reflected list(array(uint8, 2d, C)), axis=Literalint)

根据建议立即更新代码

^{pr2}$

一切正常!!!!在

但它不能生成输出图像。。。。。在

麻烦你给点建议好吗?在


Tags: 代码infornplinearrayjitappend
1条回答
网友
1楼 · 发布于 2024-09-30 20:17:14

更新—以rgb图像作为输入的工作示例。我以前没有用Python做过图像处理,所以这是一次不错的旅行。下面是一些代码。请注意,我为函数添加了一些输入参数,以使其更加通用。我还更改了一些变量名,但这只是一个口味问题,所以不用担心。在

import cv2
import numpy as np
from numba import njit

@njit
def select(b, g, r, n, w, h): # added number of input images, width and height
    r_res = r.reshape(n, w*h) # flatten the images' rgb 2d arrays to 1d
    g_res = g.reshape(n, w*h)
    b_res = b.reshape(n, w*h)
    newr = np.zeros((w*h), dtype=np.uint8)
    newg = np.zeros((w*h), dtype=np.uint8)
    newb = np.zeros((w*h), dtype=np.uint8)
    for i in range(w*h): # this is what takes time...
        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]))
    newb = newb.reshape(w,h) # reshape back from 1d to 2d
    newg = newg.reshape(w,h)
    newr = newr.reshape(w,h)
    return newb, newg, newr

imgs = ['D:/test/DSC02142.jpg', 'D:/test/DSC02606.jpg', 'D:/test/DSC01747.jpg']
bcolorlist, gcolorlist, rcolorlist = [], [], []
for i in imgs:
    b, g, r = cv2.split(cv2.imread(i))
    bcolorlist.append(b)
    gcolorlist.append(g)
    rcolorlist.append(r)

b = np.array(bcolorlist)
g = np.array(gcolorlist)
r = np.array(rcolorlist)

n, w, h = b.shape # assuming all images of equal width x height!
newb, newg, newr = select(b, g, r, n, w, h)

imgmerge = cv2.merge((newb, newg, newr))
cv2.imwrite('D:/test/output.jpg', imgmerge)

…会产生这样奇怪的东西: merged_img

相关问题 更多 >