Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #define _USE_MATH_DEFINES
- #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>
- using namespace std;
- using namespace cv;
- bool Rotate(Mat_<uchar> &peppersPixels, Mat_<uchar> &rotatedPeppersPixels);
- double stop=0, tempo=1;
- int kierunekObrotu = 0;
- int _tmain(int argc, _TCHAR* argv[])
- {
- // Stworzenie okna w którym przechwycone obrazy będą wyświetlane
- cvNamedWindow("Obracanie obrazu", CV_WINDOW_AUTOSIZE);
- // Pobranie obrazu
- Mat imagePeppers = imread("peppers.jpg",CV_LOAD_IMAGE_GRAYSCALE);
- // Utworzenie obiektu przechowującego obraz, który będzie obracany
- Mat rotatedPeppers = imagePeppers.clone();
- // Uzyskanie macierzy pikseli na podstawie obiektów Mat
- Mat_<uchar> peppersPixels = imagePeppers;
- Mat_<uchar> rotatedPeppersPixels = rotatedPeppers;
- while(1)
- {
- // Obracanie obrazu
- if(Rotate(peppersPixels, rotatedPeppersPixels) == true)
- break;
- medianBlur ( rotatedPeppers, rotatedPeppers, 3);
- // Wyświetlanie obrazu
- imshow("Obracanie obrazu", rotatedPeppers);
- }
- // Niszczenie okna
- cvDestroyWindow("Obracanie obrazu");
- return 0;
- }
- bool Rotate(Mat_<uchar> &peppersPixels, Mat_<uchar> &rotatedPeppersPixels)
- {
- /* Funkcja obracająca obraz peppersPixels o liczbę stopni zwiększaną o 1 przy każdym kolejnym wywołaniu.
- Kiedy liczba stopni dochodzi do 360, następuje powtórzenie cyklu - ustawienie liczbę stopni na 0.
- Wynikowy obraz (obrócony) zostaje zapisywany w rotatedPeppersPixels. */
- // TODO
- double rad= stop*M_PI/180;
- for (int i = 0; i < peppersPixels.rows; i++) {
- for (int j = 0; j < peppersPixels.cols; j++) {
- rotatedPeppersPixels[i][j] = 0;
- }
- }
- for (int i = 0; i < peppersPixels.rows; i++) {
- for (int j = 0; j < peppersPixels.cols; j++) {
- int x, y;
- x = ( i-peppersPixels.cols/2)* sin(rad) + (j-peppersPixels.rows/2) * cos(rad)+peppersPixels.rows/2+0.5;
- y =( i-peppersPixels.rows/2)* cos(rad) - (j-peppersPixels.cols/2) * sin(rad)+peppersPixels.cols/2+0.5;
- if (x >= 0 && y >= 0 && x < peppersPixels.rows && y <peppersPixels.cols) {
- rotatedPeppersPixels[x][y] = peppersPixels[i][j];}
- }
- }
- //stop++;
- //if(stop==360)
- //stop=0;
- if (kierunekObrotu == 0) {
- stop = stop + tempo;
- }
- else if (kierunekObrotu == 1) {
- stop= stop - tempo;
- }
- if (stop > 359) {
- stop= 0;
- }
- else if (stop < -359) {
- stop= 0;
- }
- // Oczekiwanie na wciśnięcie klawisza Esc lub Enter
- char key = cvWaitKey(1);
- if(key == 27 )
- return true;
- if(key == 13 )
- tempo++;
- if(key == 97 )
- tempo--;
- if(key==32){
- if(kierunekObrotu==0) kierunekObrotu=1;
- else kierunekObrotu=0;
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement