Advertisement
bartek27210

lab3_muit_labki

May 13th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.37 KB | None | 0 0
  1. #include "stdafx.h"
  2.  
  3. #include <opencv2/imgproc/imgproc.hpp>  // Gaussian Blur
  4. #include <opencv2/core/core.hpp>        // Basic OpenCV structures (cv::Mat, Scalar)
  5. #include <opencv2/highgui/highgui.hpp>
  6. #include <stdio.h>
  7. #include <math.h>
  8. #include <iostream>
  9.  
  10. using namespace std;
  11. using namespace cv;
  12. int prog=40;
  13. void detectEdges(Mat_<uchar> &peppersPixels, Mat_<uchar> &handEdgesMergedPixels, int centerX, int centerY);
  14.  
  15. int _tmain(int argc, _TCHAR* argv[])
  16. {
  17.     // Stworzenie okna w którym przechwycone obrazy będą wyświetlane
  18.     cvNamedWindow("Wykrywanie krawędzi", CV_WINDOW_AUTOSIZE);
  19.    
  20.     // Pobranie obrazu
  21.     Mat imagePeppers = imread("hand.jpg",CV_LOAD_IMAGE_GRAYSCALE);
  22.  
  23.     // Utworzenie obiektu przechowującego obraz z wyznaczonymi krawędziami
  24.     Mat handEdgesMerged = imagePeppers.clone();
  25.  
  26.     // Uzyskanie macierzy pikseli na podstawie obiektów Mat
  27.     Mat_<uchar> handPixels = imagePeppers;
  28.     Mat_<uchar> handEdgesMergedPixels = handEdgesMerged;
  29.  
  30.     // Wyznaczenie wpółrzędnych środa obrazu
  31.     int centerX = handPixels.cols/2;
  32.     int centerY = handPixels.rows/2;
  33.  
  34.     // Obracanie obrazu
  35.     detectEdges(handPixels, handEdgesMergedPixels, centerX, centerY);
  36.  
  37.     // Wyświetlanie obrazu
  38.     imshow("Wykrywanie krawędzi", handEdgesMerged);
  39.  
  40.     // Oczekiwanie na wciśnięcie klawisza Esc lub Enter
  41.     char key;
  42.     do  key = cvWaitKey(1);
  43.     while(key != 27 && key != 13);
  44.     int prog=100;
  45.     // Niszczenie okna
  46.     cvDestroyWindow("Wykrywanie krawędzi");
  47.     return 0;
  48. }
  49.  
  50. void detectEdges(Mat_<uchar> &handPixels, Mat_<uchar> &handEdgesMergedPixels, int centerX, int centerY)
  51. {
  52.     /* Funkcja wyznaczająca krawędzie za pomocą maski morfologicznej podanej przez prowadzącego.
  53.     Wyznaczane są najpierw krawędzie pionowe, później poziome. Następnie wykonywane jest scalenie
  54.     krawędzi pionowych i poziomych, przeskalowanie obrazu scalonego do zakresu 0-255 oraz binaryzacja
  55.     z eksperymentalnie dobranym progiem.
  56.     Wynikowy obraz zostaje zapisany w handEdgesMergedPixels.*/
  57.    
  58.     // Utworzenie obiektów przechowujących obrazy z wyznaczonymi krawędziami: pionowymi i poziomymi
  59.     Mat handEdgesVertical = handPixels.clone();
  60.     Mat handEdgesHorizontal = handPixels.clone();
  61.  
  62.     // Uzyskanie macierzy pikseli na podstawie obiektów Mat
  63.     Mat_<int> handEdgesVerticalPixels = handEdgesVertical;
  64.     Mat_<int> handEdgesHorizontalPixels = handEdgesHorizontal;
  65.  
  66.  
  67.  
  68.     int sobel[3][3]= {
  69.         {1,2,1},
  70.         {0,0,0},
  71.         {-1,-2,-1}
  72.     };
  73.  
  74.     int prewitt[3][3]={
  75.         {-1,0,1},
  76.         {-1,0,1},
  77.         {-1,0,1}
  78. };
  79.  
  80.  
  81.     for(int y = 1; y < handEdgesHorizontalPixels.rows - 1; y++){
  82.         for(int x = 1; x < handEdgesHorizontalPixels.cols - 1; x++){
  83.             int suma =
  84.             handPixels[y-1][x-1]* sobel[0][0] +
  85.             handPixels[y-1][x]* sobel[0][1] +
  86.             handPixels[y-1][x+1]* sobel[0][2] +
  87.             handPixels[y][x-1]* sobel[1][0] +
  88.             handPixels[y][x]* sobel[1][1] +
  89.             handPixels[y][x+1]* sobel[1][2] +
  90.             handPixels[y+1][x-1]* sobel[2][0] +
  91.             handPixels[y+1][x]* sobel[2][1] +
  92.             handPixels[y+1][x+1]* sobel[2][2];
  93.             handEdgesHorizontalPixels[y][x] = abs(suma);
  94.         }
  95.     }
  96.  
  97.     int max = handEdgesHorizontalPixels[0][0];
  98.     int min = handEdgesHorizontalPixels[0][0];
  99.  
  100.     for(int i=0; i < handEdgesHorizontalPixels.rows; i++){
  101.         for (int j=0; j < handEdgesHorizontalPixels.cols; j++){
  102.             if (handEdgesHorizontalPixels[i][j] < min){
  103.                 min = handEdgesHorizontalPixels[i][j];
  104.             }
  105.             if (handEdgesHorizontalPixels[i][j] > max){
  106.                 max = handEdgesHorizontalPixels[i][j];
  107.             }
  108.         }
  109.     }
  110.  
  111.     for(int y=1; y < handEdgesHorizontalPixels.rows - 1; y++){
  112.         for (int x=1; x < handEdgesHorizontalPixels.cols - 1; x++){
  113.             handEdgesHorizontalPixels[y][x] = ((handEdgesHorizontalPixels[y][x]-min)*(max-min))/(max-min);
  114.         }
  115.     }
  116.  
  117.     //handEdgesMergedPixels=handEdgesHorizontalPixels;
  118.  
  119.     for(int y = 1; y < handEdgesVerticalPixels.rows - 1; y++){
  120.         for(int x = 1; x < handEdgesVerticalPixels.cols - 1; x++){
  121.             int suma =
  122.             handPixels[y-1][x-1]* prewitt[0][0] +
  123.             handPixels[y-1][x]* prewitt[0][1] +
  124.             handPixels[y-1][x+1]* prewitt[0][2] +
  125.             handPixels[y][x-1]* prewitt[1][0] +
  126.             handPixels[y][x]* prewitt[1][1] +
  127.             handPixels[y][x+1]* prewitt[1][2] +
  128.             handPixels[y+1][x-1]* prewitt[2][0] +
  129.             handPixels[y+1][x]* prewitt[2][1] +
  130.             handPixels[y+1][x+1]* prewitt[2][2];
  131.             handEdgesVerticalPixels[y][x] = abs(suma);
  132.         }
  133.     }
  134.  
  135.     for(int i=0; i < handEdgesVerticalPixels.rows; i++) {
  136.         for (int j=0; j < handEdgesVerticalPixels.cols; j++){
  137.             if (handEdgesVerticalPixels[i][j] < min) {
  138.                 min = handEdgesVerticalPixels[i][j];
  139.             }
  140.             if (handEdgesVerticalPixels[i][j] > max){
  141.                 max = handEdgesVerticalPixels[i][j];
  142.             }
  143.         }
  144.     }
  145.  
  146.     for(int y=0; y < handEdgesVerticalPixels.rows - 1; y++) {
  147.         for (int x=0; x < handEdgesVerticalPixels.cols - 1; x++) {
  148.             handEdgesVerticalPixels[y][x] = ((handEdgesVerticalPixels[y][x]-min)*(max-min))/(max-min);
  149.         }
  150.     }
  151.  
  152.     int minskala = handEdgesVerticalPixels[0][0]+handEdgesHorizontalPixels[0][0];
  153.     int maxskala = handEdgesVerticalPixels[0][0]+handEdgesHorizontalPixels[0][0];
  154.  
  155.     for(int i=0; i < handEdgesVerticalPixels.rows; i++){
  156.         for(int j = 0; j < handEdgesVerticalPixels.cols; j++) {
  157.             if(handEdgesVerticalPixels[i][j] + handEdgesHorizontalPixels[i][j] > maxskala)
  158.                 maxskala = handEdgesVerticalPixels[i][j] + handEdgesHorizontalPixels[i][j];
  159.             if(handEdgesVerticalPixels[i][j] + handEdgesHorizontalPixels[i][j] < minskala)
  160.                 minskala = handEdgesVerticalPixels[i][j] + handEdgesHorizontalPixels[i][j];
  161.         }
  162.     }
  163.  
  164.         Mat_<int> got = handEdgesVertical;
  165.         for(int y=0;y< handEdgesVerticalPixels.rows; y++){
  166.         for(int x = 0; x < handEdgesVerticalPixels.cols; x++){
  167.             got[y][x]=((handEdgesHorizontalPixels[y][x]+handEdgesVerticalPixels[y][x]-minskala)*(maxskala-minskala))/(maxskala-minskala);
  168.         }
  169.     }
  170.        
  171.  for(int y=0;y< got.rows; y++){
  172.         for(int x = 0; x < got.cols; x++){
  173.             if(got[y][x]>prog)
  174.                 got[y][x]=255;
  175.             else got[y][x]=0;
  176.         }
  177.  }
  178.     handEdgesMergedPixels = got;
  179.  
  180.    
  181.     // TODO
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement