简体   繁体   English

OpenCV / C ++-将图像转换为双精度向量以进行FFT(快速傅立叶变换)

[英]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. 我正在尝试计算图像的FFT(快速傅立叶变换)以使用频率来确定图像是否模糊。

I need to use a custom FFT algorithm that we already have in our codebase. 我需要使用代码库中已有的自定义FFT算法。 The FFT algorithm requires a standard 1D vector of doubles or ints . FFT算法需要doublesints的标准一维矢量。 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. 我需要一种读取图像的方法,然后将其转换为双精度的向量,以便可以计算图像的FFT。

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, OpenCV错误:在copyTo中,断言失败(channels()== CV_MAT_CN(dtype)),

Any ideas to how I can achieve this? 关于如何实现此目标的任何想法?

CV_32F means float , not double . CV_32F表示float ,而不是double You should use CV_64F instead. 您应该改用CV_64F

You also need to specify the number of channels. 您还需要指定通道数。 This example is for 1 channel image (grayscale), and probably what you need: 本示例适用于1通道图像(灰度),可能还需要:

// 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: 对于3个频道,您可以执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM