2

I use OpenCV 2.4.5. I want to draw the lines between matched points of two images. The code is:

const int &w=image1.cols;
for (size_t i = 0; i<good_matches.size(); i++ )
{
    //-- Get the keypoints from the good matches
    img1.push_back(keypoints1[good_matches[i].queryIdx].pt);
    img2.push_back(keypoints2[good_matches[i].trainIdx].pt);

    circle(image1,img1[i],20,Scalar(255,0,0),5);
    circle(image2,img2[i],20,Scalar(0,255,0),5);

    line(image1,img1[i],Point2f(img2[i].x+w,img2[i].y),Scalar(255,255,255),5);
    line(image2,Point2f(img1[i].x-w,img1[i].y),img2[i],Scalar(255,255,255),5);
}

When length of line within the bounds of image is more than 16400 I get strange result. It looks like triangle of lines or sometimes broken line between 2 corresponding points instead of straigth line between points.

So I should to draw line segments instead of total line. But it's not very convenient. Is it due to restrictions of line drawing algorythm or can be correcteed somehow?

  • 2
    Perhaps you can use a built in function `drawMatches`. http://docs.opencv.org/modules/features2d/doc/drawing_function_of_keypoints_and_matches.html?highlight=drawmatches#void drawMatches(const Mat& img1, const vector& keypoints1, const Mat& img2, const vector& keypoints2, const vector& matches1to2, Mat& outImg, const Scalar& matchColor, const Scalar& singlePointColor, const vector& matchesMask, int flags) – Alexey May 22 '13 at 13:20
  • Alex,Thanks for prompting about `drawMatches` it will be useful for me in some other cases. But I tried it and result was the same because this function also try to draw full length of long line with line function that doesn't work in mine case. – alex leonty May 27 '13 at 08:51

1 Answers1

0

The line drawing function cannot draw very long lines.

Below is some code demonstrating that a line 32800 pixels long won't render, while a line 32700 pixels long will. The cv::line function breaks down inside a called function ThickLine (drawing.cpp).

int length = 32800;
cv::Mat canvas = cv::Mat::zeros(5,length,CV_8UC3);// create blank canvas
cv::line(canvas,cv::Point(0,1),cv::Point(length,1),cv::Scalar(255,0,0),1,8); //blue line
cv::line(canvas,cv::Point(0,3),cv::Point(length-100,3),cv::Scalar(0,0,255),1,8); //red line
cv::imwrite("d:\\canvas.tif",canvas);

I don't know why your lines corrupt over 16400 (maybe 2^14 ?) pixels in length. You could try setting the thickness to 1, which leads down a different code path and see what happens.