Advertisement
Guest User

directional edge detection

a guest
May 29th, 2014
651
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.65 KB | None | 0 0
  1. #include <opencv2/core/core.hpp>
  2. #include <opencv2/highgui/highgui.hpp>
  3. #include <opencv2/imgproc/imgproc.hpp>
  4.  
  5. #include <iostream>
  6. #include <vector>
  7.  
  8. using namespace cv;
  9. using namespace std;
  10.  
  11. RNG rng(12345);
  12.  
  13. Mat mat2gray(const cv::Mat& src)
  14. {
  15.     Mat dst;
  16.     normalize(src, dst, 0.0, 255.0, cv::NORM_MINMAX, CV_8U);
  17.  
  18.     return dst;
  19. }
  20.  
  21. Mat oriEdge(Mat mag, Mat ori, double magThres, double oriThres)
  22. {
  23.     Mat Edge = Mat::zeros(ori.size(), CV_8UC1);
  24.  
  25.     double tol=0.1;
  26.     uchar val;
  27.  
  28.     for (int i=0; i<Edge.rows; i++)
  29.         for (int j=0; j<Edge.cols; j++)
  30.         {
  31.             if ((abs(ori.at<float>(i,j)-oriThres)<tol)&&(mag.at<float>(i,j)>=magThres))
  32.             {
  33.                 Edge.at<uchar>(i,j)=255;
  34.             }
  35.         }
  36.  
  37.     return Edge;
  38. }
  39.  
  40. Mat orientationMap(const cv::Mat& mag, const cv::Mat& ori, double thresh = 1.0)
  41. {
  42.     Mat oriMap = Mat::zeros(ori.size(), CV_8UC3);
  43.     Vec3b red(0, 0, 255);
  44.     Vec3b cyan(255, 255, 0);
  45.     Vec3b green(0, 255, 0);
  46.     Vec3b yellow(0, 255, 255);
  47.     for(int i = 0; i < mag.rows*mag.cols; i++)
  48.     {
  49.         float* magPixel = reinterpret_cast<float*>(mag.data + i*sizeof(float));
  50.         if(*magPixel > thresh)
  51.         {
  52.             float* oriPixel = reinterpret_cast<float*>(ori.data + i*sizeof(float));
  53.             Vec3b* mapPixel = reinterpret_cast<Vec3b*>(oriMap.data + i*3*sizeof(char));
  54.             if(*oriPixel < 90.0)
  55.                 *mapPixel = red;
  56.             else if(*oriPixel >= 90.0 && *oriPixel < 180.0)
  57.                 *mapPixel = cyan;
  58.             else if(*oriPixel >= 180.0 && *oriPixel < 270.0)
  59.                 *mapPixel = green;
  60.             else if(*oriPixel >= 270.0 && *oriPixel < 360.0)
  61.                 *mapPixel = yellow;
  62.         }
  63.     }
  64.  
  65.     return oriMap;
  66. }
  67.  
  68.  
  69.  
  70. int main(int argc, char* argv[])
  71. {
  72.  
  73.     VideoCapture cap(0); // open the default camera
  74.     if(!cap.isOpened())  // check if we succeeded
  75.         return -1;
  76.  
  77. //  Mat image = Mat::zeros(Size(320, 240), CV_8UC1);
  78.     Mat image;
  79.     Mat edges, Sx, Sy, mag, ori, oriMap;
  80.     Mat oriEdImg, linkedImg;
  81.     //namedWindow("edges",1);
  82.  
  83.     // contour vars
  84.     vector<vector<Point> > contours;
  85.     vector<Vec4i> hierarchy;
  86.  
  87.     for(;;)
  88.     {
  89.         Mat frame;
  90.         cap >> frame; // get a new frame from camera
  91.         cvtColor(frame, image, CV_BGR2GRAY);
  92.  
  93.  
  94.         //Mat image = Mat::zeros(Size(320, 240), CV_8UC1);
  95.         //circle(image, Point(160, 120), 80, Scalar(255, 255, 255), -1, CV_AA);
  96.  
  97.         imshow("original", image);
  98.  
  99.         //Mat Sx;
  100.         Sobel(image, Sx, CV_32F, 1, 0, 3);
  101.  
  102.         //Mat Sy;
  103.         Sobel(image, Sy, CV_32F, 0, 1, 3);
  104.  
  105.         //Mat mag, ori;
  106.         magnitude(Sx, Sy, mag);
  107.         phase(Sx, Sy, ori, false);
  108.  
  109.         //Mat oriMap = orientationMap(mag, ori, 1.0);
  110.         //oriMap = orientationMap(mag, ori, 1.0);
  111.  
  112.         oriEdImg = oriEdge(mag,ori,150,CV_PI/4);
  113.  
  114.         //linkedImg = linkedEdge(oriEdImg);
  115.         // find contours instead
  116.         /// Find contours RISK HERE!!!
  117.         findContours( oriEdImg, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
  118.        
  119.         /// Draw contours
  120.         linkedImg = Mat::zeros( oriEdImg.size(), CV_8UC3 );
  121.         for( int i = 0; i< contours.size(); i++ )
  122.         {
  123.             //Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
  124.             Scalar color = Scalar(255,255,255);
  125.             drawContours( linkedImg, contours, i, color, 2, 8, hierarchy, 0, Point() );
  126.         }  
  127.  
  128.         imshow("magnitude", mat2gray(mag));
  129.         imshow("orientation", mat2gray(ori));
  130.         imshow("Oriented Edge", oriEdImg);
  131.         imshow("Linked Edge", linkedImg);
  132.        
  133.  
  134.         //imshow("orientation map", oriMap);
  135.         //waitKey();
  136.  
  137.  
  138.         // to smooth out noise that affects edge detection
  139.         edges = image;
  140.         GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);  
  141.  
  142.         //Canny(edges, edges, 0, 200, 3);
  143.         Canny(edges, edges, 0, 150, 3);
  144.         imshow("edges", edges);
  145.         if(waitKey(30) >= 0) break;
  146.     }
  147.     // the camera will be deinitialized automatically in VideoCapture destructor
  148.     //return 0;
  149.  
  150.  
  151.     return 0;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement