Fourier transformation in frequency domain with opencv - opencv

I'm trying to implement the fourier transformation in frequency domain.
I used getOptimalDFTSize accordingly, and I copied the image and mask, in bigger images, suitable for fourier transformation. I used the sample code from here as a reference.
Now, I have to separate the real and imaginary part, and to perform pixelwise multiplication of the image imaginary part with the mask imaginary part, and the same for the real part.But when I try to do so, I get the following error message:
OpenCV Error: Assertion failed (type == srcB.type() && srcA.size() == srcB.size()) in mulSpectrums, file /build/buildd/opencv-2.1.0/src/cxcore/cxdxt.cpp, line 1855
/build/buildd/opencv-2.1.0/src/cxcore/cxdxt.cpp:1855: error: (-215) type == srcB.type() && srcA.size() == srcB.size() in function mulSpectrums
The code is following:
//fourier transfromation of real and imaginary part
Mat complex_image, real_image, complex_mask, real_mask;
cv::dft(new_image, complex_image, DFT_COMPLEX_OUTPUT);
cv::dft(new_image, real_image, DFT_REAL_OUTPUT);
cv::dft(new_mask, complex_mask, DFT_COMPLEX_OUTPUT);
cv::dft(new_mask, real_mask, DFT_REAL_OUTPUT);
//pixelwise multiplication
Mat multiplied_complex, multiplied_real;
cv::mulSpectrums(complex_image, complex_mask, multiplied_complex, DFT_COMPLEX_OUTPUT );
cv::mulSpectrums(real_image, real_mask, multiplied_real, DFT_REAL_OUTPUT);
What am I doing wrong here?

Image and mask should have same size (width and height) and (most probably this is problem) type. So if it is different type you need to convert one of them so they have equal type.

Related

Homography matrix in Opencv?

In LATCH_match.cpp in opencv_3.1.0 the homography matrix is defined and used as:
Mat homography;
FileStorage fs("../data/H1to3p.xml", FileStorage::READ);
...
fs.getFirstTopLevelNode() >> homography;
...
Mat col = Mat::ones(3, 1, CV_64F);
col.at<double>(0) = matched1[i].pt.x;
col.at<double>(1) = matched1[i].pt.y;
col = homography * col;
...
Why H1to3p.xml is:
<opencv_storage><H13 type_id="opencv-matrix"><rows>3</rows><cols>3</cols><dt>d</dt><data>
7.6285898e-01 -2.9922929e-01 2.2567123e+02
3.3443473e-01 1.0143901e+00 -7.6999973e+01
3.4663091e-04 -1.4364524e-05 1.0000000e+00 </data></H13></opencv_storage>
With which criteria these numbers were chosen? They can be used for any other homography test for filtering keypoints (as in LATCH_match.cpp)?
I assume that your "LATCH_match.cpp in opencv_3.1.0" is
https://github.com/Itseez/opencv/blob/3.1.0/samples/cpp/tutorial_code/xfeatures2D/LATCH_match.cpp
In that file, you find:
// If you find this code useful, please add a reference to the following paper in your work:
// Gil Levi and Tal Hassner, "LATCH: Learned Arrangements of Three Patch Codes", arXiv preprint arXiv:1501.03719, 15 Jan. 2015
And so, looking at http://arxiv.org/pdf/1501.03719v1.pdf you will find
For each set, we compare the first image against each of the remaining
five and check for correspondences. Performance is measured using the
code from [16, 17]1 , which computes recall and 1-precision
using known ground truth homographies between the images.
I think that the image ../data/graf1.png is https://github.com/Itseez/opencv/blob/3.1.0/samples/data/graf1.png that I show here:
According to the comment Homography matrix in Opencv? by Catree the original dataset is at http://www.robots.ox.ac.uk/~vgg/research/affine/det_eval_files/graf.tar.gz where it is said that
Homographies between image pairs included.
So I think that the homography stored in file ../data/H1to3p.xml is the homography between image 1 and image 3.

