19

Everywhere in features2D classes I see terms query and train. For example matches have trainIdx and queryIdx, and Matchers have train() method.

I know the definition of words train and query in English, but I can't understand the meaning of this properties or methods.

P.S. I understand, that it's very silly question, but maybe it's because English is not my native language.

Larry Foobar
  • 11,092
  • 15
  • 56
  • 89

3 Answers3

22

To complete sansuiso's answer, I suppose the reason for choosing these names should be that in some application we have got a set of images (training images) beforehand, for example 10 images taken inside your office. The features can be extracted and the feature descriptors can be computed for these images. And at run-time an image is given to the system to query the trained database. Hence the query image refers to this image. I really don't like the way they have named these parameters. Where you have a pair of stereo images and you want to match the features, these names don't make sense but you have to chose a convention say always call the left image the query image and the right image as the training image. I did my PhD in computer vision and some naming conventions in OpenCV seem really confusing/silly to me. So if you find these confusing or silly you're not alone.

fireant
  • 14,080
  • 4
  • 39
  • 48
  • 7
    To put it even more simple, train is the image you learned (extracted features) beforehand, query is the image that you are trying to match with the ones trained. – Rui Marques Jun 11 '12 at 09:45
  • I used to call these things earlier in another way: I called `train` as `ethalon` and `query` as `sample`. – Larry Foobar Oct 22 '12 at 13:46
  • 1
    @Shambool, i got a question, seems silly, but confused me a lot. if i put 2 trainDescripters trainA and trainB into flann matcher for train. then comes the query image queryC, will the match algorithm be executed for both trainA and trainB with queryC? – Ethan Wang Jan 28 '14 at 03:37
  • Agree, silly names. Better as `srcIdx`, `dstIdx` or `firstIdx`, `secondIdx`. – nn0p Dec 18 '15 at 13:28
12
  • train: this function builds the classifier inner state in order to make it operational. For example, think of training an SVM, or building a kd-tree from the reference data. Maybe you are confused because this step is often referred to as learning in the literature.

  • query is the action of finding the nearest neighbors to a set of points, and by extension it also refers to the whole set of points for which yo want a nearest neighbor. Recall that you can ask for the neighbors of 1 point, or a whole lot in the same function call (by stacking the feature points in a matrix).

  • trainIdxand queryIdx refer to the index of a pint in the reference / query set respectively, i.e. you ask the matcher for the nearest point (stored at the trainIdx position) to some other point (stored at the queryIdxposition). Of course, trainIdxis known after the function call. If your points are stored in a matrix, the index will be the line of the considered feature.

sansuiso
  • 9,259
  • 1
  • 40
  • 58
  • 1
    This answer makes it clear that trainIdx and queryIdx refer to the first and second (descriptor/keypoint) sets, respectively. – Greg Kramida Feb 19 '15 at 14:32
  • 2
    @GregKramida I think it is the other way around. When the matches are obtained with `matcher.match(descriptors1, descriptors2, matches)`, then `queryIdx` will index into `keypoints1` and `trainIdx` into `keypoints2`. c.f. http://stackoverflow.com/a/13320083/2397253 – oarfish Nov 29 '15 at 16:52
  • @oarfish, yes, you're right. I wonder if I should remove the comment so it doesn't confuse anybody. – Greg Kramida Nov 30 '15 at 14:18
  • @greg "First" and "second" in what reference frame? In the knnMatch method the first argument is the query, and second is train: http://docs.opencv.org/3.2.0/db/d39/classcv_1_1DescriptorMatcher.html#a378f35c9b1a5dfa4022839a45cdf0e89 – dividebyzero Feb 08 '17 at 11:16
  • @dividebyzero, yes. – Greg Kramida Feb 08 '17 at 16:28
3

I understand "query" and "train" in a very naive but useful way: "train": a data or image is preprocessed to get a database "query": an input data or image that will be queried in the database which we trained before. Hope it helps u as well.

thunderY
  • 59
  • 4
  • That's not naive, that is what it is. Or what it should be... It means a single train point may be assigned to more than one query point, for one thing. And also you can assign all the query points, but some train points may never be assigned. – dividebyzero Feb 08 '17 at 14:37