简体   繁体   English

带有IMREAD_COLOR的图像中不使用OpenCV,DFT函数

[英]OpenCV, DFT function don't use in the image with IMREAD_COLOR

While reading the image with IMREAD_COLOR, 'dft' function throws the error: 使用IMREAD_COLOR读取图像时,“ dft”函数将引发错误: 错误

DFT function works just fine when reading an image with IMREAD_GRAYSCALE. 使用IMREAD_GRAYSCALE读取图像时,DFT功能可以正常工作。 But I want to read the image with IMREAD_COLOR. 但是我想用IMREAD_COLOR读取图像。

main function 主功能

const char* filename = "face.jpg";
Mat I = imread(filename, IMREAD_COLOR);
if(I.empty()) return 0;
Mat padded;

I.convertTo(padded, CV_32F);

Mat fft;
Mat planes[2];
dft(padded, fft, DFT_SCALE|DFT_COMPLEX_OUTPUT);

Mat fftBlur = fft.clone();

fftBlur *= 0.5;

split(fftBlur, planes);

Mat ph, mag;

mag.zeros(planes[0].rows, planes[0].cols, CV_32F);
ph.zeros(planes[0].rows, planes[0].cols, CV_32F);
cartToPolar(planes[0], planes[1], mag, ph);

merge(planes, 2, fftBlur);

//inverse
Mat invfft;

dft(fftBlur, invfft, DFT_INVERSE|DFT_REAL_OUTPUT);

Mat result;

invfft.convertTo(result, CV_8U);
Mat image;
cvtColor(result, image, COLOR_GRAY2RGB);

imshow("Output", result);
imshow("Image", image);


waitKey();

From Learning OpenCV (about dft function): 学习OpenCV (关于dft函数):

The input array must be of floating-point type and may be single- or double-channel . 输入数组必须是浮点类型,并且可以是单通道或双通道 In the single-channel case, the entries are assumed to be real numbers, and the output will be packed in a special space-saving format called complex conjugate symmetrical. 在单通道情况下,假定条目为实数,并且输出将以一种特殊的节省空间的格式打包,称为复杂共轭对称。

The same question is mentioned here in terms of matlab image processing. 关于matlab图像处理, 这里提到相同的问题。 You can check out cv::split function if you want to separate channels of your initial image. 如果您想分离初始图像的通道,则可以签出cv :: split函数。

The message you receive is an assertion it tells you DFT function only takes single precision floating point image with one or two channels (CV_32FC1, CV_32FC2, the letter C at the end of the flag mean channel) or double precision floating point images with one or two channels (CV_64FC1, CV_64FC2). 您收到的消息是一个断言,告诉您DFT函数仅拍摄具有一个或两个通道(CV_32FC1,CV_32FC2,标志平均通道末尾的字母C)的单精度浮点图像或具有一个或两个通道的双精度浮点图像两个通道(CV_64FC1,CV_64FC2)。 The two channel case is actually the representation of complex image in OpenCV data storage. 实际上,两通道情况是OpenCV数据存储中复杂图像的表示。 If you want you can split you image to std::vector<cv::Mat> where each element does represent one channel, using cv::split apply the DFT on each channels do the processing you want on it and recreate an multichannel image thanks to cv::merge . 如果需要,可以将图像拆分为std::vector<cv::Mat> ,其中每个元素确实代表一个通道,使用cv::split在每个通道上应用DFT对其进行所需的处理,然后重新创建多通道图像感谢cv::merge

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

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