由于二维变换展开形式过于复杂,这里不进行展开
感兴趣的读者可以查阅相关资料进行学习.
OpenCV4 提供了
dct()
函数用于计算离散余弦变换.
?离散余弦逆变换
?离散余弦变换的正逆交换是相反的交换,由于 dct()函数只能变换单通道的矩阵,因此分别对单个通道进行离散余弦变换,并将变换结果重新组成一幅具有三通道的彩色图像。
void visionagin::Mydct()
{
//对数据进行离散余弦变换
Mat data = (Mat_<float>(5, 5) << 1, 2, 3, 4, 5,
2, 3, 4, 5, 6,
3, 4, 5, 6, 7,
4, 5, 6, 7, 8,
5, 6, 7, 8, 9
);
Mat data_out, i_data_out;
dct(data, data_out);
idct(data_out, i_data_out);
cout << data_out << endl;
cout << i_data_out << endl;
Mat img = imread("C:\\Users\\86176\\Downloads\\visionimage\\gaoda.jfif");
imshow("原图", img);
//计算最优尺寸
int width = 2 * getOptimalDFTSize((img.cols + 1)/2);
int height = 2 * getOptimalDFTSize((img.rows + 1) / 2);
//扩展图像
int t = 0;
int b = height - img.rows - t;
int l = 0;
int r = width - img.cols-l;
Mat appropriate;
copyMakeBorder(img, appropriate, t, b, l, r, BORDER_CONSTANT, Scalar(255, 255, 255));
//对三通道分别进行余弦离散变换
vector<Mat>channels;
split(appropriate, channels);
//提取bgr各通道的值
Mat one = channels.at(0);
Mat two = channels.at(1);
Mat three = channels.at(2);
Mat one_out, two_out, three_out;
//数据必须是单通道的浮点数
dct(Mat_<float>(one), one_out);
dct(Mat_<float>(two), two_out);
dct(Mat_<float>(three), three_out);
//再将结果合并
vector<Mat>total_out;
total_out.push_back(one_out);
total_out.push_back(two_out);
total_out.push_back(three_out);
Mat result;
merge(total_out, result);
imshow("result", result);
}
?
|