图像交叉格式和平坦格式互相转换(cross->planar, planar->cross)

#include "imghead.h"
#include <conio.h>

template <typename T>
void img_Cross2Planar(T *src, T *dst, int width, int height);
template <typename T>
void img_Planar2Cross(T *src, T *dst, int width, int height);

void main(int argc, char *argv[])
{

    VideoCapture cap;
    cap.open(0); //打开摄像头

    if (!cap.isOpened())
    {
        return;
    }
    // process
    Mat imgSrc;
    cap >> imgSrc;//等价于cap.read(frame);
    int height = imgSrc.rows;
    int width = imgSrc.cols;
    int memSize = 3 * height * width * sizeof(unsigned char);
    unsigned char *pSrc = (unsigned char*)malloc(memSize);
    int c = _getch();
    while (1)
    { 
        cap >> imgSrc;//等价于cap.read(frame);
        img_Cross2Planar(imgSrc.data, pSrc, width, height);
        
        Mat imgSrc2(imgSrc.rows, imgSrc.cols, CV_8UC3);
        img_Planar2Cross(pSrc, imgSrc2.data, width, height);
        namedWindow("show result", 0);
        imshow("show result", imgSrc2);
        cvWaitKey(100);
    }
    system("pause");
}


template <typename T>
void img_Cross2Planar(T *src, T *dst, int width, int height)
{
    int single_size = width * height;
    int single_size2 = single_size << 1;

    for (int cross_count = 0; cross_count < single_size; cross_count++)
    {
        dst[0] = src[0];
        dst[single_size] = src[1];
        dst[single_size2] = src[2];
        src += 3;
        dst++;
    }
}

template <typename T>
void img_Planar2Cross(T *src, T *dst, int width, int height)
{
    int single_size = width * height;
    int single_size2 = single_size << 1;
    int memSize = single_size + single_size2;

    for (int c = 0; c < 3; c++)
    {
        for (int i = 0; i < single_size; i++)
        {
            dst[c] = src[0];
            src++;
            dst += 3;
        }
        dst -= memSize;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值