OpenCV:如何将多通道矩阵作为函数参数传递?

时间:2018-09-20 15:23:54

标签: c++ opencv

我有一个函数带有以下签名:import tensorflow as tf sess = tf.Session() hello = tf.constant("Hellow") print(sess.run(hello)) a = tf.constant(20) b = tf.constant(22) print('a + b = {0}'.format(sess.run(a+b))) 。我可以像这样将单个通道矩阵传递给它:

PS F:\tensorflow> python .\installation_test.py
Traceback (most recent call last):
  File "C:\Users\thava\AppData\Local\Programs\Python\Python37-32\lib\site-packages\tensorflow\python\pywrap_tensorflow.py",
line 18, in swig_import_helper
    fp, pathname, description = imp.find_module('_pywrap_tensorflow', [dirname(__file__)])
  File "C:\Users\thava\AppData\Local\Programs\Python\Python37-32\lib\imp.py", line 297, in find_module
    raise ImportError(_ERR_MSG.format(name), name=name)
ImportError: No module named '_pywrap_tensorflow'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\thava\AppData\Local\Programs\Python\Python37-32\lib\site-packages\tensorflow\python\__init__.py", line 66,
in <module>
    from tensorflow.python import pywrap_tensorflow
  File "C:\Users\thava\AppData\Local\Programs\Python\Python37-32\lib\site-packages\tensorflow\python\pywrap_tensorflow.py",
line 28, in <module>
    _pywrap_tensorflow = swig_import_helper()
  File "C:\Users\thava\AppData\Local\Programs\Python\Python37-32\lib\site-packages\tensorflow\python\pywrap_tensorflow.py",
line 20, in swig_import_helper
    import _pywrap_tensorflow
ModuleNotFoundError: No module named '_pywrap_tensorflow'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File ".\installation_test.py", line 1, in <module>
    import tensorflow as tf
  File "C:\Users\thava\AppData\Local\Programs\Python\Python37-32\lib\site-packages\tensorflow\__init__.py", line 24, in <module>
    from tensorflow.python import *
  File "C:\Users\thava\AppData\Local\Programs\Python\Python37-32\lib\site-packages\tensorflow\python\__init__.py", line 72,
in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "C:\Users\thava\AppData\Local\Programs\Python\Python37-32\lib\site-packages\tensorflow\python\pywrap_tensorflow.py",
line 18, in swig_import_helper
    fp, pathname, description = imp.find_module('_pywrap_tensorflow', [dirname(__file__)])
  File "C:\Users\thava\AppData\Local\Programs\Python\Python37-32\lib\imp.py", line 297, in find_module
    raise ImportError(_ERR_MSG.format(name), name=name)
ImportError: No module named '_pywrap_tensorflow'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\thava\AppData\Local\Programs\Python\Python37-32\lib\site-packages\tensorflow\python\__init__.py", line 66,
in <module>
    from tensorflow.python import pywrap_tensorflow
  File "C:\Users\thava\AppData\Local\Programs\Python\Python37-32\lib\site-packages\tensorflow\python\pywrap_tensorflow.py",
line 28, in <module>
    _pywrap_tensorflow = swig_import_helper()
  File "C:\Users\thava\AppData\Local\Programs\Python\Python37-32\lib\site-packages\tensorflow\python\pywrap_tensorflow.py",
line 20, in swig_import_helper
    import _pywrap_tensorflow
ModuleNotFoundError: No module named '_pywrap_tensorflow'


Failed to load the native TensorFlow runtime.

See https://github.com/tensorflow/tensorflow/blob/master/tensorflow/g3doc/get_started/os_setup.md#import_error

for some common reasons and solutions.  Include the entire stack trace
above this error message when asking for help.

没有投诉。但是,如何通过两个通道传递类型为process(Mat_<float>&)的矩阵?矩阵是这样创建的:

Mat img = cv::imread(inPath, 0);
Mat_<float> imgReal;
img.convertTo(imgReal, CV_32F, 1.0/255.0);
process(imgReal);

现在,当我调用CV_32FC2时,编译器将抛出Mat img = cv::imread(inPath, 0); Mat_<float> imgReal; img.convertTo(imgReal, CV_32F, 1.0/255.0); Mat imgImag = Mat(imgReal.rows, imgReal.cols, CV_32F, float(0)); vector<Mat> channels; channels.push_back(imgReal); channels.push_back(imgImag); Mat imgComplex = Mat(imgReal.rows, imgReal.cols, CV_32FC2); merge(channels,imgComplex); process(imgComplex);

那是什么原因?

1 个答案:

答案 0 :(得分:1)

您的情况与此类似:

struct asdf {
    asdf(int x){} // converting constructor
};
process(asdf& x) {}

鉴于此,允许以下内容

asdf x = 3;   // calls converting constructor
process(x);   // pass lvalue

但这是不允许的:

int x = 3;
process(x);   // attempts to call converting constructor 
              // and pass the result (rvalue!) to process

因为不允许将转换产生的右值绑定到非常量引用。作为解决方法,可以更改进程的签名(在此示例中为process(int&)),或者进行转换并分两步调用函数(类似于第一个代码段)。