Assertion failed (type == CV_32FC1 || type == CV_32FC2 || type == CV_64FC1 || type == CV_64FC2) in dft
时间: 2024-03-29 10:32:54 浏览: 180
这个错误信息是OpenCV库中的一个断言错误,意味着在进行离散傅里叶变换(DFT)时,输入图像的数据类型不符合要求。
根据错误信息,输入图像的数据类型必须是CV_32FC1、CV_32FC2、CV_64FC1或CV_64FC2中的一种。其中,CV_32FC1表示单通道的32位浮点型图像,CV_32FC2表示两通道的32位浮点型图像,CV_64FC1表示单通道的64位浮点型图像,CV_64FC2表示两通道的64位浮点型图像。
要解决这个问题,你可以检查一下你的输入图像的数据类型是否符合要求。如果不符合,你可以尝试将图像转换为正确的数据类型后再进行DFT操作。
相关问题
terminate called after throwing an instance of 'cv::Exception' what(): OpenCV(4.5.4) /home/cicv/Downloads/opencv-4.5.4/modules/core/src/copy.cpp:1026: error: (-215:Assertion failed) top >= 0 && bottom >= 0 && left >= 0 && right >= 0 && _src.dims() <= 2 in function 'copyMakeBorder'
这个错误是由于 `copyMakeBorder` 函数的参数不正确导致的。根据错误信息,可以看出是边界的大小设置不正确。
你可以尝试修改 `copyMakeBorder` 函数的第二个和第三个参数,使其不为负数。具体来说,可以将它们设置为图像大小的一定比例。例如,将它们设置为图像高度的 $5\%$,宽度的 $5\%$,代码如下:
```c++
#include <opencv2/opencv.hpp>
int main() {
cv::Mat img = cv::imread("input.jpg");
cv::Mat T = cv::Mat::zeros(2, 3, CV_32FC1);
T.at<float>(0, 0) = 1;
T.at<float>(1, 1) = 1;
T.at<float>(1, 2) = -50; // 向上平移 50 个像素
cv::Mat dst;
cv::warpAffine(img, dst, T, img.size());
int top = cvRound(dst.rows * 0.05);
int bottom = cvRound(dst.rows * 0.05);
int left = cvRound(dst.cols * 0.05);
int right = cvRound(dst.cols * 0.05);
cv::copyMakeBorder(dst, dst, top, bottom, left, right,
cv::BORDER_CONSTANT, cv::Scalar(255, 255, 255));
cv::imshow("output", dst);
cv::waitKey();
return 0;
}
```
这样应该就能够去掉黑框了。如果还有其他问题,请提供更多信息。
error: (-215:Assertion failed) src1.type() == CV_32FC1 || src1.type() == CV_64FC1 in function 'cv::phaseCorrelate'
This error occurs when trying to use the phaseCorrelate function in OpenCV and the input images are not of the correct data type. The function requires that the input images be of type CV_32FC1 or CV_64FC1, which are single-channel floating point images of either 32 or 64 bits.
To fix this error, you need to convert the input images to the correct data type before passing them to the phaseCorrelate function. You can use the cv::cvtColor function to convert the images to grayscale, and then use cv::convertTo to convert them to the correct data type.
Here's an example of how to do this:
```
cv::Mat image1 = cv::imread("image1.jpg", cv::IMREAD_GRAYSCALE);
cv::Mat image2 = cv::imread("image2.jpg", cv::IMREAD_GRAYSCALE);
cv::Mat floatImage1, floatImage2;
image1.convertTo(floatImage1, CV_32FC1);
image2.convertTo(floatImage2, CV_32FC1);
cv::Point2d phaseShift = cv::phaseCorrelate(floatImage1, floatImage2);
```
In this example, we first load the input images as grayscale using cv::imread. We then convert them to the CV_32FC1 data type using cv::convertTo. Finally, we pass the converted images to the phaseCorrelate function to calculate the phase shift between the two images.
阅读全文