Repository: opencv
Issue: Calling cv2.merge() on a UMat triggers a Null error. 
Labels: bug,duplicate,category: python bindings

##### System information (version)
- OpenCV => 4.5
- Operating System / Platform => Windows 64 Bit
- Compiler => Clang 11.0.0 for MSVC

##### Detailed description

Calling cv2.merge() on a UMat triggers a Null System error, calling ```.get()``` on the UMat works but incurs an unneeded penalty. If this is not how I'm supposed to merge and split channels with a UMat in python please tell me how, as all I could find was a closed stale issue and no one answered in IRC, and the single question about this on the forum from 2017 just says "no". Converting the datatype of a UMat in python without copying back to the CPU into a numpy array is also something I couldn't find documented anywhere. 

##### Steps to reproduce

```.py
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt


def crop_spectrum(mag):
    mag_rows, mag_cols = mag.shape
    # crop the spectrum, if it has an odd number of rows or columns
    magR = mag[0 : (mag_rows & -2), 0 : (mag_cols & -2)]
    cx = int(mag_rows / 2)
    cy = int(mag_cols / 2)
    q0 = mag[0:cx, 0:cy]  # Top-Left - Create a ROI per quadrant
    q1 = mag[cx : cx + cx, 0:cy]  # Top-Right
    q2 = mag[0:cx, cy : cy + cy]  # Bottom-Left
    q3 = mag[cx : cx + cx, cy : cy + cy]  # Bottom-Right
    tmp = np.copy(q0)  # swap quadrants (Top-Left with Bottom-Right)
    mag[0:cx, 0:cy] = q3
    mag[cx : cx + cx, cy : cy + cy] = tmp
    tmp = np.copy(q1)  # swap quadrant (Top-Right with Bottom-Left)
    mag[cx : cx + cx, 0:cy] = q2
    mag[0:cx, cy : cy + cy] = tmp
    return mag


def mendimeter():
    cap = cv.VideoCapture(0)

    while True:
        # Capture frame-by-frame
        ret, frame = cap.read()
        (cols, rows, channels) = frame.shape
        # upload image to the GPU.
        frame = cv.UMat(frame)

        # Our operations on the frame come here
        nrows = cv.getOptimalDFTSize(rows)
        ncols = cv.getOptimalDFTSize(cols)
        nimg = cv.resize(frame, (nrows, ncols))

        b, g, r = cv.split(nimg)

        r_padded = cv.copyMakeBorder(
            r, 0, nrows - rows, 0, ncols - cols, cv.BORDER_CONSTANT, value=[0, 0, 0]
        )

        r_local = r_padded.get()
        r_planes = cv.UMat(
            np.array(
                [
                    np.float32(r_local),
                    np.zeros(r_local.shape, np.float32),
                ]
            )
        )

        g_padded = cv.copyMakeBorder(
            g, 0, nrows - rows, 0, ncols - cols, cv.BORDER_CONSTANT, value=[0, 0, 0]
        )
        g_local = g_padded.get()
        g_planes = cv.UMat(
            np.array(
                [
                    np.float32(g_local),
                    np.zeros(g_local.shape, np.float32),
                ]
            )
        )

        b_padded = cv.copyMakeBorder(
            b, 0, nrows - rows, 0, ncols - cols, cv.BORDER_CONSTANT, value=[0, 0, 0]
        )
        b_local = b_padded.get()
        b_planes = cv.UMat(
            np.array(
                [
                    np.float32(b_local),
                    np.zeros(b_local.shape, np.float32),
                ]
            )
        )

        # Add to the expanded another plane with zeros
        complexR = cv.merge(r_planes)
        complexG = cv.merge(g_planes)
        complexB = cv.merge(b_planes)

        cv.dft(complexR, complexR)  # this way the result may fit in the source matrix
        cv.dft(complexG, complexG)  # also doing these inplace for performance.
        cv.dft(complexB, complexB)

        cv.split(
            complexR, r_planes
        )  # planes[0] = Re(DFT(Red channel of Frame), r_planes[1] = Im(DFT(channel of Frame))
        cv.split(
            complexG, g_planes
        )  # planes[0] = Re(DFT(Green channel of Frame), g_planes[1] = Im(DFT(channel of Frame))
        cv.split(
            complexB, b_planes
        )  # planes[0] = Re(DFT(Blue channel of Frame), b_planes[1] = Im(DFT(blue channel of Frame)))

        # for each channel's plane, planes[0] = magnitude
        cv.magnitude(r_planes[0], r_planes[1], r_planes[0])
        cv.magnitude(g_planes[0], g_planes[1], g_planes[0])
        cv.magnitude(b_planes[0], b_planes[1], b_planes[0])

        magR = r_planes[0]
        magG = g_planes[0]
        magB = b_planes[0]

        matOfOnes = np.ones(magR.shape, dtype=magR.dtype)

        #  switch to logarithmic scale
        cv.add(matOfOnes, magR, magR)
        cv.add(matOfOnes, magG, magG)
        cv.add(matOfOnes, magB, magB)
        cv.log(magR, magR)
        cv.log(magG, magG)
        cv.log(magB, magB)

        magR = crop_spectrum(magR)
        magG = crop_spectrum(magG)
        magB = crop_spectrum(magB)

        cv.normalize(magR, magR, 0, 1, cv.NORM_MINMAX)
        cv.normalize(magG, magG, 0, 1, cv.NORM_MINMAX)
        cv.normalize(magB, magB, 0, 1, cv.NORM_MINMAX)

        # Display the resulting frame
        cv.imshow("cropped for dft", nimg)
        cv.imshow("red channel", r)
        cv.imshow("green channel", g)
        cv.imshow("blue channel", b)
        cv.imshow("red channel dft magnitude spectrum", magR)
        cv.imshow("green channel dft magnitude spectrum", magG)
        cv.imshow("blue channel dft magnitude spectrum", magB)

        if cv.waitKey(1) & 0xFF == ord("q"):
            break

    # When everything done, release the capture
    cap.release()
    cv.destroyAllWindows()


if __name__ == "__main__":
    mendimeter()
```


##### Issue submission checklist

 - [ x] I report the issue, it's not a question
   <!--
   OpenCV team works with answers.opencv.org, Stack Overflow and other communities
   to discuss problems. Tickets with question without real issue statement will be
   closed.
   -->
 - [ x] I checked the problem with documentation, FAQ, open issues,
       answers.opencv.org, Stack Overflow, etc and have not found solution
   <!--
   Places to check:
   * OpenCV documentation: https://docs.opencv.org
   * FAQ page: https://github.com/opencv/opencv/wiki/FAQ
   * OpenCV forum: https://answers.opencv.org
   * OpenCV issue tracker: https://github.com/opencv/opencv/issues?q=is%3Aissue
   * Stack Overflow branch: https://stackoverflow.com/questions/tagged/opencv
   -->
 - [x ] I updated to latest OpenCV version and the issue is still there
   <!--
   master branch for OpenCV 4.x and 3.4 branch for OpenCV 3.x releases.
   OpenCV team supports only latest release for each branch.
   The ticket is closed, if the problem is not reproduced with modern version.
   -->
 - [x ] There is reproducer code and related data files: videos, images, onnx, etc
   <!--
   The best reproducer -- test case for OpenCV that we can add to the library.
   Recommendations for media files and binary files:
   * Try to reproduce the issue with images and videos in opencv_extra repository
     to reduce attachment size
   * Use PNG for images, if you report some CV related bug, but not image reader
     issue
   * Attach the image as archive to the ticket, if you report some reader issue.
     Image hosting services compress images and it breaks the repro code.
   * Provide ONNX file for some public model or ONNX file with with random weights,
     if you report ONNX parsing or handling issue. Architecture details diagram
     from netron tool can be very useful too. See https://lutzroeder.github.io/netron/
   -->