DCT 实现存在问题
Issue with a DCT implementation
我必须在C++中实现DCT算法,这是我现在的代码:
// dct: computes the discrete cosinus tranform of a 8x8 block
template<typename Tin=uchar,typename Tout=float>
inline cv::Mat_<Tout> dct(const cv::Mat_<Tin>& oBlock) {
int indexNumber;
float pi = 3.14159265359;
float fcoscos, fxy, cos1, cos2, forCos1, forCos2;
cv::Mat_<Tout> resultBloc(8, 8);
for (int u = 0; u < oBlock.rows; u++){
for (int v = 0; v < oBlock.cols; v++){
float cu=0, cv=0, Result=0;
// calcul c(u)
if (u == 0){
cu = (float)sqrt((float)1 / (float)oBlock.rows);
}
else {
cu = (float)sqrt((float)2 / (float)oBlock.rows);
}
// calcul c(v)
if (v == 0){
cv = (float)sqrt((float)1 / (float)oBlock.cols);
}
else {
cv = (float)sqrt((float)2 / (float)oBlock.cols);
}
float sums = 0;
for (int x = 0; x < oBlock.rows; x++){
for (int y = 0; y < oBlock.cols; y++){
indexNumber = x * oBlock.rows + y;
fxy = (int)oBlock.data[indexNumber];
forCos1 = (pi*((2 * x) + 1)*u) / (2 * oBlock.rows);
forCos2 = (pi*((2 * y) + 1)*v) / (2 * oBlock.cols);
cos1 = cos(forCos1);
cos2 = cos(forCos2);
fcoscos = fxy * cos1 * cos2;
sums += fcoscos;
}
}
// calcul total
Result = sums*cu*cv;
indexNumber = u * oBlock.rows + v;
resultBloc.data[indexNumber] = Result;
}
}
return resultBloc;
}
我将结果与cv DCT算法进行了如下比较:
cv::Mat_<float> tempImage(8,8);
for (int i = 0; i < vecImageCut[0].cols*vecImageCut[0].rows; i++){
tempImage.data[i] = (int)vecImageCut[0].data[i];
}
cv::Mat_<float> dctCV;
cv::dct(tempImage, dctCV);
for (int i = 0; i < blocksAfterDCT[0].cols*blocksAfterDCT[0].rows; i++){
std::cerr << "Difference DCT for pixel " << i << " : " << dctCV.data[i] - blocksAfterDCT[0].data[i] << std::endl;
}
我的 DCT 和 cv DCT 之间的结果非常不同,所以我假设我的 DCT 算法是错误的,但我搜索了几个小时,找不到我的错误,谁能告诉我我哪里做错了什么?
你的指数计算是错误的。 在indexNumber = x * oBlock.rows + y;
中,由于x
正在计算行数,因此需要乘以列数:
indexNumber = x * oBlock.cols + y;
indexNumber = u * oBlock.rows + v;
也是如此
indexNumber = u * oBlock.cols + v;
相关文章:
- C++quit()函数中可能存在作用域问题
- 这个极客对极客的trie实现是否存在内存泄漏问题
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数
- gcc和clang在表达式是否为常量求值的问题上存在分歧
- C++Builder中的OnClick事件签名存在问题
- 当我使用 C++ 中的 C# dll 来使用 Selenium 时,存在异常处理问题
- C++ 中的函数 GetCursorPos() 和 SetCursorPos() 存在一些问题
- 在 C++ 中使用逗号的 echo 命令中存在问题
- Malloc 在使用线程并行化 SSH 调用时存在问题
- 将文件复制到自定义位置,存在字符串转换问题
- 构造函数 (C++) 中的 char 指针参数存在问题
- 在 boost::variant 中类 holden 的复制构造函数存在问题
- 具有sleep_for和sleep_until功能的 gcc 可能存在的问题
- 用户定义的文本运算符(在原始模式下)存在问题
- 以下代码中存在哪些问题?
- 这种比较是否不一致(或者存在其他问题)?
- C++中的数组存在问题
- 用于检测函数是否存在的C++元函数的问题
- 以下代码存在哪些问题?
- 我的选择排序代码是否存在导致它跳过数组中的元素的问题?