Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C++
Tip/Trick

Detect Amount of Arc

Rate me:
Please Sign up or sign in to vote.
3.71/5 (3 votes)
11 Dec 2013CPOL1 min read 14K   396   4   3
With this algorithm, we can detect amount and knoll of an arc

Introduction

Today, Image processing does a lot of work and can be applied in every subject such as medic, tumor detection, driving, speed getting, traffic analyse, edge detection, background extraction, text extraction, ...

With this algorithm, we can find knoll and get amount of arc of a line in every picture easily.

Using the Code And Algorithm

First of all, we must load the image from hard to program. We do it with CImage Object.

C++
CImage i;
i.Load(_T("1.png"));

After that, we should grey the image for more easy processing. Working with two colors, black and white. We put black color where RGB is less than 80 and put white where RGB is more than 80 value. To convert the image to gray-mode:

C++
CDC cdc;//declaration an object from Class Device Content
cdc.Attach(i.GetDC());//get Device Content from CImage (i)
COLORREF c,c1;//define two color
long r,g,b;//define red ,green, blue values
int avg;
for(int y=0;y<i.GetHeight();y++)
	for(int x=0;x<i.GetWidth();x++){
	c=cdc.GetPixel(x,y);//get color from image
//get RGB
r=GetRValue(c); g=GetGValue(c);  b=GetBValue(c); avg=(r+g+b)/3;
	if(avg>80)r=255;
	else r=0;
	cdc.SetPixel(x,y,RGB(r,r,r));//put color to image
}
cdc.Detach();
i.ReleaseDC();

The next step is arc detection. The algorithm is:

First we look for first pixel that is not the back color (white) and call Point 1 (x1,y1). Then continue. Next pixel that has this trait, if it’s distance from pre point, it belongs to arc, else not. Keep on until we won't find the pixel that belongs to arc then last point we call Point 2(x2,y2). Point 3(x3,y3) is knoll of arc. Put (x3,y3) to pixel that has higher height.

C++
CDC cdc;
cdc.Attach(i.GetDC());
CPen p(PS_SOLID,1,RGB(255,255,0)),p1(PS_SOLID,1,RGB(0,0,255)),p2(PS_DASH,1,RGB(255,0,0));
cdc.SelectObject(&p);
register int x1=-1,x2,x3,y1,y2,y3,d=10;x1=x2=x3=y1=y2=y3=-1;
register COLORREF c;
for(register int x=0;x<i.GetWidth();x++)
	
for(register int y=0;y<i.GetHeight();y++){
	
c=i.GetPixel(x,y);
	if(c!=RGB(255,255,255)){//line detect
	if(x1==-1){//first point of line
	x1=x2=x3=x;y1=y2=y3=y;
	}else if(sqrt(pow(x-x2,2.0)+pow(y-y2,2.0))<=d){//next point
	x2=x;y2=y;
	if(abs(y2-y1)>=abs(y3-y1)){
	x3=x2;y3=y2;
	}
	cdc.SetPixel(x,y,RGB(255,255,0));
	}
	}
	}
register float length=sqrt(pow(x1-x2,2.0)+pow(y1-y2,2.0));
cdc.SelectObject(&p1);
cdc.MoveTo(x3,y1);cdc.LineTo(x3,y3);
cdc.SelectObject(&p2);cdc.MoveTo(x1,y1);cdc.LineTo(x2,y2);
cdc.Detach();i.ReleaseDC();OnPaint();
float arc=4000*abs(y3-y2)/length;
CString s;char a[20]="";itoa(arc,a,10);s=a;s+=_T(" mM");	

1. Original Image

2. Grey image

3. Processed image

The Solution And Points

For detecting the knoll of an arc without its function is difficult and we must use digital analyse to do this. But with image processing and this algorithm, we can do it easily and fast. It's useful.

Writer: Mahdi Nejadsahebi - mahdiacuransx@yahoo.com

License

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


Written By
Software Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
YvesDaoust15-Dec-13 23:32
YvesDaoust15-Dec-13 23:32 
GeneralRe: My vote of 1 Pin
Mahdi Nejadsahebi19-Dec-13 20:54
Mahdi Nejadsahebi19-Dec-13 20:54 
QuestionWhat is this doing ? Pin
YvesDaoust15-Dec-13 23:31
YvesDaoust15-Dec-13 23:31 

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.