FFTW fftwf_plan_r2r_2d() with FFTW_REDFT01 equivalent

I am trying to port code that uses FFTW to use KissFFT.
The code uses fftwf_plan_r2r_2d() with FFTW_REDFT01.
What would be the equivalent call in KissFFT?
If this call (with FFTW_REDFT01) is equivalent to a DCT, could I just use a direct DCT transform instead, e.g. such as OpenCV cv::dct?
Is there some input data modification I'd need to do, like reflections and symmetrizations?
Answering my own question...
With the help of these two references, I ended up not using DFT at all, but using OpenCV's cv::dct() and cv::idct() instead.
To answer the question, fftwf_plan_r2r_2d(...,FFTW_REDFT10, FFTW_REDFT10,...) can be replaced by this OpenCV code with the additional scaling:
cv::dct(img, resFFT); // fwd dct. This is like Matlab's dct2()
resFFT *= (4 * sqrt(float(img.rows/2)) * sqrt(float(img.cols/2)));
resFFT.row(0) *= sqrt(2.f);
resFFT.col(0) *= sqrt(2.f);
The inverse with FFTW_REDFT01 can be done like so:
// First re-scale the data for idct():
resFFT /= (4 * sqrt(float(img.rows/2)) * sqrt(float(img.cols/2)));
resFFT.row(0) /= sqrt(2.f);
resFFT.col(0) /= sqrt(2.f);
cv::idct(resFFT, outImg); // this will return the input exactly
// However, the transforms computed by FFTW are unnormalized, exactly like the corresponding,
// so computing a transform followed by its inverse yields the original array scaled by N, where N is the logical DFT size.
// The logical DFT size: Logical N=2*n for each axis, this is th implicit symmetrization
// of the image: reflect right and then reflect both halves down.
int logicalSizeN = (2*img.rows) * (2*img.cols);
outImg *= logicalSizeN; // scale to be like FFTW result
More helpful links here and here.
Note that OpenCV supports only images with an even number of rows and columns.

Convex hull on the mask outputted by BackgroundSubtractorMOG

I am using the BackgroundsubtractorMOG() to basically extract a mask to separate out the foreground. I am then using convexHull() on the mask to locate the position of a moving object.
But i am getting the following error:
openCV Error: Assertion failed (nelems >= 0 && (depth == CV_32F || depth == CV_32S)) in convexHull, file /home/ameya/OpenCV2.4.2/modules/imgproc/src/contours.cpp, line 1947
terminate called after throwing an instance of 'cv::Exception'
what(): /home/ameya/OpenCV2.4.2/modules/imgproc/src/contours.cpp:1947: error: (-215) nelems >= 0 && (depth == CV_32F || depth == CV_32S) in function convexHull
I have checked the no. of elements as well as type-casted the mask matrix. But the error still persists.
Has anyone encountered a similar problem before. I am using OpenCV 2.4.2
Use this format, it will help (notice typecasting to Mat):
convexhull(Mat(inputarray),hull,0,0)
are you calling convexhull on your mask image there ?
it's supposed to work with point2d(or index) vectors, eg. from findContours()

counting bright pixels and summing them. Medical Image C++

