C++ で画像データを前処理する

        Mat img = cv::imread(R"(M:\AllSourceImage\2022062900000000.png)");
        
        //printf("读取完成");
        int img_channels = img.channels();
        if (img_channels != 3) {
            printf("请输入三通道图片");
        }
        int input_image_col = img.cols;
        int input_image_row = img.rows;
        float new_image_col = INPUT_H;
        float new_image_row = INPUT_W;
        //得到图片缩放比列
        float scale = min(new_image_col / input_image_col, new_image_row / input_image_row);
        //得到缩放后的大小
        Size scaleSize = cv::Size(input_image_col * scale, input_image_row * scale);
        std::cout << "scaleSize" << scaleSize;
        Mat rgb;
        //BGR2RGB输出rgb图像
        cvtColor(img, rgb, COLOR_BGR2RGB);

        //将图片缩放
        Mat resized;
        cv::resize(rgb, resized, scaleSize, 0, 0, INTER_NEAREST);
        //创建灰色图像
        Mat image_back(Size(new_image_col, new_image_row), img.type(), cv::Scalar(128, 128, 128));
        //将样本图片粘贴在灰色图片上
        //--------创建设置画布绘制区域
        Rect roi_rect = Rect(0, 447, resized.cols, resized.rows);
        //cout << "*********" << resized.cols << "*********" << resized.rows;
        //--------将样本图片粘贴到灰色图片上去
        resized.copyTo(image_back(roi_rect));
        //cout << image_back.channels() << "channels输出完成";
        //将图片进行归一化
        Mat img_float;
        image_back.convertTo(img_float, CV_32FC3, 1.0 / 255.0);

        vector<Mat> channels;
        split(img_float, channels);
        std::cout << channels.at(0).cols << "  " << channels.at(0).rows << "\n";
        //float* image_data = new float[3 * 1788 * 1788];
        float* DATAIMAGE = new float[3 * 1788 * 1788];
        for (int i = 0; i < 3; ++i)
        {
            Mat image = channels.at(i).clone();
            Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> m_image;
            cv2eigen(image, m_image);
            Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> m_image_row = m_image;
            float* arr = new float[319694];
            //arr = m_image_row.data();
            //copy(arr, arr + 3196944, DATAIMAGE + (i * 3196944));
            copy(m_image_row.data(), m_image_row.data() + 3196944, DATAIMAGE + (i * 3196944));
        }

おすすめ

転載: blog.csdn.net/weixin_67615387/article/details/129226050