Friday , December 15 2017
Home / opencv / C++ / How to do line detection in opencv C++

How to do line detection in opencv C++

(Last Updated On: October 11, 2017)

First of all we include few OpenCV libraries after configuration. After libraries include one must and main thing is Mat objects creation. Mat is two dimensional array one contain header pointer and second pixel value. Size of the Mat depends on the size of image or frame. Mat are created simple Mat word and object name.

Mat input, dest, cdest;

After that we make string for image path we can pass path string directly to imread function. As in the above example in input. empty () is true it means no frame fetched so we will proceed further because frame fetching is main and primary task. After frames we apply canny edge detection to fetched frames. Canny edge detection give us the structural information of the different type of objects found in frame. Canny consider an edge from frame when it gets a color difference in accumulator array for specific range. Canny Edge detection has following main properties:

  • It detects edges with a very low error rate which means a edge detected by this technique is 99% is edge.
  • Edge points detected by canny edge detection method are mostly real and accurate edges.
  • Noise ratio in canny edge detection is almost zero so we get true edges.

Canny edge detection function in OpenCV take 5 parameters. First parameter is frames input which is always a Mat object. Second parameter is also Mat object which is out and save the output edges. The next two 3rd and 4th parameters are about threshold value tell minimum and maximum thresh value. The last parameter used in canny edge detection is kernel size which is always 3 because we use sobel kernel. After edges extraction we create a vector object for line store and calculations.

After frame and edges extraction main thing is Hough line transform use. HoughLines function in OpenCV take 6 main arguments. First is output of canny edge function here taken as input , the second parameter is vector object lines that will store line parameter angle (theta) and value of r of the detected lines. Both the theta and r are obtained by setting double array value 0, 1. At 0 we get value of rho which is resolution parameter tells us the scene resolution. Normally in Hough method 1 pixel resolution is used. Next and fourth main argument for Hough method is value of theta which is also 1 degree by default. Threshold value is the next main argument for Hough line transform.

Last step in line detection is drawing of lines in scene. So after Hough line detection we get a lot of lines from scene from which we select few ones of our need based on the value of rho and theta. Line function is used for Line drawing which gets first output of Hough method as input to points of type Point, color value for line. These lines are shown in an infinite loop dependent on size of incoming line.

 

Code:

#include <stdio.h>

#include <stdlib.h>

#include "cv.h"

#include "highgui.h"

#include <iostream>

#include <vector>

#include<conio.h>

using namespace cv;

using namespace std;

int main()

{

Mat input, dest, cdest;

String file_name ;

input = imread("imagepath", CV_LOAD_IMAGE_COLOR);

if(input.empty())

{

cout << "ERROR IN FRAME GET”;

}

Canny(input, dest, 50, 200, 3);

cvtColor(dest, cdest, CV_GRAY2BGR);

vector<Vec2f> lines;

HoughLines( dest, lines, 3, CV_PI/180, 100, 0, 0 );

for( size_t i = 0; i < lines. size(); i++ )

{

float rho = lines[i][0];

float theta = lines[i][1];

Point point1, point2;

double cs = cos(theta), sn = sin(theta);

double  d1= cs*rho, d2 = sn*rho;

pointt1.x = cvRound(d1 + 1000*(-sn));

point1.y = cvRound(d2 + 1000*(cs));

point2.x = cvRound(d1 - 1000*(-sn));

point2.y = cvRound(d2 - 1000*(cs));

line( cdst, point1, point2, Scalar(0,0,255), 3, CV_AA);

}

imshow("Orignal frames", input);

imshow("detected lines on frames", cdest);

return 0;

}

About Admin

Check Also

How to use Opencv with Android

(Last Updated On: October 8, 2017)OpenCV is Open sourced computer vision library. It is machine …

One comment

  1. Thank you so much that really saved me !!!

Leave a Reply

Your email address will not be published. Required fields are marked *