【指针的申请】:
unsigned char *PRGB = new unsigned char[<span style="font-family:FangSong_GB2312;">640*480</span>*<span style="font-family:FangSong_GB2312;">3</span>]; //<span style="font-family:FangSong_GB2312;">申请640*480*3个字节的图像内存</span>
//PRGB=(unsigned char*)malloc(<span style="font-family:FangSong_GB2312;">640*480</span>*<span style="font-family:FangSong_GB2312;">3</span>*sizeof(unsigned char));
malloc 和 new 都可以申请内存,二选一.
【指针的释放】:
delete []PRGB; //释放数组指针
PRGB =NULL;
【YUV2BGR】:
/***********************************************************************************
@code
unsigned char *PRGB = new unsigned char[nLen*2]; //nLen 是 YUV图像的length
//因为YUV.length =Width*Height*3/2, BGR.length = Width*Height*3
//所以 BGR = YUV*2
v4lconvert_yuv420_to_bgr241__((const unsigned char*)pBuf, PRGB,nWidth, nHeight, 24);
@encode
***********************************************************************************/
void Mythread::v4lconvert_yuv420_to_bgr241__(const unsigned char *src, unsigned char *dest,int width, int height, int yvu)
{
int i, j;
const unsigned char *ysrc = src;
const unsigned char *usrc, *vsrc;
if (yvu)
{
vsrc = src + width * height;
usrc = vsrc + (width * height) / 4;
} else {
usrc = src + width * height;
vsrc = usrc + (width * height) / 4;
}
for (i = 0; i < height; i++) {
for (j = 0; j < width; j += 2) {
#if 1 /* fast slightly less accurate multiplication free code */
int u1 = (((*usrc - 128) << 7) + (*usrc - 128)) >> 6;
int rg = (((*usrc - 128) << 1) + (*usrc - 128) +
((*vsrc - 128) << 2) + ((*vsrc - 128) << 1)) >> 3;
int v1 = (((*vsrc - 128) << 1) + (*vsrc - 128)) >> 1;
*dest++ = CLIP(*ysrc + u1);
*dest++ = CLIP(*ysrc - rg);
*dest++ = CLIP(*ysrc + v1);
ysrc++;
*dest++ = CLIP(*ysrc + u1);
*dest++ = CLIP(*ysrc - rg);
*dest++ = CLIP(*ysrc + v1);
#else
*dest++ = YUV2B(*ysrc, *usrc, *vsrc);
*dest++ = YUV2G(*ysrc, *usrc, *vsrc);
*dest++ = YUV2R(*ysrc, *usrc, *vsrc);
ysrc++;
*dest++ = YUV2B(*ysrc, *usrc, *vsrc);
*dest++ = YUV2G(*ysrc, *usrc, *vsrc);
*dest++ = YUV2R(*ysrc, *usrc, *vsrc);
#endif
ysrc++;
usrc++;
vsrc++;
}
/* Rewind u and v for next line */
if (!(i & 1)) {
usrc -= width / 2;
vsrc -= width / 2;
}
}
}
【从内存读取数据到Mat】:
Mat YUVDATA(Size(nWidth,nHeight),CV_8UC3,Scalar(0));
memcpy(YUVDATA.data,PRGB,nWidth*nHeight*3); //用memcpy将内存中的数据copy到Mat 中,然后<span style="font-family:FangSong_GB2312;">释放内存指针</span>
delete []PRGB;
PRGB =NULL;
【QT保存与读取图像】:
【保存成JPG图片】
QFile filebak(QString::fromStdString("/<span style="font-family:FangSong_GB2312;">yue</span>/info.jpg"));
filebak.open(QFile::WriteOnly);
filebak.write((const char*)pBuf, nLen);
filebak.close();
<span style="font-family:FangSong_GB2312;">【读取图片】:
QPixmap pixmap("/yue/info.jpg");
</span>
【opencv解码JPG】:
<span style="font-size:18px;"> //因为JPG格式编码的图像,图像大小就等于它的Length
Mat temp_mat = Mat(1,nLen,CV_8UC1,pBuf); </span><span style="font-size:18px;">
Mat jpgBuf = imdecode(temp_mat,CV_LOAD_IMAGE_COLOR);
Mat reSizeMat;
cv::resize(jpgBuf,reSizeMat,Size(iWidth,iHeight));
cv::cvtColor(reSizeMat,reSizeMat,CV_BGR2RGB);</span>
先申请一个一维的Mat来存jpg格式的数据(矩阵的长度就是图像的大小),然后imdecode解码,解码以后是BGR图像,需要转化成RGB