Advertisement
bartek27210

techniki_muli_lab2

Apr 15th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 KB | None | 0 0
  1. #include "stdafx.h"
  2.  
  3. #define _USE_MATH_DEFINES
  4.  
  5. #include <opencv2/imgproc/imgproc.hpp>  // Gaussian Blur
  6. #include <opencv2/core/core.hpp>        // Basic OpenCV structures (cv::Mat, Scalar)
  7. #include <opencv2/highgui/highgui.hpp>
  8. #include <stdio.h>
  9. #include <math.h>
  10. #include <iostream>
  11.  
  12. using namespace std;
  13. using namespace cv;
  14.  
  15. bool Rotate(Mat_<uchar> &peppersPixels, Mat_<uchar> &rotatedPeppersPixels);
  16. double stop=0, tempo=1;
  17. int kierunekObrotu = 0;
  18.  
  19. int _tmain(int argc, _TCHAR* argv[])
  20. {
  21.     // Stworzenie okna w którym przechwycone obrazy będą wyświetlane
  22.     cvNamedWindow("Obracanie obrazu", CV_WINDOW_AUTOSIZE);
  23.    
  24.     // Pobranie obrazu
  25.     Mat imagePeppers = imread("peppers.jpg",CV_LOAD_IMAGE_GRAYSCALE);
  26.  
  27.     // Utworzenie obiektu przechowującego obraz, który będzie obracany
  28.     Mat rotatedPeppers = imagePeppers.clone();
  29.  
  30.     // Uzyskanie macierzy pikseli na podstawie obiektów Mat
  31.     Mat_<uchar> peppersPixels = imagePeppers;
  32.     Mat_<uchar> rotatedPeppersPixels = rotatedPeppers;
  33.  
  34.     while(1)
  35.     {
  36.         // Obracanie obrazu
  37.         if(Rotate(peppersPixels, rotatedPeppersPixels) == true)
  38.             break;
  39.         medianBlur ( rotatedPeppers,  rotatedPeppers, 3);
  40.         // Wyświetlanie obrazu
  41.         imshow("Obracanie obrazu", rotatedPeppers);
  42.     }
  43.  
  44.     // Niszczenie okna
  45.     cvDestroyWindow("Obracanie obrazu");
  46.     return 0;
  47. }
  48.  
  49. bool Rotate(Mat_<uchar> &peppersPixels, Mat_<uchar> &rotatedPeppersPixels)
  50. {
  51.     /* Funkcja obracająca obraz peppersPixels o liczbę stopni zwiększaną o 1 przy każdym kolejnym wywołaniu.
  52.     Kiedy liczba stopni dochodzi do 360, następuje powtórzenie cyklu - ustawienie liczbę stopni na 0.
  53.     Wynikowy obraz (obrócony) zostaje zapisywany w rotatedPeppersPixels. */
  54.    
  55.     // TODO
  56.  
  57.     double rad= stop*M_PI/180;
  58.  
  59.     for (int i = 0; i < peppersPixels.rows; i++) {
  60.         for (int j = 0; j < peppersPixels.cols; j++) {
  61.             rotatedPeppersPixels[i][j] = 0;
  62.         }
  63.     }
  64.  
  65.    
  66.  
  67.     for (int i = 0; i < peppersPixels.rows; i++) {
  68.         for (int j = 0; j < peppersPixels.cols; j++) {
  69.             int x, y;
  70.            
  71.             x = ( i-peppersPixels.cols/2)* sin(rad) + (j-peppersPixels.rows/2) * cos(rad)+peppersPixels.rows/2+0.5;
  72.             y =( i-peppersPixels.rows/2)* cos(rad) - (j-peppersPixels.cols/2) * sin(rad)+peppersPixels.cols/2+0.5;
  73.            
  74.            
  75.  
  76.             if (x >= 0 && y >= 0 && x < peppersPixels.rows && y <peppersPixels.cols) {
  77.                 rotatedPeppersPixels[x][y] = peppersPixels[i][j];}
  78.         }
  79.     }
  80.  
  81.    
  82.  
  83.     //stop++;
  84.     //if(stop==360)
  85.         //stop=0;
  86.  
  87.  
  88.     if (kierunekObrotu == 0) {
  89. stop = stop + tempo;
  90. }
  91. else if (kierunekObrotu == 1) {
  92. stop= stop - tempo;
  93. }
  94. if (stop > 359) {
  95. stop= 0;
  96. }
  97. else if (stop < -359) {
  98.     stop= 0;
  99. }
  100.    
  101.  
  102.     // Oczekiwanie na wciśnięcie klawisza Esc lub Enter
  103.     char key = cvWaitKey(1);
  104.     if(key == 27 )
  105.         return true;
  106.     if(key == 13 )
  107.         tempo++;
  108.     if(key == 97 )
  109.         tempo--;
  110.     if(key==32){
  111.         if(kierunekObrotu==0) kierunekObrotu=1;
  112.          else kierunekObrotu=0;
  113.     }
  114.  
  115.     return false;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement