Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h" //CWICZENIE NR 4 KOMPRESJA I DEKOMPRESJA OBRAZOW JPEG
- #include <opencv2/imgproc/imgproc.hpp> // Gaussian Blur
- #include <opencv2/core/core.hpp> // Basic OpenCV structures (CV::Mat, Scalar)
- #include <opencv2/highgui/highgui.hpp>
- #include <stdio.h>
- #include <math.h>
- #include <iostream>
- #define PI (3.141592653589793)
- using namespace std;
- using namespace CV;
- int _tmain(int argc, _TCHAR* argv[])
- {
- // Stworzenie okna w którym przechwycone obrazy będą wyświetlane
- cvNamedWindow("JPEG", CV_WINDOW_AUTOSIZE);
- // Pobranie obrazu
- Mat imageFruits = imread("fruits2.jpg",CV_LOAD_IMAGE_GRAYSCALE);
- // Uzyskanie macierzy pikseli na podstawie obiektów Mat
- Mat_<uchar> fruitsPixels = imageFruits;
- const int blockCols = 320/8;
- const int blockRows = 240/8;
- // Utworzenie bloków przechowujących odpowiednio: oryginalny obraz, obraz po DCT, obraz po IDCT
- float block[blockRows][blockCols][8][8];
- float blockDCT[blockRows][blockCols][8][8];
- float blockIDCT[blockRows][blockCols][8][8];
- // TODO
- // podział obrazu na bloki
- for(int i = 0; i<blockRows; i++)
- {
- for(int j = 0; j<blockCols; j++)
- {
- for(int k = 0; k<8; k++)
- {
- for(int l = 0; l<8; l++)
- {
- block[i][j][k][l] = fruitsPixels[i*8 + k][j*8 + l];
- blockDCT[i][j][k][l] = 0;
- blockIDCT[i][j][k][l] = 0;
- }
- }
- }
- }
- /*for(int i = 0; i<8; i++)
- {
- for (int j = 0; j<8; j++)
- {
- block[1][1][i][j] = 0;
- }
- }*/
- // zerowanie blockDCT i blockIDCT
- // TODO
- // DCT
- for(int i = 0; i<blockRows; i++)
- {
- for(int j = 0; j<blockCols; j++)
- {
- for(int k = 0; k<8; k++)
- {
- for(int l = 0; l<8; l++)
- {
- for(int m = 0; m<8; m++)
- {
- for(int n = 0; n<8; n++)
- {
- blockDCT[i][j][k][l] += block[i][j][m][n] * cos(((2*m + 1)*k*PI)/(2*8)) * cos(((2*n + 1)*l*PI)/(2*8));
- }
- }
- blockDCT[i][j][k][l] /= 64.0;
- }
- }
- }
- }
- // KWANTYZACJA
- float qMatrix[8][8]={
- {16,11,10,16,24,40,51,61},
- {12,12,14,19,26,58,60,55},
- {14,13,16,24,40,57,69,56},
- {14,17,22,29,51,87,80,62},
- {18,22,37,56,68,109,103,77},
- {24,35,55,64,81,104,114,92},
- {49,64,78,87,103,121,120,101},
- {72,92,95,98,112,100,103,99}
- };
- float qualityLevel = 50;
- for(int i = 0; i < 8; i++)
- {
- for (int j = 0; j < 8; j++)
- {
- qMatrix[i][j] = qMatrix[i][j] * ((100 - qualityLevel)/99)*0.18+0.02;
- }
- }
- for(int i = 0; i < blockRows; i++)
- {
- for (int j = 0; j < blockCols; j++)
- {
- for(int k = 0; k < 8; k++)
- {
- for (int l = 0; l < 8; l++)
- {
- blockDCT[i][j][k][l] = (int)(blockDCT[i][j][k][l] / qMatrix[k][l]);
- }
- }
- }
- }
- // DEKWANTYZACJA
- for(int i = 0; i < blockRows; i++)
- {
- for (int j = 0; j < blockCols; j++)
- {
- for(int k = 0; k < 8; k++)
- {
- for (int l = 0; l < 8; l++)
- {
- blockDCT[i][j][k][l] = blockDCT[i][j][k][l] * qMatrix[k][l];
- }
- }
- }
- }
- // IDCT
- for(int m = 0; m<blockRows; m++)
- {
- for(int n = 0; n<blockCols; n++)
- {
- for(int k = 0; k<8; k++)
- {
- for(int l = 0; l<8; l++)
- {
- for(int i = 0; i<8; i++)
- {
- for(int j = 0; j<8; j++)
- {
- int CU, CV;
- if(i == 0) CU = 1;
- else CU = 2;
- if(j == 0) CV = 1;
- else CV = 2;
- //blockIDCT[i][j][k][l] += CU * CV * blockDCT[i][j][m][n] * cos(((2*k + 1)*m*PI)/(2*8)) * cos(((2*l + 1)*n*PI)/(2*8));
- blockIDCT[m][n][k][l] += CU * CV * blockDCT[m][n][i][j] * cos(((2*k + 1)*i*PI)/(2*8)) * cos(((2*l + 1)*j*PI)/(2*8));
- }
- }
- }
- }
- }
- }
- // TODO
- // "składanie" obrazu z bloków
- for(int i = 0; i<blockRows; i++)
- {
- for(int j = 0; j<blockCols; j++)
- {
- for(int k = 0; k<8; k++)
- {
- for(int l = 0; l<8; l++)
- {
- blockIDCT[i][j][k][l] > 255 ? blockIDCT[i][j][k][l] = 255 : blockIDCT[i][j][k][l] < 0 ? blockIDCT[i][j][k][l] = 0 : blockIDCT[i][j][k][l] = blockIDCT[i][j][k][l];
- fruitsPixels[i*8+k][j*8+l] = blockIDCT[i][j][k][l];
- }
- }
- }
- }
- // Wyświetlenie obrazu
- imshow("JPEG", imageFruits);
- // Oczekiwanie na wciśnięcie klawisza Esc lub Enter
- char key;
- do key = cvWaitKey(1);
- while(key != 27 && key != 13);
- // Niszczenie okna
- cvDestroyWindow("JPEG");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement