函数cvtC中scn==3 | | scn==4

2024-06-01 07:08:24 发布

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

我试着训练一个神经网络模型,这是图像中心代码的一部分,问题是当我运行这段代码时-

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`

有什么办法解决这个问题吗? 提前谢谢


Tags: pathinimageimgsizenpcolcv2