OpenCV - Conversion between cv::Mat and unsigned char* array or float* array, conversion between cv::Mat and std::vector

1 Use conventional methods to convert cv::Mat to unsigned char array or float array

Normally, cv::Mat can be passed directly through const cv::Mat& img in the same opencv project. However, if cross-language transfer is required, such as C++ to C# or C# to C++, then usually this In this case, you need to convert cv::Mat into a memory pointer such as unsigned char pointer or float pointer for transfer.

1.1 cv::Mat is converted to unsigned char array, and unsigned char array is converted to cv::Mat

#include <iostream>
#include "opencv/cv.h"
#include "opencv2/opencv.hpp"


void ConvertOpencvMatToArray()
{
    
    
	cv::Mat img = cv::imread("demo.png");

	int img_length = img.total() * img.channels();
	unsigned char* image_array_ptr = new unsigned char[img_length]();

	std::memcpy(image_array_ptr, img.ptr<unsigned char>(0), img_length * sizeof(unsigned char));

	// 从unsigned char* 再转换为cv::Mat,验证转换是否正确
	cv::Mat a = cv::Mat(img.rows, img.cols, img.type(), image_array_ptr);
	cv::imwrite("a.jpg", a);

	delete[] image_array_ptr;
}


int main()
{
    
    
	ConvertOpencvMatToArray();


	getchar();

	return 0;
}

1.2 cv::Mat is converted to a float array, and a float array is converted to cv::Mat

#include <iostream>
#include "opencv/cv.h"
#include "opencv2/opencv.hpp"


void ConvertOpencvMatToArray()
{
    
    
	cv::Mat img = cv::imread("demo.png");
	
	img.convertTo(img, CV_32FC3);

	int img_length = img.total() * img.channels();
	float* image_array_ptr = new float[img_length]();

	std::memcpy(image_array_ptr, img.ptr<float>(0), img_length * sizeof(float));

	// 从float* 再转换为cv::Mat,验证转换是否正确
	cv::Mat a = cv::Mat(img.rows, img.cols, img.type(), image_array_ptr);
	cv::imwrite("a.jpg", a);

	delete[] image_array_ptr;
}


int main()
{
    
    
	ConvertOpencvMatToArray();


	getchar();

	return 0;
}

2 Convert cv::Mat to std::vector using C++ template

We can modify the code in Section 1 not to use dynamic arrays, but to use std::vector, so that we don't have to worry too much about memory leaks.

We can write a template method to complete the conversion between cv::Mat and different types of std::vector.

template<typename _Tp>
std::vector<_Tp> convert_mat_to_vector(const cv::Mat& mat)
{
    
    
    //通道数不变,按行转为一行
    return (std::vector<_Tp>)(mat.reshape(1, 1));
}

template<typename _Tp>
cv::Mat convert_vector_to_mat(std::vector<_Tp> v, int channels, int rows)
{
    
    
    //将vector变成单列的mat,这里需要clone(),因为这里的赋值操作是浅拷贝
    cv::Mat mat = cv::Mat(v).clone();
    cv::Mat dest = mat.reshape(channels, rows);
    return dest;
}

The sample code used is as follows:

#include <iostream>
#include "opencv/cv.h"
#include "opencv2/opencv.hpp"

template<typename _Tp>
std::vector<_Tp> convert_mat_to_vector(const cv::Mat& mat)
{
    
    
    //通道数不变,按行转为一行
    return (std::vector<_Tp>)(mat.reshape(1, 1));
}

template<typename _Tp>
cv::Mat convert_vector_to_mat(std::vector<_Tp> v, int channels, int rows)
{
    
    
    //将vector变成单列的mat,这里需要clone(),因为这里的赋值操作是浅拷贝
    cv::Mat mat = cv::Mat(v).clone();
    cv::Mat dest = mat.reshape(channels, rows);
    return dest;
}

int main()
{
    
    
    cv::Mat img = cv::imread("a.jpg");
    std::vector<float> v = convert_mat_to_vector<float>(img);
    cv::Mat dest = convert_vector_to_mat<float>(v, 3, img.rows);

    cv::imwrite("b.jpg", dest);

    getchar();

    return 0;
}

If you are interested, you can visit my personal website: www.stubbornhuang.com

Guess you like

Origin blog.csdn.net/HW140701/article/details/131064164