# Hough Lines & Canny Edge Detection in OpenCV [Code]

> HoughLines, Canny edges, for OpenCV line detection edges detection in symple described C++ code, where all the steps are visualize and exmplayn.

- Canonical URL: https://www.funvisiontutorials.com/blog/hough-lines-canny-edge-detection-opencv-code/
- Author: Vladimir Kucera
- Published: 2016-01-18
- Updated: 2026-02-20
- Topics: Object detection, Image processing, Installing & building OpenCV

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](/blog/opencv-31-opencv-2413-instalation-visual-studio/)

[Image: HoughLines hough lines]

###

## Sobel derivatives

Sobel derivatives are the convolution of image parts with the kernel that represent Sobel derivative approximation. The upper image is our Sobel kernel. Simple 3 x 3 matrices with these parameters. This configuration 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](https://en.wikipedia.org/wiki/Kernel_\(image_processing\))

Use this kernel with  3x3 image part 1. This image matrix has constant values 1. There is no edges in x direction. The number generated by convolution is 0. If you convolve the kernel with the 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.

[Image: Sobel derivatives convolution]

## Canny edges, Sobel and Hough lines code

**#include &lt;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_

[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);**

[Image: 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);**

[Image: sobel derivatives]

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

[Image: sobel derivatives]

   **waitKey(10000);**

    **vector&lt;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 &lt; 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);**

[Image: Hough lines]

**waitKey(10000);**

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

[Image: Hough lines]

**waitKey(10000);**

**}**
