如何在 python 中连接输入彩色图像和梯度幅度

how to concatenate input color image and gradient magnitude in python

concatenated image with gray and gradient magnitude

gradient magnitude image

input color image

我无法连接输入彩色图像和梯度大小

input = cv2.imread('figures/output.png')# 3 channel
input.astype(np.float32)
gray = cv2.cvtColor(input, cv2.COLOR_BGR2GRAY)
dx = cv2.Sobel(gray, cv2.CV_32F, 1, 0)  # float 
dy = cv2.Sobel(gray, cv2.CV_32F, 0, 1)

mag = cv2.magnitude(dx, dy)  # gradient magnitude , no channel
mag = np.clip(mag, 0, 255).astype(np.uint8)  # 255보다 커질 수 있으므로 saturate 연산

cv2.imshow('magnitude', mag)

addv = cv2.vconcat((input, mag)) # error 

我收到一个 addv 部分的错误 但是 gray = cv2.cvtColor(input, cv2.COLOR_BGR2GRAY) 带有渐变幅值连接的灰色很好

cv2.error: OpenCV(4.5.1) /tmp/pip-req-build-1syr35c1/opencv/modules/core/src/matrix_operations.cpp:111: error:
 (-215:Assertion failed) src[i].dims <= 2 && src[i].cols == src[0].cols && src[i].type() == src[0].type() in function 'vconcat'

我不知道为什么

import cv2 
from skimage import io
import numpy as np

im = cv2.imread('figures/output.png')# 3 channel
im.astype(np.float32)
img = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
dx = cv2.Sobel(img, cv2.CV_32F, 1, 0)  # float 
dy = cv2.Sobel(img, cv2.CV_32F, 0, 1)

mag = cv2.magnitude(dx, dy)  # gradient magnitude , no channel
mag = np.clip(mag, 0, 255).astype(np.uint8)  # 255보다 커질 수 있으므로 saturate 연산

cv2.imshow('magnitude', mag)
mag = cv2.cvtColor(mag, cv2.COLOR_GRAY2RGB) # convert mag to RGB
addv = cv2.vconcat((im, mag))