c++實現矩陣直接做dct變換
阿新 • • 發佈:2019-01-02
#include <iostream> #include <string.h> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2\core\mat.hpp> #include<opencv2/opencv.hpp>//如果要包含所有庫 #define PI 3.1415926 using namespace cv; using namespace std; //將mat型別轉換為二維陣列的型別 //進行dct變換 double Coun(int x) { if (x != 0) return 1.0; return 1.0 / (sqrt(2.0)); } void mydct(double (*matrix)[8], int M, int N) { double matrix1[4][8]; int p, q; int m, n; double ap, aq; double num; double max; //double *point = (double *)A.data; int i, j; //for (i = 0; i < M; i++) //{ // for (j = 0; j < N; j++) // { // //cout << point[i*N + j] << "\n"; // cout << A.at<float>(i, j) << "\n"; // } //} //for (p = 0; p < M; p++) //{ // cout << p << "\n"; // for (q = 0; q < N; q++) // { // double tmp = 0.0; // for (m = 0; m < M; m++) // { // for (n = 0; n < N; n++) // { // tmp += A.data[m*N + n] * cos((2 * m + 1)*p*PI / (2.0 * M)) * cos((2 * n + 1)*q*PI / (2.0 * N));//每個畫素對應的tmp值即累加式計算完畢 // } // } // //將畫素值賦給B.data // B.at<float>(p,q)= 2.0*Coun(p)*Coun(q)*tmp/M; // cout << "p:" << p << " q:" << q << " p*N+q:" << p*N + q << " tmp:" << tmp << " mutiply:" << 2.0*Coun(p)*Coun(q)*tmp / M << " B.data:" << (double)B.data[p*N + q] << "\n"; // } //} for (p = 0; p < M; p++) { cout << p << "\n"; for (q = 0; q < N; q++) { if (p == 0) { ap = sqrt(1.0 / M); } else { ap = sqrt(2.0 / M); } if (q == 0) { aq = sqrt(1.0 / N); } else { aq = sqrt(2.0 / N); } double tmp = 0.0; for (m = 0; m < M; m++) { for (n = 0; n < N; n++) { //tmp += A.at<float>(p, q)*255.0 * cos((2 * m + 1)*p*PI / (2.0 * M)) * cos((2 * n + 1)*q*PI / (2.0 * N));//每個畫素對應的tmp值即累加式計算完畢 // tmp += data[i][j] * cos((2 * i + 1)*u*PI / (2.0 * NUM)) * cos((2 * j + 1)*v*PI / (2.0 * NUM)); tmp += matrix[m][n] * cos((2 * m + 1)*p*PI / (2.0 * M)) * cos((2 * n + 1)*q*PI / (2.0 * N));//每個畫素對應的tmp值即累加式計算完畢 } } //將畫素值賦給B.data matrix1[p][q] = ap*aq*tmp; //cout << "p:" << p << " q:" << q << " p*N+q:" << p*N + q << " tmp:" << tmp << " mutiply:" << ap*aq*tmp << " B.data:" << B.data[p*N + q] << "\n"; cout << "p:" << p << " q:" << q << " p*N+q:" << p*N + q << " tmp:" << tmp << " mutiply:" << ap*aq*tmp << " B.data:" << matrix1[p][q] << "\n"; } } //得出max //max = B.data[0]; //for (i = 0; i < M; i++) //{ // for (j = 0; j < N; j++) // { // if (max<B.data[i*N+j]) // { // max=B.data[i*N + j]; // } // } // // //} // cout << "the value of max:" << max << "\n"; //得出歸一化的B.data //for (i = 0; i < M; i++) //{ // for (j = 0; j < N; j++) // { // C.data[i*N + j] = 255 - B.data[i*N + j]; // } //} } //將二維陣列轉化成mat型別以圖片形式輸出 void main() { double matrix[4][8] = { { 89.0, 101.0, 114.0, 125.0, 126.0, 115.0, 105.0, 96.0 }, { 97.0, 115.0, 131.0, 147.0, 149.0, 135.0, 123.0, 113.0 }, { 114.0, 134.0, 159.0, 178.0, 175.0, 164.0, 149.0, 137.0 }, { 121.0, 143.0, 177.0, 196.0, 201.0, 189.0, 165.0, 150.0 }, }; //double matrix[8][8] = {{ 89, 101, 114, 125, 126, 115, 105, 96 }, //{ 97, 115, 131, 147, 149, 135, 123, 113 }, //{ 114, 134, 159, 178, 175, 164, 149, 137 }, //{ 121, 143, 177, 196, 201, 189, 165, 150 }, //{ 119, 141, 175, 201, 207, 186, 162, 144 }, //{ 107, 130, 165, 189, 192, 171, 144, 125 }, //{ 97, 119, 149, 171, 172, 145, 117, 96 }, //{ 88, 107, 136, 156, 155, 129, 97, 75 } //}; int M = 4; int N = 8; mydct(matrix, M, N); }
執行結果與matlab相比較:
G
M
T
Detect languageAfrikaansAlbanianAmharicArabicArmenianAzerbaijaniBasqueBelarusianBengaliBosnianBulgarianCatalanCebuanoChichewaChinese (Simplified)Chinese (Traditional)CorsicanCroatianCzechDanishDutchEnglishEsperantoEstonianFilipinoFinnishFrenchFrisianGalicianGeorgianGermanGreekGujaratiHaitian CreoleHausaHawaiianHebrewHindiHmongHungarianIcelandicIgboIndonesianIrishItalianJapaneseJavaneseKannadaKazakhKhmerKoreanKurdishKyrgyzLaoLatinLatvianLithuanianLuxembourgishMacedonianMalagasyMalayMalayalamMalteseMaoriMarathiMongolianMyanmar (Burmese)NepaliNorwegianPashtoPersianPolishPortuguesePunjabiRomanianRussianSamoanScots GaelicSerbianSesothoShonaSindhiSinhalaSlovakSlovenianSomaliSpanishSundaneseSwahiliSwedishTajikTamilTeluguThaiTurkishUkrainianUrduUzbekVietnameseWelshXhosaYiddishYorubaZulu | AfrikaansAlbanianAmharicArabicArmenianAzerbaijaniBasqueBelarusianBengaliBosnianBulgarianCatalanCebuanoChichewaChinese (Simplified)Chinese (Traditional)CorsicanCroatianCzechDanishDutchEnglishEsperantoEstonianFilipinoFinnishFrenchFrisianGalicianGeorgianGermanGreekGujaratiHaitian CreoleHausaHawaiianHebrewHindiHmongHungarianIcelandicIgboIndonesianIrishItalianJapaneseJavaneseKannadaKazakhKhmerKoreanKurdishKyrgyzLaoLatinLatvianLithuanianLuxembourgishMacedonianMalagasyMalayMalayalamMalteseMaoriMarathiMongolianMyanmar (Burmese)NepaliNorwegianPashtoPersianPolishPortuguesePunjabiRomanianRussianSamoanScots GaelicSerbianSesothoShonaSindhiSinhalaSlovakSlovenianSomaliSpanishSundaneseSwahiliSwedishTajikTamilTeluguThaiTurkishUkrainianUrduUzbekVietnameseWelshXhosaYiddishYorubaZulu |
Text-to-speech function is limited to 200 characters