python - 断言错误 : image must be rgb 224x224 ((224, 224, 4))

标签 python opencv computer-vision

我正在尝试运行一个开源推理代码来预测给定边界框的多边形,称为 Polygon-RNN++

我正在尝试运行我自己裁剪到 (224,224) 的图像,但我仍然遇到这样的断言错误。

--------------------------------------------------------------------------- AssertionError                            Traceback (most recent call last) <ipython-input-8-224b99f63642> in <module>
      4 image_np = skimage.transform.resize(image_np,(224,224))
      5 #image_np = np.expand_dims(image_np, axis=0)
----> 6 preds = [model.do_test(polySess, image_np, top_k) for top_k in range(_FIRST_TOP_K)]
      7 
      8 # sort predictions based on the eval score to pick the best.

<ipython-input-8-224b99f63642> in <listcomp>(.0)
      4 image_np = skimage.transform.resize(image_np,(224,224))
      5 #image_np = np.expand_dims(image_np, axis=0)
----> 6 preds = [model.do_test(polySess, image_np, top_k) for top_k in range(_FIRST_TOP_K)]
      7 
      8 # sort predictions based on the eval score to pick the best.

~/Desktop/polyrnn/src/PolygonModel.py in do_test(self, sess, input_images, first_top_k)
     61         Return polygon
     62         """
---> 63         assert input_images.shape[1:] == (224, 224, 3), 'image must be rgb 224x224 (%s)' % str(input_images.shape)
     64         pred_dict = sess.run(
     65             self._prediction(),

AssertionError: image must be rgb 224x224 ((224, 224, 4))

我的图像是普通的 RGB 图像,为什么它的形状是 (224,224,4),我怎样才能将它 reshape 为 (224,224,3)?

最佳答案

由于有四个 channel ,您的源图像是 RGBA 格式,其中最后一个 channel 是 alpha/透明 channel 。您可以对数组进行切片以隔离 RGB channel

remove_alpha_from_image = image[:,:,:3]

另一种方法是这样的

remove_alpha_from_image = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)

关于python - 断言错误 : image must be rgb 224x224 ((224, 224, 4)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56944173/

相关文章:

python - 使用 TorchScript 类作为 pytorch 模块中的成员

javascript - 从 webrtc 将网络摄像头帧传递给 python opencv

image-processing - Caffe的卷积层中bias_filler类型 "constant"的默认值是多少?

python - 在 Raspberry Pi 上使用 Python cv2 渲染图像

Python - 如果我不导入日志记录模块,日志记录会发生吗

javascript - 如何根据 js 的命名空间和有趣名称调用 django View 函数?

c++ - 使用图像像素

python - 使用OpenCV和Python从图像中去除黑点

c++ - 找到两个嘈杂的光照变体图像之间的差异(直方图对齐等)

python - 如何使用 python 在我的图像上获得 "smart sharpen"效果?