Share
Visit

Monday, January 18, 2016

Hough lines and canny edge, Sobel derivatives Opencv Tutorial

Hough lines, Canny edges and Sobel derivatives

HoughLines, Canny edges, for OpenCV line detection  edges detection in symple described C++ code, where all the steps are visualize and exmplayn. In this tutorial is used Visual studio 2015 instalation by nuget packages. Easy and fast without usual problems with version, dll, and environmental vatiables. Check this tutorial here

HoughLines hough lines


Sobel derivatives

Sobel derivatives is convolution of image parts with kernel that represent sobel derivative approximation. The upper image is our sobel kernel. Simple 3 x 3 matrices with this parameters. This configurations can detect edges or changes which is vertically oriented. How? 
Convolution of source image 3x3 part with this kernel generates a number.

Kernel Convolution wiki

Use this kernel with  3x3 image part 1. This image matrices has constant values 1. There is no edges in x direction. Number generates by convolution is 0. If you convolve kernel with image part 2. There is edges in x direction from 1 to 5. Convolution of the same kernel with this part generates number 16. See the example.

Try to think how simple is this in all directions. 


Sobel derivatives convolution


Canny edges, sobel and hough lines code



#include <Windows.h>
#include "opencv2\highgui.hpp"
#include "opencv2\imgproc.hpp"
#include "opencv2/imgcodecs/imgcodecs.hpp"
#include "opencv2/videoio/videoio.hpp"

using namespace cv;
using namespace std;

int main(int argc, const char** argv)
{

Mat image;


// Load an image
canny hough lines

image = imread("1.jpg", 0);
resize(image, image, Size(800, 600));

cv::Mat edges;

// Canny edge 
cv::Canny(image, edges, 95, 100);


imwrite("edges.jpg", edges);
imshow("Canny edges", edges);
Canny edges
waitKey(10);

cv::Mat dx, dy;

// sobel derivative approximation X direction of edges image
cv::Sobel(edges, dx, CV_32F, 1, 0);

// sobel derivative approximation Y direction of edges image
cv::Sobel(edges, dy, CV_32F, 0, 1);

imwrite("dx.jpg", dx);
imshow("Sobel in x dirrection", dx);
sobel derivatives
waitKey(10000);

imwrite("dy.jpg", dy);
imshow("Sobel in y dirrection", dy);
sobel derivatives
   waitKey(10000);



    vector<Vec4i> lines;
           // Find hough lines 
   HoughLinesP(edges, lines, 1, CV_PI / 180, 100, 100, 10);

           // Prepare blank mat with same sizes as image
   Mat Blank(image.rows, image.cols, CV_8UC3, Scalar(0, 0, 0));

       // Draw lines into image and Blank images
       for (size_t i = 0; i < lines.size(); i++)
        {
      Vec4i l = lines[i];

      line(image, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0, 0, 0), 2, CV_AA);

      line(Blank, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(255, 255, 255), 2, CV_AA);

        }



imwrite("houg.jpg", image);
imshow("Edges", image);

Hough lines

waitKey(10000);

imwrite("houg2.jpg", Blank);
imshow("Edges Structure", Blank);

Hough lines
waitKey(10000);


}

1 comment:

ad