Hough Transform

Przestrzeń Hough

#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("./2.png", 1);
 Mat img_grey = img.clone();
 cvtColor(img, img_grey, CV_BGR2GRAY);

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

 int height = (int)(sqrt(pow(img.cols, 2) + pow(img.rows, 2)));
 Mat img_hough_space(2*height, 180, CV_8UC3, Scalar(0, 0, 0));

 for (int y = 0; y(x, y);
   if (pixelValue > 0)
   {
    for (int m = 0; m < 540; m++)
    {
     int r = (int) (x*cos((m*PI)/180) + y*sin((m*PI)/180) );     
     if (r + height > 0){
      img_grey.at<uchar>(x, y) = 255;
      img_hough_space.at<uchar>(r + height, m) = img_hough_space.at<uchar>(r + height, m) + 1;
     }    
    }   
   }
  }
 }
 imshow("Canny", img_grey);
 imshow("Image", binary_edges);
 resize(img_hough_space, img_hough_space, Size(binary_edges.size().width, binary_edges.size().height));
 imshow("Hough space", img_hough_space);
 waitKey(0);
 return 0;
}

Wykrywanie prostych Hough Transform

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

using namespace std;
using namespace cv;
#define PI 3.14159265
int main(){
 Mat img = imread("./2.png", 1);
 Mat img_hough = img.clone();
 cvtColor(img, img, CV_BGR2GRAY);

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

 imshow("Image", img);
 imshow("Canny", binary_edges);

 vector<Vec2f> hough_lines;
 HoughLines(binary_edges, hough_lines, 1, PI / 200.0, 150);
 for (size_t i = 0; i < hough_lines.size(); i++){
  float rho = hough_lines[i][0], theta = hough_lines[i][1];
  Point pt1, pt2;
  double a = cos(theta), b = sin(theta);
  double x0 = a*rho, y0 = b*rho;
  pt1.x = cvRound(x0 + 1000 * (-b));
  pt1.y = cvRound(y0 + 1000 * (a));
  pt2.x = cvRound(x0 - 1000 * (-b));
  pt2.y = cvRound(y0 - 1000 * (a));
  line(img_hough, pt1, pt2, Scalar(0, 0, 255), 1, CV_AA);
 }
 imshow("Hough image", img_hough);
 waitKey(0);

 return 0;
}

Wykrywanie okręgów Hough Transform

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

using namespace std;
using namespace cv;
#define PI 3.14159265
int main(){
  Mat img = imread("./3.png", 1);
  Mat img_hough = img.clone();
  cvtColor(img, img, CV_BGR2GRAY);

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


  imshow("Image", img);
  imshow("Canny", binary_edges);

  vector<Vec3f> circles;
  HoughCircles(binary_edges, circles, CV_HOUGH_GRADIENT, 2, 20, 500, 20, 30, 33);

  for (size_t i = 0; i < circles.size(); i++){
    Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
    int radius = cvRound(circles[i][2]);
    // draw the circle center
    circle(img_hough, center, 3, Scalar(0, 255, 0), -1, 8, 0);
    // draw the circle outline
    circle(img_hough, center, radius, Scalar(0, 0, 255), 3, 8, 0);
  }
  imshow("Hough image", img_hough);
  waitKey(0);
  return 0;
}