作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 OpenCV 中处理一些图像。具体来说,使用以下函数交换颜色 Pane 。
def green_ble_swap(image)
im_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
im_copy = np.copy(im_rgb)
blue = im_copy[:,:,2].copy()
green = im_copy[:,:,1].copy()
im_copy[:,:,2] = green
im_copy[:,:,1] = blue
return im_copy
> Unsupported depth of input image:
> 'VDepth::contains(depth)'
> where
> 'depth' is 4 (CV_32S)
最佳答案
您遇到错误是因为您试图对 4 channel 图像执行 3 channel 操作。具体来说,错误来自尝试转换 BGR
图片发送至 RGB
当输入图像具有透明 channel 时。正确的方法是做 cv2.COLOR_BGRA2RGB
而不是 cv2.COLOR_BGR2RGB
.您可以使用 cv2.split()
就地交换蓝色和绿色 channel 获取 BGR
channel (用于 3 channel 图像)和 BGRA
channel (4 channel 图像)然后使用 Numpy 索引交换 channel 。您还需要使用 cv2.IMREAD_UNCHANGED
加载图像时标记或 alpha channel 将被删除。例子:
输入 ->
输出
import cv2
import numpy as np
def green_blue_swap(image):
# 3-channel image (no transparency)
if image.shape[2] == 3:
b,g,r = cv2.split(image)
image[:,:,0] = g
image[:,:,1] = b
# 4-channel image (with transparency)
elif image.shape[2] == 4:
b,g,r,a = cv2.split(image)
image[:,:,0] = g
image[:,:,1] = b
return image
# Load image
image = cv2.imread('1.png', cv2.IMREAD_UNCHANGED)
cv2.imshow('image', image)
# Swap channels
swapped = green_blue_swap(image)
cv2.imshow('swapped', swapped)
cv2.waitKey()
关于python - 不支持的输入图像深度 : 'VDepth::contains(depth)' where 'depth' is 4 (CV_32S),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59804225/
我正在尝试在 OpenCV 中处理一些图像。具体来说,使用以下函数交换颜色 Pane 。 def green_ble_swap(image) im_rgb = cv2.cvtColor(ima
Traceback (most recent call last): File "demo.py", line 132, in `result = find_strawberry(image
我是一名优秀的程序员,十分优秀!