簡體   English   中英

在 C++ 中擴展 OpenCV mat 對象的維度

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

我的 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]
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