Monday 4 April 2016

Opencv C++ Code For Vertical Line Detection

This OpenCV C++ Tutorial is about Vertical Line Detection i.e. How to Detect Vertical Edges or Lines in an Image
To detect Vertical Lines in an Image we use the mask:
-1-1-1
222
-1-1-1

Thus sliding this mask over an Image we can detect vertical Lines.
Note:- Sum of the Elements of the Mask is Zero
Here is the Opencv C++ Code with Example for Vertical Line/Edge Detection below:

//Opencv C++ Example for Detecting Vertical Lines
#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);
 //If image not found 
 if (!src1.data)                                                                          
     { cout << "No image data \n";  return -1; } 

 src2 = src1.clone();
 cout<<"Enter the mask Size(Preferably Enter Odd Number) =";
 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( (q==(a-1)/2))
   {
    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-example-vertical-edges.png",src2);
    waitKey(0);
    return 0;
    }
Input :

OpenCV C++ Vertical Line Detection Input

Output:

OpenCV C++Vertical Line Detection Output


For Horizontal Line Detection Refer:
Opencv C++ Code : Detecting Horizontal Line in an Image.

No comments:

Post a Comment