Thursday, 7 April 2016

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

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

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:

Here is the Opencv C++ Example of Slant Line (lines/edges at 45 degrees)Detection below:
//Opencv C++ Code for detecting line inclined at minus 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;  
     } 

 //Take the Size of Mask
  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)
   {
    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("Low Pass Filtered Image");     
  imshow("Low Pass Filtered 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