C++: Format mat to float *
作者:XD / 发表: 2020年7月22日 00:46 / 更新: 2020年7月24日 04:28 / 编程笔记 / 阅读量:2318
C++: Format mat to float *
#include < string.h>
#include < opencv2/opencv.hpp>
using namespace std;
void main()
{
string test_path = "D:\\test.jpg";
cv::Mat img = cv::imread(test_path, CV_8UC1);
cv::imwrite("test01.jpg", img);
// format to float *
int img_height = img.rows;
int img_width = img.cols;
unsigned char *img_array = new unsigned char[img.rows*img.cols];
img_array = img.data;
float *img_pt = new float[img.rows*img.cols];
for (int i = 0; i < img.rows*img.cols; i++)
{
img_pt[i] = (float)img_array[i];
}
// format back to mat
uchar* temp_pt = new uchar[img_height*img_width];
for (int i = 0; i < img_height*img_width; i++)
temp_pt[i] = uchar(img_pt[i]);
cv::Mat out_img(img_height, img_width, CV_8UC1);
out_img.data = new uchar[img_height* img_width];
memcpy(out_img.data, temp_pt, img_height* img_width);
cv::imwrite("test02.jpg", out_img);
}
相关标签