Currently, I'm working on a project in medical engineering. I have a big image with several sub-images of the cell, so my first task is to divide the image.
I thought about the next thing:
Convert the image into binary
doing a projection of the brightness pixels into the x-axis so I can see where there are gaps between brightnesses values and then divide the image.
The problem comes when I try to reach the second part. My idea is using a vector as the projection and sum all the brightnesses values all along one column, so the position number 0 of the vector is the sum of all the brightnesses values that are in the first column of the image, the same until I reach the last column, so at the end I have the projection.
This is how I have tried:
void calculo(cv::Mat &result,cv::Mat &binary){ //result=the sum,binary the imag.
int i,j;
for (i=0;i<=binary.rows;i++){
for(j=0;j<=binary.cols;j++){
cv::Scalar intensity= binaria.at<uchar>(j,i);
result.at<uchar>(i,i)=result.at<uchar>(i,i)+intensity.val[0];
}
cv::Scalar intensity2= result.at<uchar>(i,i);
cout<< "content" "\n"<< intensity2.val[0] << endl;
}
}
When executing this code, I have a violation error. Another problem is that I cannot create a matrix with one unique row, so...I don't know what could I do.
Any ideas?! Thanks!
At the end, it does not work, I need to sum all the pixels in one COLUMN. I did:
cv::Mat suma(cv::Mat& matrix){
int i;
cv::Mat output(1,matrix.cols,CV_64F);
for (i=0;i<=matrix.cols;i++){
output.at<double>(0,i)=norm(matrix.col(i),1);
}
return output;
}
but It gave me a mistake:
Assertion failed (0 <= colRange.start && colRange.start <= colRange.end && colRange.end <= m.cols) in Mat, file /home/usuario/OpenCV-2.2.0/modules/core/src/matrix.cpp, line 276
I dont know, any idea would be helpful, anyway many thanks mevatron, you really left me in the way.
If you just want the sum of the binary image, you could simply take the L1-norm. Like so:
Mat binaryVectorSum(const Mat& binary)
{
Mat output(1, binary.rows, CV_64F);
for(int i = 0; i < binary.rows; i++)
{
output.at<double>(0, i) = norm(binary.row(i), NORM_L1);
}
return output;
}
I'm at work, so I can't test it out, but that should get you close.
EDIT : Got home. Tested it. It works. :) One caveat...this function works if your binary matrix is truly binary (i.e., 0's and 1's). You may need to scale the norm output with the maximum value if the binary matrix is say 0's and 255's.
EDIT : If you don't have using namespace cv; in your .cpp file, then you'll need to declare the namespace to use NORM_L1 like this cv::NORM_L1.
Have you considered transposing the matrix before you call the function? Like this:
sumCols = binaryVectorSum(binary.t());
vs.
sumRows = binaryVectorSum(binary);
EDIT : A bug with my code :)
I changed:
Mat output(1, binary.cols, CV_64F);
to
Mat output(1, binary.rows, CV_64F);
My test case was a square matrix, so that bug didn't get found...
Hope that is helpful!

resize an image and changing its depth

I need to resize an IplImage and convert it into a CvMat of different depth, this is the code I've written so far:
void cvResize2(IplImage *imgSrc, IplImage *imgDst)
{
IplImage *imgTemp;
imgTemp = cvCreateImage( cvGetSize( imgSrc ), IPL_DEPTH_64F, 1 );
cvScale( imgSrc, imgTemp, 1/255., 0.0 );
cvResize( imgTemp, imgDst );
}
The source image is grayscale, the destination one is 64F bit deep. cvScale only scales between images of same size, hence the temp image.
The program rises the following exception when invoking cvResize:
OpenCV Error: Assertion failed (func != 0) in resize, file /tmp/buildd/opencv-2.1.0/src/cv/cvimgwarp.cpp, line 1488
terminate called after throwing an instance of 'cv::Exception'
what(): /tmp/buildd/opencv-2.1.0/src/cv/cvimgwarp.cpp:1488: error: (-215) func != 0 in function resize
I can't figure out why, I've checked that the images respect the conditions imposed
src: 512x384, 8 depth
tmp: 512x384, 64 depth
dst: 64x64, 64 depth
Any clues?
Thanks in advance
You may have found a bug. I can reproduce it on my end, too (Ubuntu 64-bit, OpenCV-2.1.0). If you use 32-bit floating point precision, it works, but crashes with 64-bit floats. My recommendation is to update your OpenCV to the most recent version and see if the problem goes away. If not, then build the library in debug mode and step through the function that is throwing the assertion. From looking at the culprit source in cvimgwarp.cpp, it looks like it's unable to find an interpolation method to use for the destination image.

Resources