OpenCV: split()函数的数据类型断言失败

OpenCV: Data type assertion fail with split() function

本文关键字:数据类型 断言 失败 函数 split OpenCV      更新时间:2023-10-16

我试图遵循这里的离散傅里叶变换(dft)的例子:http://docs.opencv.org/doc/tutorials/core/discrete_fourier_transform/discrete_fourier_transform.html

我在Windows 8的Visual Studio 2013 Express上运行2.4.8。

我已经修改了这个例子,以便我使用从我的网络摄像头捕获的彩色图像(加载到Mat变量中)而不是加载灰度图像。

当我运行上面的例子时,我得到以下错误:

"断言失败Tp>::channels == m.s channels()) in简历::垫::操作符"

和下一行的换行符:

Mat planes[] = { Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F) };

环顾四周,我发现这是在类型之间转换的旧方法,所以我添加了这些行来将所有内容转换为CV_32F:

padded.convertTo(padded32, CV_32F);
Mat planes[] = { padded32, Mat::zeros(padded32.size(), CV_32F) };

现在的问题是,我得到了另一个断言失败几行以下:

split(complexI, planes); 

错误是:

"断言失败(Type == CV_32FC1 || Type == CV_32FC2 ||…| |类型== CV_64FC2) in cv::dft"

所以现在看起来它不喜欢CV_32F数据类型。我试图使数据类型CV_32FC1,但它有同样的错误。我怀疑这与dft()函数的输出数据类型complexI有关,但我不确定该怎么做。这也可能与我输入的通道数量有关(3通道彩色vs 1通道灰度图像)。

谢谢你的帮助。

链接示例中的完整代码:

#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
int main(int argc, char ** argv)
{
    const char* filename = argc >=2 ? argv[1] : "lena.jpg";
    Mat I = imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
    if( I.empty())
        return -1;
    Mat padded;                            //expand input image to optimal size
    int m = getOptimalDFTSize( I.rows );
    int n = getOptimalDFTSize( I.cols ); // on the border add zero values
    copyMakeBorder(I, padded, 0, m - I.rows, 0, n - I.cols, BORDER_CONSTANT, Scalar::all(0));
    Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)};
    Mat complexI;
    merge(planes, 2, complexI);         // Add to the expanded another plane with zeros
    dft(complexI, complexI);            // this way the result may fit in the source matrix
    // compute the magnitude and switch to logarithmic scale
    // => log(1 + sqrt(Re(DFT(I))^2 + Im(DFT(I))^2))
    split(complexI, planes);                   // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))
    magnitude(planes[0], planes[1], planes[0]);// planes[0] = magnitude
    Mat magI = planes[0];
    magI += Scalar::all(1);                    // switch to logarithmic scale
    log(magI, magI);
    // crop the spectrum, if it has an odd number of rows or columns
    magI = magI(Rect(0, 0, magI.cols & -2, magI.rows & -2));
    // rearrange the quadrants of Fourier image  so that the origin is at the image center
    int cx = magI.cols/2;
    int cy = magI.rows/2;
    Mat q0(magI, Rect(0, 0, cx, cy));   // Top-Left - Create a ROI per quadrant
    Mat q1(magI, Rect(cx, 0, cx, cy));  // Top-Right
    Mat q2(magI, Rect(0, cy, cx, cy));  // Bottom-Left
    Mat q3(magI, Rect(cx, cy, cx, cy)); // Bottom-Right
    Mat tmp;                           // swap quadrants (Top-Left with Bottom-Right)
    q0.copyTo(tmp);
    q3.copyTo(q0);
    tmp.copyTo(q3);
    q1.copyTo(tmp);                    // swap quadrant (Top-Right with Bottom-Left)
    q2.copyTo(q1);
    tmp.copyTo(q2);
    normalize(magI, magI, 0, 1, CV_MINMAX); // Transform the matrix with float values into a
                                            // viewable image form (float between values 0 and 1).
    imshow("Input Image"       , I   );    // Show the result
    imshow("spectrum magnitude", magI);
    waitKey();
    return 0;
}

不能在超过2个通道的虚拟机上使用dft

即使图像有两个通道,第二个通道也会被解释为复数的虚部,所以这可能不是你想要的。

所以你有2个选择:要么转换彩色图像,你得到从你的网络摄像头到一个单一的通道图像,如灰度图像,或应用dft为每个通道独立。

你可以看看混合通道或分割,它们都可以从你的图像中提取单个通道,然后对每个通道应用dft,