当前位置:网站首页>Five speakers: seventy genius_platform software platform development 】 【 turn YUY2 RGB24 implementation source code
Five speakers: seventy genius_platform software platform development 】 【 turn YUY2 RGB24 implementation source code
2022-08-02 19:04:00 【random wind】
- Connecting vigorously todayir红外sensor时,Requires the first halfYUV422of pseudo-color image dataRGB24转换,The source code can be transferredRGB24或者BGR24,Some functions can be deleted,Just focus on the key conversion function
1. 实现源码
Yuy2FrameProcess.h
#ifndef __YUY2_FRAME_PROCESS_H__
#define __YUY2_FRAME_PROCESS_H__
#include "OLeiIrFrameDef.h"
#include "Stream/GpIrFrameProcess.h"
//
// yuv420数据流处理类
class CYuy2FrameProcess : public CGpIrFrameProcess
{
public:
CYuy2FrameProcess();
virtual ~CYuy2FrameProcess();
// yuy2图像数据到rgb24图像数据转换
INT32 yuy2ToRgb24Convert(unsigned char* pSrc, unsigned char* pDst, int nImageWidth, int nImageHeight);
// yuy2图像数据到bgr24图像数据转换
INT32 yuy2ToBgr24Convert(unsigned char* pSrc, unsigned char* pDst, int nImageWidth, int nImageHeight);
public:
// 帧处理函数(目前与IRThere is no difference in stream processing)
virtual INT32 ProcessFrame(void* pData = NULL);
};
#endif //__YUY2_FRAME_PROCESS_H__
Yuy2FrameProcess.cpp
#include "Yuy2FrameProcess.h"
#include "Base/Thread/GpFrameProcessThread.h"
#include "Stream/GpFrameProcessCenter.h"
#include "Stream/Gpts/Yuy2FrameDef.h"
//
//
CYuy2FrameProcess::CYuy2FrameProcess()
{
LOGMSG("CYuy2FrameProcess::CYuy2FrameProcess");
// 等待耗时统计
TimePoint nTotalCostTm = currentTime();
// 设置type
this->setType(FrameProcessType_Yuv420);
LOGMSG("CYuy2FrameProcess::CYuy2FrameProcess is suc... nTotalCostTm=[%u]", (currentTime() - nTotalCostTm).count());
}
//
//
CYuy2FrameProcess::~CYuy2FrameProcess()
{
LOGMSG("CYuy2FrameProcess::~CYuy2FrameProcess");
// 等待耗时统计
TimePoint nTotalCostTm = currentTime();
// 释放线程资源
this->releaseFrameProcThread();
LOGMSG("CYuy2FrameProcess::~CYuy2FrameProcess is suc... nTotalCostTm=[%llu]", (currentTime() - nTotalCostTm).count());
}
//
// 处理数据流
INT32 CYuy2FrameProcess::ProcessFrame(void *pData)
{
CGpFrameProcessThread* pThread = static_cast<CGpFrameProcessThread*>(pData);
CHECKI(pThread);
CHECKF(m_pFrameProcessCenter);
CGpFrame* pFrame = this->takeFirst();
if (!pFrame || pFrame->isEmpty())
{
// 阻塞等待,Test the efficiency later when you have time
/* * 1.Locks cause a lot of context switching overhead,resulting in frame rate30~10帧 */
//std::unique_lock<std::mutex> ulock(m_Mtx);
//m_Cond.wait(ulock);
msSleep(10);
return ReturnCode_Empty;
}
GpFrameType frameType = pFrame->getFrameType();
CHECKI(frameType == FRAME_TYPE_EXT_IR);
int nConvertFormat = CONVERT_FORMAT_RGB24;
// 计算所需内存大小(w * h * 3) 每个像素点3通道
int nRgb24Size = pFrame->getWidth() * pFrame->getHeight() * 3;
// 构造ir数据帧
CGpFrame* pRgb24Frame = new CGpFrame(nRgb24Size);
if (pRgb24Frame == NULL)
{
LOGERROR("CYuy2FrameProcess::ProcessFrame new CGpFrame pRgb24Frame is NULL");
goto delete_frame;
}
pRgb24Frame->setBlockId(pFrame->getBlockId());
pRgb24Frame->setNetCode(pFrame->getNetCode());
pRgb24Frame->setCompressType(DATA_COMPRESS_RAW);
pRgb24Frame->setFrameType(FRAME_TYPE_RGB24);
pRgb24Frame->setWidth(pFrame->getWidth());
pRgb24Frame->setHeight(pFrame->getHeight());
pRgb24Frame->setChannelIndex(pFrame->getChannelIndex());
pRgb24Frame->setFrameCount(pFrame->getFrameCount());
pRgb24Frame->setTimestamp(pFrame->getTimestamp());
// 生产rgb或者bgr
if (nConvertFormat == CONVERT_FORMAT_RGB24)
{
// yuy2图像数据到rgb24图像数据转换
this->yuy2ToRgb24Convert((unsigned char*)pFrame->getData(), (unsigned char*)pRgb24Frame->getData(), pFrame->getWidth(), pFrame->getHeight());
}
else if (nConvertFormat == CONVERT_FORMAT_BGR24)
{
// yuy2图像数据到bgr24图像数据转换
this->yuy2ToRgb24Convert((unsigned char*)pFrame->getData(), (unsigned char*)pRgb24Frame->getData(), pFrame->getWidth(), pFrame->getHeight());
}
else
{
LOGERROR("CYuy2FrameProcess::yuy2ToRgb24Convert nConvertFormat is error... nConvertFormat=[%d]", nConvertFormat);
goto delete_frame;
}
CGpFrameProcessCenter* pFrameProcessCenter = dynamic_cast<CGpFrameProcessCenter*>(this->getFrameProcessCenter());
if (pFrameProcessCenter == NULL)
{
LOGERROR("CYuy2FrameProcess::ProcessFrame pFrameProcessCenter is NULL");
goto delete_frame;
}
// Send data processing
pFrameProcessCenter->outputFrame(pRgb24Frame);
delete_frame:
// 释放pIrFrame
SAFE_DELETE(pFrame);
// 释放pRgb24Frame
SAFE_DELETE(pRgb24Frame);
return ReturnCode_Success;
}
//
// yuy2图像数据到rgb24图像数据转换
// 原文链接:https://blog.csdn.net/tong5956/article/details/112037131
INT32 CYuy2FrameProcess::yuy2ToRgb24Convert(unsigned char* pSrc, unsigned char* pDst, int nImageWidth, int nImageHeight)
{
CHECKI(pSrc);
CHECKI(pDst);
CHECKI(nImageWidth > 0);
CHECKI(nImageHeight > 0);
// 获取yuyv数据指针
unsigned char* pYuyvData = pSrc;
int z = 0;
int i = 0;
int j = 0;
for (i = 0; i < nImageHeight; i++)
{
for (j = 0; j < nImageWidth; j++)
{
int r, g, b;
int y, u, v;
// YUYV格式
if (!z)
{
y = pYuyvData[0] << 8;
}
else
{
y = pYuyvData[2] << 8;
}
u = pYuyvData[1] - 128;
v = pYuyvData[3] - 128;
// yuvcomponent torgb分量转换
r = (y + (359 * v)) >> 8;
g = (y - (88 * u) - (183 * v)) >> 8;
b = (y + (454 * u)) >> 8;
// 如果溢出>255或者小于0
r = (r > 255) ? 255 : ((r < 0) ? 0 : r);
g = (g > 255) ? 255 : ((g < 0) ? 0 : g);
b = (b > 255) ? 255 : ((b < 0) ? 0 : b);
// 转换出来是rgb图像
*pDst++ = r;
*pDst++ = g;
*pDst++ = b;
// 执行0/1Reset and data pointer offset
if (z++)
{
z = 0;
pYuyvData += 4;
}
}
}
return ReturnCode_Success;
}
//
// yuy2图像数据到bgr24图像数据转换
INT32 CYuy2FrameProcess::yuy2ToBgr24Convert(unsigned char* pSrc, unsigned char* pDst, int nImageWidth, int nImageHeight)
{
CHECKI(pSrc);
CHECKI(pDst);
CHECKI(nImageWidth > 0);
CHECKI(nImageHeight > 0);
// 获取yuyv数据指针
unsigned char* pYuyvData = pSrc;
int z = 0;
int i = 0;
int j = 0;
for (i = 0; i < nImageHeight; i++)
{
for (j = 0; j < nImageWidth; j++)
{
int r, g, b;
int y, u, v;
// YUYV格式
if (!z)
{
y = pYuyvData[0] << 8;
}
else
{
y = pYuyvData[2] << 8;
}
u = pYuyvData[1] - 128;
v = pYuyvData[3] - 128;
// yuvcomponent torgb分量转换
r = (y + (359 * v)) >> 8;
g = (y - (88 * u) - (183 * v)) >> 8;
b = (y + (454 * u)) >> 8;
// 如果溢出>255或者小于0
r = (r > 255) ? 255 : ((r < 0) ? 0 : r);
g = (g > 255) ? 255 : ((g < 0) ? 0 : g);
b = (b > 255) ? 255 : ((b < 0) ? 0 : b);
// 转换出来是bgr图像
*pDst++ = b;
*pDst++ = g;
*pDst++ = r;
// 执行0/1Reset and data pointer offset
if (z++)
{
z = 0;
pYuyvData += 4;
}
}
}
return ReturnCode_Success;
}
2. 测试文件
- Execute program production2个test_rgb24.bin和test_bgr24.bin文件,Using the software platform offline tool to play the display is correct;
边栏推荐
猜你喜欢
随机推荐
js实现改变原来对象中的键值对对应的值
Navicat premium download and install 15 detailed tutorial
Oracle 11 g rac finished patch, dbca new patches of SQL database also needs to perform?
uniapp引入vantUI库
Limit实现分页
One article to understand DI dependency injection in php
es6 map使用场景
VMware启动报错:另一个程序已锁定文件的一部分,进程无法访问(删除最近的.lck文件夹)
Switch 块、Switch 表达式、Switch 模式匹配,越来越好用的 Switch
Mysql——字符串函数
总结嵌入式C语言难点 (1部分) 【结尾有资料】
解析并执行 shell 命令
[LeetCode]剑指 Offer 32 - II. 从上到下打印二叉树 II
持续集成(四)Jenkins配置报警机制
代码随想录笔记_哈希_61扑克牌中的顺子
RAID存储级别分类
锁定和并发控制(一)
jar包应用的简单启停脚本
排查生产环境:MySQLTransactionRollbackException数据库死锁
《独行月球》