gpt4 book ai didi

python - scn == 3 ||函数 cvtColor 中的 scn == 4

转载 作者:太空宇宙 更新时间:2023-11-03 22:46:54 27 4
gpt4 key购买 nike

我正在尝试训练神经网络模型,这是使图像居中的代码的一部分,问题是当我运行这段代码时-

def centering_image(img):
size = [256,256]

img_size = img.shape[:2]

# centering
row = (size[1] - img_size[0]) // 2
col = (size[0] - img_size[1]) // 2
resized = np.zeros(list(size) + [img.shape[2]], dtype=np.uint8)
resized[row:(row + img.shape[0]), col:(col + img.shape[1])] = img

return resized


x = []
for i, file_path in enumerate(file_paths):
#read image
img = cv2.imread(file_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

#resize
if(img.shape[0] > img.shape[1]):
tile_size = (int(img.shape[1]*256/img.shape[0]),256)
else:
tile_size = (256, int(img.shape[0]*256/img.shape[1]))

#centering
img = centering_image(cv2.resize(img, dsize=tile_size))

#out put 224*224px
img = img[16:240, 16:240]
x.append(img)

x = np.array(x)

我收到这个错误-

error                                     Traceback (most recent call 
last)
<ipython-input-11-2a14d86a9a00> in <module>()
17 #read image
18 img = cv2.imread(file_path)
---> 19 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
20
21 #resize

error: /io/opencv/modules/imgproc/src/color.cpp:9716: error: (-215)
scn == 3 || scn == 4 in function cvtColor`

有什么解决办法吗?提前致谢

最佳答案

您加载的一张图片似乎有四个 channel 。因此,从 BGR 到 RBG 的颜色转换不起作用,因为它需要三个 channel 。

您可以改用 cv2.COLOR_BGRA2BGR 标志。

错误表明带有 COLOR_BGR2RGBcvtColor 需要具有 3 或 4 个 channel 的图像,但得到了不同的东西。因此,您应该使用 print img.shape 仔细检查 channel 数。

关于python - scn == 3 ||函数 cvtColor 中的 scn == 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45622108/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com