Click here to Skip to main content
13,866,049 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


62 bookmarked
Posted 10 Nov 2011
Licenced CPOL

Detect the Angle of a Line in an Image

, 15 Dec 2012
Rate this:
Please Sign up or sign in to vote.
How to detect the angle of a line in an image


Today's image processing technology helps us in many areas such as determining tumors, text extraction, checking images,... In this algorithm, we can get the angle of a line in an image. Of course, there are several ways to do this with advanced algorithms, but this is a simple algorithm.


  • Works in real images with several colors
  • Simple to implement
  • Gets the length of the line
  • Very light-weight code

Source Code

I have commented the code clearly. First, we must define a global CImage and then load the image file from the hard disk:

CImage i;

After that, we need to draw an image when cleared with the OnPaint() function:

void CDetectangleoflineDlg::OnPaint()
  if(!i.IsNull()){//image isn't null
    //get handle device content from this dialog
    CClientDC dc(this);
    //draw picture in dialog background

Next, we load the image:

void CDetectangleoflineDlg::OnBnClickedButton1()//load image
    CFileDialog f(1);//to call open file dialog
    TCHAR tc[260];
    CString s;
    if(f.DoModal()==IDOK){//image selected
        if(!i.IsNull())//image isn't null
            i.Destroy();//destroy image
              //load image to i (CImage)
        OnPaint();//call draw image

Finally, we use the Process algorithm to determine the angle:

void CDetectangleoflineDlg::OnBnClickedButton2()//process
    CImage i1;
    i1=i;//copy 'i' to 'i1'
    CDC cdc;//to get device content of 'i1'
    //get device content of i to cdc to draw or getbitmap
    //define pens and choose them in 'cdc'
    CPen p(PS_SOLID,1,RGB(255,255,0)),p1(PS_SOLID,1,RGB(0,0,255));
    //define necessary variables
    register int x1,x2,y1,y2;
    double pi=3.1415926535;
    register long r,g,b,r1,g1,b1;
    register COLORREF c,c1;
    //'tol':toleranc;defference between colors.
    //'d':the most width of line
    register int tol=30,d=20;
    //get first color as background color
    bool ar[600][450];
    //clear the array
    for(register int y=0;y<i.GetHeight();y++)for(register int x=0;x<i.GetWidth();x++)
    //convert the image to grey status
    for(register int y=0;y<i.GetHeight();y++)
	for(register int x=0;x<i.GetWidth();x++){
		else ar[y][x]=0;
    //process to detect the arc
    for(register int y=0;y<i.GetHeight();y++)
	for(register int x=0;x<i.GetWidth();x++){
		if(ar[y][x]){//line detect
			if(x1==-1){//first point of line
			}else if(sqrt(pow(x-x2,2.0)+pow(y-y2,2.0))<=d){//next point
			}else ar[y][x]=0;
    //draw the detected arc
    for(register int y=0;y<i.GetHeight();y++)
	for(register int x=0;x<i.GetWidth();x++)

    //compute line's length width: r^2=x^2+y^2
    register float radius=sqrt(pow(x1-x2,2.0)+pow(y1-y2,2.0));
    //end:to get angle of line
    float t=atan((float)(y2-y1)/(x2-x1))*180/pi;
    cdc.SelectObject(&p1);//set 'p1' for blue color
    //draw lines around Line
    CClientDC dc(this);//get device content of dialog to draw 'i1'
    if(t>=-180&&t<=180){//to skip error
        //convert 't'int to 's'CString
        char a[10]="";
        CString s;
        dc.TextOut(0,300,s+_T(" degree"));
        //print the result
    i1.BitBlt(dc.m_hDC,i.GetWidth()+10,0,SRCCOPY);//draw 'i1'


First, we need a color for the backcolor, 'c', and a color to get the points of the image, 'c1'. We have to find the first and last points of the line (x1, y1) and (x2, y2). We set -1 as the default value of x1. This means we don't find the first point yet. For detecting the first point, we move and look around the image at (x, y). When c1!=c && x==-1, we find (x1, y1). We imagine a virtual radius like 'd' and with a default value 10. After that, we move and look again, and when c!=c && sqrt((x-x2)^2+(y-y2)^2))<=d, this point is related to the previous point and line. We continue this till the end. Finally, we'll find (x1,y2),(x2,y2) and we can get the length of the line and get the angle of the line with arctan.



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

You may also be interested in...

Comments and Discussions

QuestionMy vote of 4 Pin
Manikandan1011-Jun-14 17:48
professionalManikandan1011-Jun-14 17:48 
GeneralMy vote of 2 Pin
Thomas Willwacher8-Nov-12 23:50
memberThomas Willwacher8-Nov-12 23:50 
GeneralMy vote of 3 Pin
SergV-SNV11-Oct-12 20:48
memberSergV-SNV11-Oct-12 20:48 
GeneralMy vote of 5 Pin
Mihai MOGA18-Dec-11 4:41
professionalMihai MOGA18-Dec-11 4:41 
GeneralRe: My vote of 5 Pin
Mahdi Nejadsahebi24-Mar-12 5:47
memberMahdi Nejadsahebi24-Mar-12 5:47 
GeneralRe: My vote of 5 Pin
Mahdi Nejadsahebi17-Nov-12 9:42
memberMahdi Nejadsahebi17-Nov-12 9:42 
GeneralMy vote of 2 Pin
nv315-Nov-11 23:08
membernv315-Nov-11 23:08 
QuestionProblem in the sample OnPaint code Pin
Mukit, Ataul15-Nov-11 0:54
memberMukit, Ataul15-Nov-11 0:54 
AnswerRe: Problem in the sample OnPaint code Pin
Mukit, Ataul19-Nov-11 17:53
memberMukit, Ataul19-Nov-11 17:53 
GeneralRe: Problem in the sample OnPaint code Pin
Mahdi Nejadsahebi24-Mar-12 5:50
memberMahdi Nejadsahebi24-Mar-12 5:50 
GeneralRe: Problem in the sample OnPaint code Pin
Mukit, Ataul24-Mar-12 17:27
memberMukit, Ataul24-Mar-12 17:27 
SuggestionFormatting concern Pin
David Crow14-Nov-11 11:40
memberDavid Crow14-Nov-11 11:40 
GeneralRe: Formatting concern Pin
Mahdi Nejadsahebi24-Mar-12 5:51
memberMahdi Nejadsahebi24-Mar-12 5:51 
GeneralRe: Formatting concern Pin
Mahdi Nejadsahebi10-Oct-12 3:09
memberMahdi Nejadsahebi10-Oct-12 3:09 
GeneralMy vote of 5 Pin
Kanasz Robert10-Nov-11 23:54
memberKanasz Robert10-Nov-11 23:54 
GeneralRe: My vote of 5 Pin
Mahdi Nejadsahebi11-Nov-11 20:48
memberMahdi Nejadsahebi11-Nov-11 20:48 
GeneralRe: My vote of 5 Pin
Mahdi Nejadsahebi10-Oct-12 3:02
memberMahdi Nejadsahebi10-Oct-12 3:02 
GeneralMy vote of 5 Pin
Sergio Andrés Gutiérrez Rojas10-Nov-11 17:36
memberSergio Andrés Gutiérrez Rojas10-Nov-11 17:36 
GeneralRe: My vote of 5 Pin
Mahdi Nejadsahebi11-Nov-11 20:51
memberMahdi Nejadsahebi11-Nov-11 20:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190214.1 | Last Updated 15 Dec 2012
Article Copyright 2011 by Mahdi Nejadsahebi
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid