# Detect Line on image

I have a edge image containing only a line(i removed all noises). I used a mouse event to click on the one point of the line, then i apply a 3 by 3 window to check all the previous or followings points on the lines. The problem is for the crossing lines: the program detect the 2 ending points for the first line but for the second, it takes the meeting point of the 2 line as ending point of the second(The problem exist when my window has to choose in more than one direction). I don't know if it's clear, my english is not good enough. here is my code

void Temp(IplImage* img, int x, int y, CvPoint *endpoint1, CvPoint *endpoint2, int fs){// findind ending points function
bool up = true;
for (int i = -1; i <= 1; i++){
for (int j = -1; j <= 1; j++){
CvScalar v = cvGet2D(img, x + j,y + i);
if (v.val[0] >= 200 && up){ up = false;  p1.x = x + j; p1.y = y + i; }
else if (v.val[0] >= 200 && !up){ p2.x = x + j; p2.y = y + i; }
}
}

if (p1.x != x - 1 && p1.y != y - 1){
endpoint1->x = p1.x; endpoint1->y = p1.y;
Temp(img, p2.x, p2.y, endpoint1, endpoint2, 2);
return;
}
else if (p2.x != x + 1 && p2.y != y + 1){ endpoint2->x = p2.x; endpoint2->y = p2.y; return; }

if (fs == 1){ Temp(img, p1.x, p1.y, endpoint1, endpoint2, 1); return; }
else{ Temp(img, p2.x, p2.y, endpoint1, endpoint2, 2); return; }
cvLine(img, cvPoint(P1.x + 2, P1.y), cvPoint(P2.x + 2, P2.y), cvScalar(0, 0, 0), 1, 8, 0);


}

void onMouse(int event, int x, int y, int flags, void* param){// mouse event function
IplImage* cvimg = (IplImage*)param;
switch (event){
case CV_EVENT_LBUTTONDOWN:
CvScalar v = cvGet2D(cvimg, x, y);
cout << "Clicked Point:" << x << ", " << y << "/  Value:  " << v.val[0]<<endl;
if (v.val[0] >= 200){
Temp(cvimg, x, y, &P1, &P2, 1);
cout << "EndPoint1 :" << P1.x << ", " << P1.y << endl;
cout << "EndPoint2 :" << P2.x << ", " << P2.y << endl;
cout << "---------------------" <<endl;

}

}


}

edit retag close merge delete

3

Old C-API, please get rid of it... it gives you more headaches than beer!

( 2015-01-28 03:39:46 -0500 )edit
1

It will we better to add the source image , also try to create the desired image by using Paint or any software , more description help to find faster solution

( 2015-01-28 04:01:01 -0500 )edit

before it was not possible to add the image. but now the image is there. when clicked on a point of the line, the program find the 2 last (ending ) point , then i draw the line in green. you can see that for the second line, the program find only one ending point and the meeting point. I hope you can see what i want to say.... Thank you. For the old C-API, i know but how to learn easily the modern API C++? if you have some links of beginners tutorials, please you can help me.... Thanks

( 2015-01-28 04:18:21 -0500 )edit

Sort by ยป oldest newest most voted

I think you can solve your problem by using HoughTransfor

1. Use the function called HoughTransform.
2. The function will find 2 lines Line1 and Line2.
3. Get the muse click position on the image say it Pointxx.
4. Find the prependicular distances from Pointxx to Line1 and Line2.
5. The line which has minimum prependicular distance is the line you need.
6. Draw the line on the image.
more

Official site

GitHub

Wiki

Documentation