Opencv常用操作

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

#include 
#include 
#include 
//#include 
#include 
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 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));
}

你可能感兴趣的:(opencv,opencv,人工智能,计算机视觉)