【问题标题】:Expand dimensions for OpenCV mat object in C++在 C++ 中扩展 OpenCV mat 对象的尺寸
【发布时间】:2020-09-27 03:57:19
【问题描述】:

我的 tensorflow 模型期望图像具有以下形状:[1,512,512, 1],我正在尝试在 C++ 中扩展图像帧的尺寸。

在 Python 中,我可以按照以下方式进行操作,但如何在 C++ 中进行相同操作。

img = cv.imread('lena.png')
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
print('gray Image Dimension: ',dimensions)
#gray Image Dimension :  (512, 512)
input_ = np.expand_dims(gray, axis=0)
print('input_ Image Dimension: ',dimensions)
#input_ Image Dimension :  (1, 512, 512)
output_ = np.expand_dims(input_, axis=3)
print('output_ Image Dimension: ',dimensions)
#output_ Image Dimension :  (1, 512, 512, 1)

C++:

Mat src;
src = imread( lena.png, 1 );
cv::cvtColor(src, gray, cv::COLOR_BGR2GRAY);
// How can I convert it in to dimension of [1,512,512, 1]

【问题讨论】:

    标签: python c++ opencv


    【解决方案1】:
    std::cout << "gray: " << gray.size << std::endl;
    // gray: 512, 512 
    
    int size_1[3] = { 1, gray.rows, gray.cols };
    cv::Mat input_(3, size_1, gray.type(), gray.data);
    
    std::cout << "input_: " << input_.size << std::endl;
    // input_: 1, 512, 512
    
    int size_2[4] = { 1, gray.rows, gray.cols, 1};
    cv::Mat output_ (4, size_2, input_.type(), input_.data);
    
    std::cout << "output_ : " << output_ .size << std::endl;
    // output_ : 1, 512, 512, 1
    

    【讨论】:

      猜你喜欢
      • 2021-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-22
      相关资源
      最近更新 更多