Converting an OpenGL Texture buffer to an OpenCV GpuMat

Hi there,
This is my first post, so apologies if I don’t provide all the right information first time round.

It is worth mentioning I have never used CUDA or OpenGL before so this is all new to me.

My high level issue is I am trying to take an OpenGL image frame and convert it into an OpenCV GpuMat for simple image processing. I then want to move this image back to an OpenGL format for display.

The OpenGL texture is provided to me.
It was defined as below

GLuint mCaptureTexture;

glGenTextures(1, &mCaptureTexture);
glBindTexture(GL_TEXTURE_2D, mCaptureTexture);

As I need to keep the data in the GPU I found that the CUDA graphics interoperability functions should provide me the ability to do this. I am just struggling to understand how to do it.

Below is the process I was following:

cudaGraphicsResource_t texRes;
cudaGraphicsGLRegisterImage(&texRes, mCaptureTexture, GL_TEXTURE_2D, cudaGraphicsRegisterFlagsNone);

cudaGraphicsMapResources(1, &texRes, 0);

cudaArray* texArray;
cudaGraphicsSubResourceGetMappedArray(&texArray, texRes, 0, 0);

// Note: I am assuming that cudaGraphicsSubResourceGetMappedArray is the correct function to use. If not please advise.

Here is where I have been trying to get it into a GpuMat.
Suggestions on the web seem to indicate trying to use cudaMemcpyToArray or a another function.

cudaGraphicsUnmapResources(1, &texRes);

If someone could provide a simple example of a few lines on how to take the cudaArray and get it into a GpuMat, and also then how to get it back into OpenGL texture that would be great.

Thanks in advance.