2014-04-20 1 views
0

Visual Studio 2013 및 OpenCV 라이브러리를 사용하고 있습니다. 이산 퓨리에 변환을 표시하고 잘 작동합니다. 스펙트럼 인 디스플레이 된 이미지를 저장하고 싶지만 JPEG 품질 = 100을 사용하는 경우에도 저장된 이미지는 검정색입니다.OpenCV를 사용하여 DFT의 스펙트럼 저장

#include "opencv2/core/core.hpp" 
#include "opencv2/imgproc/imgproc.hpp" 
#include "opencv2/highgui/highgui.hpp" 
#include "opencv2/opencv.hpp" 
#include <iostream> 
#include <cmath> 

using namespace cv; 
using namespace std; 

static void help(char* progName) 
{ 
    cout << endl 
     << "This program demonstrated the use of the discrete Fourier transform     (DFT). " << endl 
     << "The dft of an image is taken and it's power spectrum is displayed." <<   endl 
     << "Usage:" << endl 
     << progName << " [image_name -- default lena.jpg] " << endl << endl; 
} 

int main(int argc, char ** argv) 
{ 
    help(argv[0]); 
    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); 
    cv::Mat gs_bgr(magI.size(), CV_8UC1); 
    cv::cvtColor(magI, gs_bgr, CV_RGB2GRAY); 
    imwrite("orig.png", gs_bgr); 
    waitKey(); 

    return 0; 
} 

답변

1

이산 푸리에 변환 (동방 박사)의 결과는 플로트 매트,하지만 당신은 단지 imwrite 이미지를 UCHAR 저장할 수 있습니다.

이미지를 [0..1]로 정규화했기 때문에 결과로 나타나는 uchar - grayscale img는 0과 1 값만 가지며 실제로는 매우 검은 색으로 보입니다.

또한 cv :: cvtColor (magI, gs_bgr, CV_RGB2GRAY)를 적용합니다. 1chan float에 img가 깨진 것 같습니다. 대신 그

, 시도 :

Mat gray; 
magI.convertTo(gray, CV_8U, 255); // upscale to [0..255] 
+0

가 잘 작동 것 감사합니다 – user3554447

관련 문제