EADST

C++: Format mat to float *

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);
}
相关标签
C++
About Me
XD
Goals determine what you are going to be.
Category
标签云
站点统计

本站现有博文258篇,共被浏览417082

本站已经建立1985天!

热门文章
文章归档
回到顶部