Ask Your Question

OpenCV C++ detect line and coordinates and move it to the another image

asked 2018-12-02 07:42:34 -0500

We're trying to make game called PaperSoccer using C++ and OpenCV.

Original board looks like this :board

And picture with one move looks like this : photo

We're using perspective transform to remove the perspective in the second picture and we're able to find coordinates of points (dots).SOURCE CODE

We want to detect the place where the move was made and move it to the original board. We tried to use


to find the difference between the board and the photo, and then use

int DetectLines(Mat src, const char* sourceName, const char* destName){

Mat dst, cdst;
Mat zapisz;
zapisz = imread("plansza3.jpg",0);

Canny(src, dst, 50, 200, 3);
cvtColor(dst, cdst, COLOR_GRAY2BGR);

vector<Vec4i> lines;

HoughLinesP(dst, lines, 1, CV_PI / 180, 20, 10, 5);

for (size_t i = 0; i < 1; i++){
    Vec4i l = lines[i];
    line(zapisz, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0, 0, 255), 3, 2);
imshow(destName, zapisz);
return 0;

to find line and draw it on the board, but it's not working.

The most important is the fact that we need coordinates of the line (move) for the logic of the game, ie two points. We do not necessarily have to transfer it to the original board, but we must in some way convey that the center point in the picture corresponds to the center point on the original board etc.

Please, help us, it's a matter of life and death;)

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-12-03 05:00:53 -0500

kbarni gravatar image

First, debug your algorithm. Display the number of detected lines, eventually the coordinates.

You'll see that your algorithm detects thousands of lines, which is not good. Draw all of them to see what's happening.

First, all the black lines and points are detected. So it's the best to filter them out. Detecting only a colored line will spare you from further problems.

If you already work with lines, doing a Canny filtering will add parasite lines (one line will become two - at both sides of the original line). So you can work with the original binarized image.

You'll also see that reducing the image size will ameliorate the results of the algorithm: the irregularities of the line will be less visible and less partial lines will be detected.

Then, fine tune the parameters of the Hough transform (theta, rho, threshold etc.) until you get the desired results.

To detect the points of the board, you can use the Harris detector. Than, it's easy to get the central point.

Glad to have saved your life.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2018-12-02 07:42:34 -0500

Seen: 511 times

Last updated: Dec 03 '18