Wykrywanie krawędzi

Krzyż Robertsa

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;
int main(){
  Mat img = imread("./house.png", 1 ); 
  cvtColor(img, img, CV_BGR2GRAY);
  Mat result_img_1;
  Mat result_img_2;
  Mat result_img_3;
  
  Mat kernel_1 = (Mat_(2,2) << 1, 0, 0, -1);
  Mat kernel_2 = (Mat_(2,2) << 0, -1, 1, 0);
  cout<<kernel_1<<endl;
  cout<<kernel_2<<endl;

  filter2D(img, result_img_1, -1, kernel_1);
  filter2D(img, result_img_2, -1 , kernel_2);

  result_img_3 = abs(result_img_1)+abs(result_img_2);

  imshow( "Image", img);
  imshow( "Result 1", result_img_1);
  imshow( "Result 2", result_img_2);
  imshow( "Result 3", result_img_3);
  waitKey(0);
  return 0;  
}  

Prewitt

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;
int main(){
  Mat img = imread("./house.png", 1 ); 
  cvtColor(img, img, CV_BGR2GRAY);
  Mat result_img_1;
  Mat result_img_2;
  Mat result_img_3;
  
  Mat kernel_1 = (Mat_(3,3) < 1, 1, 1, 0, 0, 0, -1, -1, -1);
  Mat kernel_2 = (Mat_(3,3) < -1, 0, 1, -1, 0, 1, -1, 0, 1);
  cout<<kernel_1<<endl;
  cout<<kernel_2<<endl;

  filter2D(img, result_img_1, -1, kernel_1);
  filter2D(img, result_img_2, -1 , kernel_2);

  result_img_3 = abs(result_img_1)+abs(result_img_2);

  imshow( "Image", img);
  imshow( "Result 1", result_img_1);
  imshow( "Result 2", result_img_2);
  imshow( "Result 3", result_img_3);
  waitKey(0);
  return 0;  
}  

Sobol

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;
int main(){
  Mat img = imread("./house.png", 1 ); 
  cvtColor(img, img, CV_BGR2GRAY);
  Mat result_img_1;
  Mat result_img_2;
  Mat result_img_3;
  
  Sobel( img, result_img_1, -1 ,1 ,0 );
  Sobel( img, result_img_2, -1 ,0 ,1 );
  result_img_3 = abs(result_img_1)+abs(result_img_2);

  imshow( "Image", img);
  imshow( "Result 1", result_img_1);
  imshow( "Result 2", result_img_2);
  imshow( "Result 3", result_img_3);
  waitKey(0);
  return 0;  
}  

Filtr Laplacea

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;
int main(){
  Mat img = imread("./house.png", 1 ); 
  cvtColor(img, img, CV_BGR2GRAY);
  Mat result_img_1;
  Mat result_img_2;
  
  Mat kernel_1 = (Mat_(3,3) lt;< 0, 1, 0, 1, -4, 1, 0, 1, 0);
  Mat kernel_2 = (Mat_(3,3) lt;< 1, 1, 1, 1, -8, 1, 1, 1, 1);
  cout<<kernel_1<<endl;
  cout<<kernel_2<<endl;

  filter2D(img, result_img_1, -1, kernel_1);
  filter2D(img, result_img_2, -1 , kernel_2);

  imshow( "Image", img);
  imshow( "Result 1", result_img_1);
  imshow( "Result 2", result_img_2);
  waitKey(0);
  return 0;  
}  

Filtr Laplacea z rozmyciem Gaussa

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;
int main(){
  Mat img = imread("./house.png", 1 ); 
  cvtColor(img, img, CV_BGR2GRAY);

  Mat laplacian;
  Mat blurred_img;
  GaussianBlur(img, blurred_img, Size (5,5), 1.5 );
  Laplacian(blurred_img, laplacian, -1, 3 );

  
  imshow( "Image", img);
  imshow( "Result blur", blurred_img);
  imshow( "Result laplacian", laplacian);
  waitKey(0);
  return 0;  
}  

Metoda detekcji krawędzi - Canny

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;
int main(){
  Mat img = imread("./house.png", 1 ); 
  cvtColor(img, img, CV_BGR2GRAY);

  Mat binary_edges;
  Canny(img, binary_edges, 100, 200 );

  
  imshow( "Image", img);
  imshow( "Canny", binary_edges);
  waitKey(0);
  return 0;  
}