Advertisement
Guest User

Untitled

a guest
Oct 7th, 2015
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.10 KB | None | 0 0
  1. #include<opencv2/imgproc/imgproc.hpp>  
  2. #include<opencv2/highgui/highgui.hpp>  
  3.  
  4. using namespace std;  
  5. using namespace cv;  
  6.  
  7. // To compute edge detection in an image, we are in need of gradients that include the Sobel kernel values. These can be created as functions taking  
  8. // arguements of x and y values. Those values need to come from an image, therefore we need a Matrix image - Mat  
  9.  
  10. int xGradient(Mat image, int x, int y)  
  11. {  
  12.     return  
  13.         image.at<uchar>(y - 1, x - 1) +  
  14.         2 * image.at<uchar>(y, x - 1) +  
  15.         image.at<uchar>(y + 1, x - 1) -  
  16.         image.at<uchar>(y - 1, x + 1) -  
  17.         2 * image.at<uchar>(y, x + 1) -  
  18.         image.at<uchar>(y + 1, x + 1);  
  19. }  
  20.  
  21. int yGradient(Mat image, int x, int y)  
  22. {  
  23.     return  
  24.         image.at<uchar>(y - 1, x - 1) +  
  25.         2 * image.at<uchar>(y - 1, x) +  
  26.         image.at<uchar>(y - 1, x + 1) -  
  27.         image.at<uchar>(y + 1, x - 1) -  
  28.         2 * image.at<uchar>(y + 1, x) -  
  29.         image.at<uchar>(y + 1, x + 1);  
  30. }  
  31.  
  32. int main()  
  33. {  
  34.     // In order to load images to the program, we need variables of the type Mat  
  35.     // For this code we create two Mat (Matrix) variables to load store the source image into, as well as a cloned image we'll use to  
  36.     // perform computations in the program.  
  37.  
  38.     // Calculating the final image  
  39.     // To get the final image at the end, we need to add the absolute value of the two gradients together.  
  40.     // To do this, we create two variables which we can call the gradient functions we create at the top.  
  41.     // Since we're adding it together, we get a sum. Therefore we also need to store the sum in a variable, which will be of type Int  
  42.  
  43.     Mat src, clone;  
  44.     int gradientXAxis, gradientYAxis;  
  45.     int sum = 0;  
  46.  
  47.     // Load an image  
  48.     // Initialize the src variable to the source image we wish to work with, which takes parameters of the system path to the image  
  49.     // Second variable is which color spectrum the image should be loaded into. 0 = Greyscale image. 1 = Colour image.  
  50.     // We want a greyscale image for this project, therefore the int is set to 0. It could also be initialized to CV_LOAD_IMAGE_GRAYSCALE  
  51.     // Which does the same  
  52.  
  53.     // Cloning image  
  54.     // If we wish to show the source image and the edge enhanced image, we clone the source image to a new Mat variable, which clones all the  
  55.     // information. The clone we can then perform the desired actions and show the edge enhanced image. That's why we initialize the clone image to  
  56.     // a clone of the source image  
  57.  
  58.     src = imread("C:/Pictures/building.jpg", 0);  
  59.     clone = src.clone();  
  60.     imshow("initial", src);  
  61.  
  62.     // Checks if data is present and it is not empty - Then execute the code.  
  63.     // Executes two for loops to access every pixel in the image  
  64.     if (src.data && !src.empty())  
  65.     {  
  66.         for (int y = 1; y < src.rows - 1; y++){  
  67.             for (int x = 1; x < src.cols - 1; x++){  
  68.  
  69.                 // Initialize gradientXAxis to the xGradient function values  
  70.                 gradientXAxis = xGradient(src, x, y);  
  71.  
  72.                 // Initialize gradientYAxis to the yGradient function values  
  73.                 gradientYAxis = yGradient(src, x, y);  
  74.  
  75.                 // Calculate absolute value of both gradient functions store it to the sum variable  
  76.                 sum = abs(gradientXAxis) + abs(gradientYAxis);  
  77.  
  78.                 // If statement to threshold the image, which we'll need to make it a binary image with only black and white values,  
  79.                 // where the edges are white and the rest is black  
  80.                 // If the sum is equal to or above 100, set the pixel value to 255. Else, set pixel value to 0, or black  
  81.                 if (sum >= 125) {  
  82.                     clone.at<uchar>(y, x) = 255;  
  83.                 }  
  84.                 else {  
  85.                     clone.at<uchar>(y, x) = 0;  
  86.                 }  
  87.             }  
  88.         }  
  89.     }  
  90.  
  91.     imshow("final", clone);  
  92.  
  93.     waitKey(0);  
  94.  
  95.     return 0;  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement