簡體   English   中英

OpenCV C ++,難以理解DFT的入門代碼

[英]OpenCV C++, trouble understanding starter code for DFT

不太了解為什么此代碼有效:

cv::Mat img = cv::imread('pic.jpg', -1);
cv::Mat padded;                            
std::uint16_t m = cv::getOptimalDFTSize(img.rows);  // This will be 256
std::uint16_t n = cv::getOptimalDFTSize(img.cols);  // This will be 256
cv::copyMakeBorder(img, padded, 0, m - img.rows, 0, n - img.cols, 
cv::BORDER_CONSTANT, cv::Scalar::all(0));           // With my inputs, this effectively just copies img into padded

cv::Mat planes[] = { cv::Mat_<float>(padded),cv:: Mat::zeros(padded.size(), CV_32F) };

cv::Mat dft_img;  
cv::merge(planes, 2, dft_img);         
cv::dft(dft_img, dft_img);

cv::split(dft_img, planes);                   

但這打破了內存中的異常:

cv::Mat img = cv::imread('pic.jpg', -1);    // I know this image is 256x256
cv::Mat dft_img = cv::Mat::zeros(256,256,CV_32F);  // Hard coding for simplicity atm
cv::dft(img,dft_img);

我在理解dft() https://docs.opencv.org/2.4/modules/core/doc/operations_on_arrays.html#dft以及其他與此相關的函數和類的文檔時遇到了麻煩。

我認為這與dft_img不是第二段中的多通道數組有關,但是我不知道如何初始化這樣的數組而不需要復制第一段代碼。

其次,當嘗試訪問planes [0]或planes [1]並使用以下命令修改其值時:

planes[0].at<double>(indexi,indexj) = 0;

我在內存中遇到了另一個異常,盡管我還看到一個新頁面,顯示未找到mat.inl.hpp。 使用Visual Studio,OpenCV 3.4.3(這是C ++的新手,但具有信號處理的中級功能),可以提供任何幫助。

您沒有指定遇到的異常,但是重要的一點是dft函數的輸入必須是浮點數,可以是32位或64位浮點數。 另一點是,如果您不熟悉c ++,請嘗試不要使用原始數組。 我什至建議如果使用c ++不是必需的,則將python用於OpenCV。 這是一個工作示例dft代碼:

//  read your image
cv::Mat img = cv::imread("a2.jpg", CV_LOAD_IMAGE_GRAYSCALE);    // I know this image is 256x256

//  convert it to floating point
//normalization is optional(depends on library and you I guess?)
cv::Mat floatImage;
img.convertTo(floatImage, CV_32FC1, 1.0/255.0);

//  create a placeholder Mat variable to hold output of dft
std::vector<cv::Mat> dftOutputs;
dftOutputs.push_back(floatImage);
dftOutputs.push_back(cv::Mat::zeros(floatImage.size(), CV_32F));
cv::Mat dftOutput;
cv::merge(dftOutputs, dftOutput);

//  perform dft
cv::dft(dftOutput, dftOutput);

//  separate real and complex outputs back
cv::split(dftOutput, dftOutputs);

我對教程中的代碼做了一些改動,以使其更易於理解。 如果要獲取幅值圖像等,可以在分割功能之后按照教程進行操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM