justHelloWorld
justHelloWorld

Reputation: 6842

Difference between at() and row().col()

Given opencv::Mat m1,m2, I want to check if m1[i][j] and m2[i][j] are equal.

I've seen that there are two ways to do this:

if(m1.row(i).col(j) == m2.row(i).col(j))

and:

if(m1.at<float>(i,j)==m2.at<float>(i,j))

Notice that both m1 and m2 are SIFT matrix descriptors (so float is the element type).

My question is: what's the difference between the two access methods?

Upvotes: 1

Views: 557

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726839

Although the net result of the two operations is the same, the first one is more costly:

  • m1.row(i) creates a Mat object representing row i, then
  • col(j) is called on the Mat returned from m1.row(i) to get a single-element Mat representing object at (i, j)
  • The same sequence of operations is applied to m2
  • The two Mat objects are compared with ==
  • Four temporary objects get deallocated

In contrast, m1.at<float>(i,j) simply returns a float, which then gets compared to the other float, without creating any additional objects.

Upvotes: 2

Related Questions