k_kaz k_kaz - 1 year ago 171
C++ Question

opencv namespaces and c,c++ functions

This is a test program with opencv. It isn't supposed to do anything.

#include <opencv2/opencv.hpp>

int main (){
cv::Mat src;
Mat dst; // <---- compile error (Mat not declared, suggested alternatives cv::)

cvNamedWindow("A", CV_WINDOW_AUTOSIZE);
cv::namedWindow("B", CV_WINDOW_AUTOSIZE);


cv::imshow("B", src);


I compile with cmake and make

cmake_minimum_required(VERSION 2.8)
project( opencvtest )
find_package( OpenCV REQUIRED )
add_executable( opencvtest main.cpp )
target_link_libraries( opencvtest ${OpenCV_LIBS} )

Cmake runs correctly. As you can see from the code the program complies fine without the
Mat dst
declaration. Now,
is a C function so it doesn't require namespace.
on the other hand is a C++ function so it does require a namespace declaration. http://docs.opencv.org/2.4/modules/highgui/doc/user_interface.html#imshow
So why
without namespace declaration passes the compile. Same goes with
that doesn't even have a c equivalent function

I am running on 3.1.0 opencv version and ubuntu 16.04

Answer Source

That's because of Argument-Dependent lookup (ADL).

Basically, because you give src / dst to imshow / medianBlur the compiler checks out namespace cv because src and dst come from that namespace (Mat is in the namespace cv).

It checks namespace cv, finds a function with the same name, checks that it's signature matches the call (parameters passed to it), and calls it.

This only works for functions(because you can pass arguments to them) which is why Mat dst; doesn't work. This also wouldn't work if you didn't have the include in there, of course.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download