Phorce
Phorce

Reputation: 4642

OpenCV / C++ - Convert image to vector of doubles for FFT (Fast Fourier Transform)

I'm trying to compute the FFT (Fast Fourier Transform) of an image to use the frequencies to determine whether or not the image is blurry.

I need to use a custom FFT algorithm that we already have in our codebase. The FFT algorithm requires a standard 1D vector of doubles or ints. I need a way to read in an image and then convert it to a vector of doubles so that I can compute the FFT of the image.

I have tried the following:

cv::Mat inputImage = cv::imread("testImage.png");
cv::Mat fImage; 

inputImage.convertTo(fImage, CV_32F); 
std::vector<double> actualImage = fImage.clone();

However, I am getting the error:

OpenCV Error: Assertion failed (channels() == CV_MAT_CN(dtype)) in copyTo,

Any ideas to how I can achieve this?

Upvotes: 2

Views: 1871

Answers (1)

Miki
Miki

Reputation: 41765

CV_32F means float, not double. You should use CV_64F instead.

You also need to specify the number of channels. This example is for 1 channel image (grayscale), and probably what you need:

// Load the image
cv::Mat inputImage = cv::imread("testImage.png");
// Convert to single channel (grayscale)
cv::cvtColor(inputImage, inputImage, cv::COLOR_BGR2GRAY);

// Or directly load as grayscale    
// cv::Mat inputImage = cv::imread("testImage.png", cv::IMREAD_GRAYSCALE);

// Convert to double
cv::Mat fImage; 
inputImage.convertTo(fImage, CV_64F); 

// Initialize the vector with the image content
std::vector<double> actualImage(fImage.begin<double>(), fImage.end<double>());

For 3 channels you can do:

cv::Mat inputImage = cv::imread("testImage.png");
cv::Mat fImage; 

inputImage.convertTo(fImage, CV_64F); 
fImage = fImage.reshape(1); // You need to reshape to single channel
std::vector<double> actualImage(fImage.begin<double>(), fImage.end<double>());

Upvotes: 6

Related Questions