opencv 相关函数集合

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* 合并多图像矩阵到一个大矩阵上 */
Mat mergeImgMats(vector<Mat> img_mats, int per_width, int per_height, int cols, int split_w) {
    int count = img_mats.size();
    CV_Assert(count > 0);
    // 定义 行
    int rows = count % cols == 0 ? count / cols : count / cols + 1;
 
    //准备一个大矩阵
    Mat bigMat(per_height * rows + split_w * (rows - 1), per_width*cols + split_w * (cols - 1), img_mats[0].type());
    bigMat.setTo(Scalar::all(0));
    Mat tmp;
    for (size_t i = 0; i < count; i++)
    {
        resize(img_mats[i], tmp, Size(per_width, per_height));
 
        int location_x = 0, location_y = 0;;
        //copy to location
        if (i < cols) { //第一行
            location_x = i * (per_width + split_w);
            location_y = 0;
        }
        else
        { // 第二行开始
            location_x = i % cols * (per_width + split_w);
            location_y = i / cols * (per_height + split_w);
        }
        Mat roi = bigMat(Rect(location_x, location_y, per_width, per_height));
        tmp.copyTo(roi);
    }
 
    tmp.release();
    return bigMat;
}
 
 
int BGR2YUV420P(Mat img, uint8_t *yuv_data, int w, int h) {
    assert(w % 2 == 0 && h % 2 == 0);
    Mat yuv;
    cvtColor(img, yuv, CV_BGR2YUV_I420);  //CV_BGR2YUV_I420
 
    Mat Y = yuv(Rect(0, 0, w, h));
    Mat U = yuv(Rect(0, h, w / 2, h / 2));
    Mat V = yuv(Rect(w / 2, h, w / 2, h / 2));
 
    memcpy_s(yuv_data, w * h, Y.data, h * w);
    for (size_t i = 0; i < h / 2; i++)
    {
        for (size_t j = 0; j < w; j++)
        {
            int index = i * w + j;
            if (j < 2 / w) yuv_data[w*h + index] = U.data[index];
            else yuv_data[w*h + index] = V.data[index - w / 2];
        }
    }
    yuv.release();
 
    return 0;
}
 
# YUV --> AVI
# 只适用于YUV格式数据直接存储的视频文件
int YuvFile_to_AviFile(string &yuv_filename, int src_img_w, int src_img_h, string &avi_filenanme, int dst_img_w, int dst_img_h) {
    int img_w = src_img_w, img_h = src_img_h;
    int scale_w = dst_img_w, scale_h = dst_img_h;
 
    int yuv_size = img_w * img_h * 3 / 2;
    FILE *f = NULL;
    uint8_t *yuv_data = new uint8_t[yuv_size];
    auto code = fopen_s(&f, yuv_filename.c_str(), "rb");
    assert(code == 0);
    Mat img(img_h, img_w, CV_8UC3);
    Mat yuvFrame(img_h * 3 / 2, img_w, CV_8UC1, yuv_data);
 
    VideoWriter newcw;
    auto flag = newcw.open(avi_filenanme, CV_FOURCC('M', 'P', '4', '2'), 25.0, Size(scale_w, scale_h));
    assert(true == flag);
 
    while (1)
    {
        auto _size = fread_s(yuv_data, yuv_size, 1, yuv_size, f);
        if (_size < yuv_size) {
            break;
        }
 
        cvtColor(yuvFrame, img, CV_YUV420p2RGB);
        assert(img.empty() == false && img.cols > 0);
 
        if (scale_w != img_w || scale_h != img_h) {
            resize(img, img, Size(scale_w, scale_h));
        }
        newcw.write(img);
    }
 
    yuvFrame.release();
    img.release();
    delete[] yuv_data;
    fclose(f);
    newcw.release();
 
    return 0;
}
 
 
 
# 编码
static  unsigned char* img2Encode(Mat src, int& dataLength) {
    vector<unsigned char> inImage;
    imencode(".jpg", src, inImage);
    size_t datalen = inImage.size();
    unsigned char *outImage = new unsigned char[datalen];
    for (int i = 0; i < datalen; i++)
    {
        outImage[i] = inImage[i];
    }
    inImage.clear();
    dataLength = datalen;
 
    return outImage;
}
 
# 解码
static Mat img2Decode(unsigned char *inImage, int dataLength) {
    vector<unsigned char> buff;
    for (int i = 0; i < dataLength; i++)
    {
        buff.push_back(inImage[i]);
    }
    Mat mat = imdecode(buff, CV_LOAD_IMAGE_COLOR);
    buff.clear();
 
    return mat;
}

  

posted @   dangxusheng  阅读(281)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示