在C++中扩展OpenCV对象的尺寸

2024-06-02 06:17:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我的TysFooFrad模型期望图像具有形状:(1512512, 1)并且我试图扩展C++中的图像帧的维数。p> 在Python,我可以用下面的方式做,但是我如何在C++中做同样的事情。p>

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]

Tags: 图像imagesrcimginputoutputpngcv
1条回答
网友
1楼 · 发布于 2024-06-02 06:17:42
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

相关问题 更多 >