2014-03-28 32 views
0

我試圖遵循謹慎傅立葉變換(DFT)這裏的例子: http://docs.opencv.org/doc/tutorials/core/discrete_fourier_transform/discrete_fourier_transform.htmlOpenCV的:數據類型斷言失敗,分裂()函數

我快遞在Windows上的Visual Studio 2013上運行2.4.8 8.

我修改的示例,使得代替加載灰度圖像我使用從我的攝像頭拍攝到的彩色圖像(裝入墊變量)。

當我運行上面的例子中,我得到以下錯誤:

"Assertion Failed Tp>::channels == m.channels()) in cv::Mat::operator"

,並在以下行一休:

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); 

的錯誤是:

"Assertion Failed (Type == CV_32FC1 || Type == CV_32FC2 || ... || Type == CV_64FC2) in cv::dft"

所以現在看起來它不喜歡的CV_32F數據類型。我試着讓數據類型CV_32FC1,但它有相同的錯誤。我懷疑它與dft()函數的complexI的輸出數據類型有關,但我不知道該怎麼做。它也可能與我輸入的通道數量有關(3通道顏色與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; 
} 

回答

0

不能上使用DFT想象,有超過2 channels

即使圖像有2個通道,第二個被解釋爲複數的虛部因此這可能不是你想要的要多。

所以,你有2個選擇:要麼轉換,從您的網絡攝像頭獲取到單通道圖像的彩色圖像,如灰度圖像,或獨立應用的DFT每個通道。

你可以看看在mix channelssplit,他們都可以提取從圖像中單獨的通道,然後在他們每個人的應用的DFT,

+0

謝謝你,我不知道mixChannels沒有。我會嘗試將它們分離出來,然後將它們分開 – user1626589