Ask Your Question

Complex number matrix in OpenCV (Java)

asked 2018-03-01 13:10:51 -0500

oralb gravatar image

updated 2018-03-02 10:02:11 -0500


I am working on image data and the end goal is to use fourier transform on a product of the data and some complex matrix.

  • CV_64FC1 1000x1000 matrix of grayscaled image data (I have this)

  • Secondary 1000x1000 matrix of complex numbers (I don't have this)

The first matrix will multiply with the second complex matrix and after that I will use a fourier transform on it

Is there a way of doing this in OpenCV?


// image matrix
Bitmap bitmap_image = ...
Mat matrix = new Mat(1000, 1000, CvType.CV_64FC1);
Utils.bitmapToMat(bitmap_image, matrix); 
Imgproc.cvtColor(matrix, matrix, Imgproc.COLOR_RGBA2GRAY);
matrix.convertTo(matrix, CvType.CV_64FC1, 1.0/255.0); // type when initialised is ignored
// secondary matrix
Mat secondary_matrix = new Mat(1000, 1000, CvType.CV_64FC1);
for (int i=0; i<1000; i++) {
    for (int j=0; j<1000; j++) {
        // ... do some stuff to get value
        secondary matrix.put(i,j,value);
// now need to make secondary matrix complex by multiplying by some function of i


Context: I have a hologram image in OpenCV matrix form (single channel), I have another matrix filled with real numbers (single channel) - this matrix represents the sampling period in the spatial domain. This latter matrix isn't used directly, it passes through an e^(i*...) function to make the values complex. Finally, a discrete fourier transform is applied on the per-element product of the image data and this complex matrix I just described.

edit retag flag offensive close merge delete


please show your coding attempt !

berak gravatar imageberak ( 2018-03-01 13:13:10 -0500 )edit

@berak, silly of me to forget to show it - i've updated it now. The secondary matrix will be made complex, then multiplied with the first matrix, and then I will use OpenCV DFT

oralb gravatar imageoralb ( 2018-03-01 13:51:56 -0500 )edit

"according to the Apache Commons complex class" -- forget that. you'll have to do with opencv means, not apache, maybe it's just CvType.CV_64FC2 ?

still it's unclear, why you're trying to multiply a 1 channel Mat with a 2 channel one. context, again ? (why are you trying to do this, and what outcome do you expect ?)

berak gravatar imageberak ( 2018-03-01 14:00:36 -0500 )edit

@berak, Where do you see the 2 channel matrix? I am converting code from MATLAB to Java, in MATLAB it's really easy to have a matrix for the image, a matrix of complex numbers, and then I multiply them together using dot product and then pass them to the 2D fourier transform function. My outcome is to take a hologram and apply fresnel reconstruction to it, I could add more context if you like but I think it's not necessary to understand the issue

oralb gravatar imageoralb ( 2018-03-01 14:37:25 -0500 )edit

" Where do you see the 2 channel matrix?" -- in your question. a complex Mat should be mapped to either CV_64FC2 or CV_32FC2 in opencv

"I think it's not necessary to understand the issue" -- it is ! basically, you're asking - how would anyone else write this code, given X constraints, so you need to be explicit about the latter.

berak gravatar imageberak ( 2018-03-01 14:45:44 -0500 )edit

@berak, So I need 2 channels for the imaginary and real part each. How would I do something like this then:

[1 2 
3 4]
[2+2i 1
1 2+2i]

How would I multiply those two matrices then? Should the first one have its single channel duplicated so it has two channels similar to the second matrix?

oralb gravatar imageoralb ( 2018-03-01 14:55:19 -0500 )edit

you cannot multiply a 1 channel Mat with a 2 channel one. again, context ?

(also, careful, matlab has seperate planes for channels, while opencv has it interleaved. porting code from there is always daunting)

it probably helps, if you can give a more mathsy (mathjax?) receipe, and abstract away from terrible matlab code

also, is this a per-element or a matrix multiplication ? (be concise, and don't expect any silly matlab knowledge here, it's far beyond the scope of this)

berak gravatar imageberak ( 2018-03-01 14:58:39 -0500 )edit

@berak, I don't know what more context you want... I am trying to perform a hologram reconstruction using fresnel algorithms: Image (1) ->Real Matrix, predicted diffraction of light (2) ->Real Matrix, Fresnel algorithm (3): e^(-ipi/λz) * (2) -> complex(2) ... reconstruct -> fourier transform of (1)(3)

oralb gravatar imageoralb ( 2018-03-01 15:09:33 -0500 )edit

per-element, MATLAB might be silly but at least it works ;)

oralb gravatar imageoralb ( 2018-03-01 15:10:25 -0500 )edit

(2) is the interesting part. show, how you do this

after that, it'll be probably like dft(src,dst,COMPLEX_INPUT); . we'll also have to discuss, where the "center" of it is.

(again, matlab != opencv)

(and again, the code you show , does not reflect any of it)

berak gravatar imageberak ( 2018-03-01 15:16:18 -0500 )edit

1 answer

Sort by » oldest newest most voted

answered 2018-03-04 14:09:42 -0500

oralb gravatar image

Essentially the answer to creating a complex matrix in OpenCV is to use 2 channels. In a 2 channel matrix, separate the real component and imaginary component of a number into a channel each.

So if we want e^(i*x) here, we know that is cos(x)+isin(x) from Euler's formula. So real part of the complex is cos(x) and imaginary is sin(x) (take care to account for all constants to ix in the cos(x) part). Can Core.merge if you use two matrices for real and imaginary.

As for something like an image matrix that has no imaginary component, we know that x+0i is still x so we can have one matrix for the image real data and another matrix filled with zeroes (Mat.zeros(...)) as the imaginary component and then merge same as above.

Thanks to @berak for the pointers that helped me work it out

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2018-03-01 13:10:51 -0500

Seen: 768 times

Last updated: Mar 04 '18