Opencv常用操作

  • Post author:
  • Post category:其他


一、hwc转chw(RGBRGBRGB····转RR····GG····BB···)

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>
//#include <opencv2/blob.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
    // 图片尺寸:H500,W700,C3
    cv::Mat img=cv::imread("../yolov3_mini.jpg", 1);
    printf("%d, %d, %d\n", img.rows, img.cols, img.channels());

    // 5个batch
    vector<cv::Mat> inMatList;
    inMatList.push_back(img);
    inMatList.push_back(img);
    inMatList.push_back(img);
    inMatList.push_back(img);
    inMatList.push_back(img);
    // ret.data 数据组织形式为nchw,数据格式为float32类型
    cv::Mat ret = cv::dnn::blobFromImages(inMatList);
    printf("%d, %d, %d\n", ret.rows, ret.cols, ret.channels());
    // printf("%d\n", ret.rows, ret.cols, ret.channels());
    // nchw
    unsigned char smg[500*700*3*5];
    float * data = (float * )ret.data;
    for (int n=0;n<5;n++)
    for (int c=0;c<3;c++)
    for (int h=0;h<500;h++)
    for (int w=0;w<700;w++)
    {
        smg[n*500*700*3+h*700*3+w*3+c] = data[n*3*500*700+c*500*700+h*700+w];
    }
    // 解算出原图像,验证结果
    cv::imwrite("save1.bmp", cv::Mat(500 ,700, CV_8UC3, smg+700*500*3*0));
    cv::imwrite("save2.bmp", cv::Mat(500 ,700, CV_8UC3, smg+700*500*3*1));
    cv::imwrite("save3.bmp", cv::Mat(500 ,700, CV_8UC3, smg+700*500*3*2));
    cv::imwrite("save4.bmp", cv::Mat(500 ,700, CV_8UC3, smg+700*500*3*3));
    cv::imwrite("save5.bmp", cv::Mat(500 ,700, CV_8UC3, smg+700*500*3*4));
}



版权声明:本文为xidaoliang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。