vector数组转opencv的mat

在itk中需要将单层图像转为opencv格式进行展示,又没有办法直接转,只能将数据存在vector数组中,然后转为opencv的mat格式

common.h

#pragma once

#include
#include

/// 
/// vectot数组转opencv的mat
/// 
/// 输入vector
/// 图像通道数
/// 图像h方向大小
/// 输出图像
void vector2mat(
	const std::vector<double> vinput, 
	const int channels, const int rows,
	cv::Mat& mout)
{
	cv::Mat mat = cv::Mat(vinput);
	mout = mat.reshape(channels, rows).clone();
}

main.cpp

#include
#include"common.h"
using namespace std;

int main()
{
	vector<double> gray_img_value;
	for (int i = 0; i < 400 * 400; i++) {
		double value = (rand() % 255) / 255.0;
		gray_img_value.push_back(value);
	}
	cv::Mat img;
	vector2mat(gray_img_value, 1, 400, img);

	cout << img.size << endl;
	cout << img.channels() << endl;

	cv::imshow("w", img);
	cv::waitKey();	

	return 0;
}

vector数组转opencv的mat_第1张图片

你可能感兴趣的:(opencv,opencv,计算机视觉,人工智能)