Search code examples
pythonconcatenationgradientmagnitude

how to concatenate input color image and gradient magnitude in python


concatenated image with gray and gradient magnitude

gradient magnitude image

input color image

i cant concatenate input color image and gradient magnitude

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 

i get an error that addv part but gray = cv2.cvtColor(input, cv2.COLOR_BGR2GRAY) gray with gradient magnitud concatenating is fine

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'

i dont know why


Solution

  • 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))