pabloducato

Prewitt

Apr 8th, 2019
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.97 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.  
  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.  
  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.     //handEdgesHorizontal = ;
  59.     //handEdgesMergedPixels = handEdgesHorizontal;
  60.  
  61.    
  62.     // Utworzenie obiektów przechowujących obrazy z wyznaczonymi krawędziami: pionowymi i poziomymi
  63.     Mat handEdgesVertical = handPixels.clone();
  64.     Mat handEdgesHorizontal = handPixels.clone();
  65.  
  66.     // Uzyskanie macierzy pikseli na podstawie obiektów Mat
  67.     Mat_<int> handEdgesVerticalPixels = handEdgesVertical;
  68.     Mat_<int> handEdgesHorizontalPixels = handEdgesHorizontal;
  69.  
  70.     // TODO
  71.  
  72.     static int sobel_horizontal[3][3]=
  73.     {
  74.         {1,2,1},
  75.         {0,0,0},
  76.         {-1,-2,-1}
  77.     };
  78.     static int sobel_vertical[3][3]=
  79.     {
  80.         {1,0,-1},
  81.         {2,0,-2},
  82.         {1,0,-1}
  83.     };
  84.  
  85.     //horizontal
  86.     for(int y = 1; y < handEdgesHorizontalPixels.rows - 1; y++)
  87.     {
  88.         for(int x = 1; x < handEdgesHorizontalPixels.cols - 1; x++)
  89.         {
  90.             int suma =
  91.             handPixels[y-1][x-1]* sobel_horizontal[0][0] +
  92.             handPixels[y-1][x]* sobel_horizontal[0][1] +
  93.             handPixels[y-1][x+1]* sobel_horizontal[0][2] +
  94.             handPixels[y][x-1]* sobel_horizontal[1][0] +
  95.             handPixels[y][x]* sobel_horizontal[1][1] +
  96.             handPixels[y][x+1]* sobel_horizontal[1][2] +
  97.             handPixels[y+1][x-1]* sobel_horizontal[2][0] +
  98.             handPixels[y+1][x]* sobel_horizontal[2][1] +
  99.             handPixels[y+1][x+1]* sobel_horizontal[2][2];
  100.             handEdgesHorizontalPixels[y][x] = abs(suma);
  101.         }
  102.     }
  103.     int min = handEdgesHorizontalPixels[0][0];
  104.     int max = handEdgesHorizontalPixels[0][0];
  105.  
  106.     for(int i=0; i < handEdgesHorizontalPixels.rows; i++)
  107.     {
  108.         for (int j=0; j < handEdgesHorizontalPixels.cols; j++)
  109.         {
  110.             if (handEdgesHorizontalPixels[i][j] < min)
  111.             {
  112.                 min = handEdgesHorizontalPixels[i][j];
  113.             }
  114.             if (handEdgesHorizontalPixels[i][j] > max)
  115.             {
  116.                 max = handEdgesHorizontalPixels[i][j];
  117.             }
  118.         }
  119.     }
  120.  
  121.     for(int y=1; y < handEdgesHorizontalPixels.rows - 1; y++)
  122.     {
  123.         for (int x=1; x < handEdgesHorizontalPixels.cols - 1; x++)
  124.         {
  125.             handEdgesHorizontalPixels[y][x] = ((handEdgesHorizontalPixels[y][x]-min)*255)/(max-min);
  126.         }
  127.     }
  128.  
  129.     //vertical
  130.  
  131.     for(int y = 1; y < handEdgesVerticalPixels.rows - 1; y++)
  132.     {
  133.         for(int x = 1; x < handEdgesVerticalPixels.cols - 1; x++)
  134.         {
  135.             int suma =
  136.             handPixels[y-1][x-1]* sobel_vertical[0][0] +
  137.             handPixels[y-1][x]* sobel_vertical[0][1] +
  138.             handPixels[y-1][x+1]* sobel_vertical[0][2] +
  139.             handPixels[y][x-1]* sobel_vertical[1][0] +
  140.             handPixels[y][x]* sobel_vertical[1][1] +
  141.             handPixels[y][x+1]* sobel_vertical[1][2] +
  142.             handPixels[y+1][x-1]* sobel_vertical[2][0] +
  143.             handPixels[y+1][x]* sobel_vertical[2][1] +
  144.             handPixels[y+1][x+1]* sobel_vertical[2][2];
  145.             handEdgesVerticalPixels[y][x] = abs(suma);
  146.         }
  147.     }
  148.  
  149.     for(int i=0; i < handEdgesVerticalPixels.rows; i++)
  150.     {
  151.         for (int j=0; j < handEdgesVerticalPixels.cols; j++)
  152.         {
  153.             if (handEdgesVerticalPixels[i][j] < min)
  154.             {
  155.                 min = handEdgesVerticalPixels[i][j];
  156.             }
  157.             if (handEdgesVerticalPixels[i][j] > max)
  158.             {
  159.                 max = handEdgesVerticalPixels[i][j];
  160.             }
  161.         }
  162.     }
  163.  
  164.     for(int y=0; y < handEdgesVerticalPixels.rows - 1; y++)
  165.     {
  166.         for (int x=0; x < handEdgesVerticalPixels.cols - 1; x++)
  167.         {
  168.             handEdgesVerticalPixels[y][x] = ((handEdgesVerticalPixels[y][x]-min)*255)/(max-min);
  169.         }
  170.     }
  171.  
  172.     int minscale = handEdgesVerticalPixels[0][0]+handEdgesHorizontalPixels[0][0],
  173.         maxscale = handEdgesVerticalPixels[0][0]+handEdgesHorizontalPixels[0][0];
  174.  
  175.     for(int i=0; i < handEdgesVerticalPixels.rows; i++)
  176.     {
  177.         for(int j = 0; j < handEdgesVerticalPixels.cols; j++)
  178.         {
  179.             if(handEdgesVerticalPixels[i][j] + handEdgesHorizontalPixels[i][j] > maxscale)
  180.                 maxscale = handEdgesVerticalPixels[i][j] + handEdgesHorizontalPixels[i][j];
  181.             if(handEdgesVerticalPixels[i][j] + handEdgesHorizontalPixels[i][j] < minscale)
  182.                 minscale = handEdgesVerticalPixels[i][j] + handEdgesHorizontalPixels[i][j];
  183.         }
  184.     }
  185.  
  186.     Mat_<int> mat = handEdgesVertical;
  187.     for(int y=0;y< handEdgesVerticalPixels.rows; y++)
  188.     {
  189.         for(int x = 0; x < handEdgesVerticalPixels.cols; x++)
  190.         {
  191.             mat[y][x]=((handEdgesHorizontalPixels[y][x]+handEdgesVerticalPixels[y][x]-minscale)*255)/(maxscale-minscale);
  192.         }
  193.     }
  194.  
  195.  
  196.     handEdgesMergedPixels = handEdgesHorizontalPixels;
  197. }
Add Comment
Please, Sign In to add comment