gpt4 book ai didi

c++ - opencv中dct实现错误

转载 作者:太空宇宙 更新时间:2023-11-03 22:30:10 26 4
gpt4 key购买 nike

为什么我尝试在opencv中实现dct时会出现以下错误?

Assertion failed (type == CV_32FC1 || type == CV_64FC1) in dct,

代码如下:

#include <cv.h>
#include "opencv2/imgproc/imgproc.hpp"
#include <highgui.h>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
Mat image,dimage(image);
image = imread( "lena.jpg", 1 );

if( !image.data )
{
printf( "No image data \n" );
return -1;
}

namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
imshow( "Display Image", image );
dimage=Mat::zeros(image.rows,image.cols,CV_32F);
dct(image, dimage, DCT_INVERSE == 0 );

namedWindow( "DCT Image", CV_WINDOW_AUTOSIZE );
imshow( "DCT image", dimage );
waitKey(0);

return 0;
}

最佳答案

你必须首先将你的图像从 uchar 转换为 float(可能稍后再转换回 uchar):

// also please use the c++ headers, not the old c-ones !

#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

Mat image = imread( "lena.jpg", 0 ); // grayscale

Mat fimage;
image.convertTo(fimage, CV_32F, 1.0/255); // also scale to [0..1] range (not mandatory)

// you're lucky with lena (512x512), for odd sizes, you have to
// pad it to pow2 size, or use dft() instead:
Mat dimage;
dct( fimage, dimage );
// process dimage,
// then same way back:
dct( dimage, dimage, DCT_INVERSE );
dimage.convertTo(image, CV_8U); // maybe scale back to [0..255] range (depending on your processing)

imshow("result",image);
waitKey();

关于c++ - opencv中dct实现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27730534/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com