Note that there are some explanatory texts on larger screens.

plurals
  1. POInverse filtering on OpenCV - accessing DFT values and multiplying DFT matrices
    primarykey
    data
    text
    <p>I am trying to perform an inverse and a pseudo-inverse filtering in the frequency domain.</p> <p>However I am having trouble accessing DFT coefficients and multiplying DFT matrices afterwards, since I got complex numbers and, therefore, actually two matrices...</p> <p>Basically the inverse filtering performs </p> <p>F = G/H,</p> <p>where F is the restored image, G is the blurred image and H is the kernel that blurred the image.</p> <p>The pseudo-inverse needs to access the values in H, since if the value is near 0 it should be replaced in order to avoid problems in the restoration. For this we must change the H so that:</p> <p>H(u,v) = 1/H(u,v) if H(u,v) > threshold and = 0 otherwise</p> <p>I have a kernel1 (h_1), and the images imf (restored) and img (blurred). Here is the code:</p> <pre><code> // compute the DFTs of the kernel (DFT_B) and the blurred image (DBF_A) cvDFT( dft_A, dft_A, CV_DXT_FORWARD, complexInput1-&gt;height ); cvDFT( dft_B, dft_B, CV_DXT_FORWARD, complexInput2-&gt;height ); // the first type is the inverse fitlering if (type == 1) { printf("...performing inverse filtering\n"); // dividing the transforms cvDiv(dft_A, dft_B, dft_C, 1); } // the second type is the pseudo-inverse filtering else { printf("...prepare kernel for pseudo-inverse filtering\n"); // will try to access the real values in order to see if value is above a threshold cvSplit( dft_B, image_Re1, image_Im1, 0, 0 ); // pointers to access the data into the real and imaginary matrices uchar * dRe1 = (uchar *)image_Re1-&gt;imageData; uchar * dIm1 = (uchar *)image_Im1-&gt;imageData; int width = image_Re1-&gt;width; int height = image_Re1-&gt;height; int step = image_Re1-&gt;widthStep; image_Re2 = cvCreateImage(cvGetSize(image_Re1), IPL_DEPTH_32F, 1); image_Im2 = cvCreateImage(cvGetSize(image_Im2), IPL_DEPTH_32F, 1); // pointers to access the data into the real and imaginary matrices // it will be the resulting pseudo-inverse filter uchar * dRe2 = (uchar *)image_Re2-&gt;imageData; uchar * dIm2 = (uchar *)image_Im2-&gt;imageData; printf("...building kernel for pseudo-inverse filtering\n"); for ( i = 0; i &lt; height; i++ ) { for ( j = 0; j &lt; width; j++ ) { // generate the 1/H(i,j) value if (dRe1[i * step + j] &gt; threshold) { float realsq = dRe1[i * step + j]*dRe1[i * step + j]; float imagsq = dIm1[i * step + j]*dIm1[i * step + j]; dRe2[i * step + j] = dRe1[i * step + j] / (realsq + imagsq); dIm2[i * step + j] = -1 * (dIm1[i * step + j] / (realsq + imagsq)); } else { dRe2[i * step + j] = 0; dIm2[i * step + j] = 0; } } } printf("...merging final kernel\n"); cvMerge(image_Re2, image_Im2, 0, 0, dft_B); printf("...performing pseudo-inverse filtering\n"); cvMulSpectrums(dft_A, dft_B, dft_C, 1); } printf("...performing IDFT\n"); cvDFT(dft_C, dft_H, CV_DXT_INV_SCALE, 1); printf("...getting size\n"); cvGetSubRect(dft_H, &amp;tmp3, cvRect(0, 0, img-&gt;width, img-&gt;height)); printf("......(%d, %d) - (%d, %d)\n", tmp3.cols, tmp3.rows, restored-&gt;width, restored-&gt;height); cvSplit( &amp;tmp3, image_Re1, image_Im1, 0, 0 ); cvNamedWindow("re", 0); cvShowImage("re", image_Re2); cvWaitKey(0); printf("...copying final image\n"); // error is in the line below cvCopy(image_Re1, imf, NULL); </code></pre> <p>I have an error on the last line: --- OpenCV Error: Assertion failed (src.depth() == dst.depth() &amp;&amp; src.size() == dst.size()) in cvCopy, file /build/buildd/opencv-2.1.0/src/cxcore/cxcopy.cpp, line 466</p> <p>I know it have to do with the size or depth but I don't know how to control. Anyway, I tried to show the image_Re1 and it is empty...</p> <p>Can anyone shed some light on it?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload