简体   繁体   English

在 C++ 中扩展 OpenCV mat 对象的维度

[英]Expand dimensions for OpenCV mat object in C++

My tensorflow model expects images to have shape: [1,512,512, 1] and i'm trying to expand dimensions for image frame in C++.我的 tensorflow 模型期望图像具有以下形状:[1,512,512, 1] 并且我正在尝试在 C++ 中扩展图像帧的尺寸。

In Python, I can do in below fashion but how I can do the same in 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++: 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]
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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM