#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;
}
}