如何为切片3D numpy数组的所有元素添加值?



我的代码:

img = np.asarray(ibuffer).copy()
imgb = cv2.cvtColor(img, cv2.COLOR_BGR2Lab)
imgb[:,:,0] += bvalue
imgb = cv2.cvtColor(imgb,cv2.COLOR_LAB2BGR)
photo = Image.fromarray(imgb)
photo = resize(photo)
photo = ImageTk.PhotoImage(photo)
canvas.photo = photo
canvas.create_image(0,0,anchor="nw",image = photo)

我需要将'bvalue'变量的值添加到imgb(3D numpy数组)的第一维的所有元素。当我尝试这样做时,我得到以下错误消息:

Tkinter回调异常回溯(最近一次调用):文件"C:UsersasaruAppDataLocalProgramsPythonPython39libtkinter_init_.py",第1884行,在call返回self.func (* args)文件"e:My FilesProjectImage EditorImage Editortest.py",第134行,in bright

imgb[:,:,0] += bvalue

numpy.core._exceptions。_UFuncOutputCastingError:无法将ufunc 'add'输出从dtype('<U3')转换为dtype('uint8'),转换规则为'same_kind'>

你可以添加int到numpy.uint8。如果你将你的bvalue转换为int型(并且假设bvalue是可以转换为int型的东西),那么你的代码应该可以工作。

lab[:,:,0] += int(50.0)

上面的行用于使图像变亮。注意,因为通道是uint8类型,如果超过255就会溢出。如果你在这上面,它会绕回来,继续(255 + 11 = 10)

查看Numpy Dtypes。变量bvalue是Unicode字符串类型,变量imgb是无符号整型。

最新更新