Wednesday, 6 April 2016

Opencv C++ Code For Detecting Lines Inclined at plus 45 degree

This OpenCV C++ Tutorial is about Slant Line Detection i.e. detecting lines inclined at plus 45 degrees
To detect slant lines inclined at +45 degress, we use the mask:
-1-12
-12-1
2-1-1

Thus sliding this mask over an Image we can detect Lines inclined at +45 degrees.
Here is the Opencv C++ Example of Slant Line (lines/edges at 45 degrees)Detection below:

//Opencv C++ Code for detecting line inclined at 45 degree
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "iostream"
 
using namespace cv;
using namespace std;
 
int main( )
{
    Mat src1,src2;
 int a;
 Scalar intensity1=0;
    src1 = imread("C:\\Users\\arjun\\Desktop\\opencv-test.jpg", CV_LOAD_IMAGE_GRAYSCALE);
 src2 = src1.clone();

 //If image not found 
 if (!src1.data)                                                                          
     {  
      cout << "No image data \n";  
      return -1;  
     } 

  cout<<"Enter the mask Size =";
  cin>>a;

 //for loop for counting the number of rows and columns and displaying the pixel value at each point
   for (int i = 0; i < src1.rows-a; i++) { 
    for (int j = 0; j < src1.cols-a; j++) { 
   Scalar intensity2=0;
   for (int p = 0; p<a; p++) { 
     for (int q = 0; q<a; q++){ 
    intensity1 = src1.at<uchar>(i+p,j+q); 
    if( (p+q==(a-1)))
      {
    intensity2.val[0] +=(a-1)*intensity1.val[0];
      }
       else
      {
    intensity2.val[0] +=(-1)*intensity1.val[0];
      }
   
     }
  
  }
        src2.at<uchar>(i+(a-1)/2,j+(a-1)/2)=intensity2.val[0]/(a*a);
   } 
    
  }
 //Display the original image
 namedWindow("Display Image");                
 imshow("Display Image", src1); 

 //Display the Low Pass Filtered Image image
    namedWindow("Line Detection Image");     
 imshow("Line Detection Image", src2);  
    imwrite("C:\\Users\\arjun\\Desktop\\opencv-slant-line.jpg",src2);
 waitKey(0);
 return 0;
 }

Input:

OpenCV C++ Line Detection Input
Output:

OpenCV C++ Slant Line Detection Output

No comments:

Post a Comment