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;
}
}
}
Old C-API, please get rid of it... it gives you more headaches than beer!
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
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