海思篇之基于openCV的png图片加载

一、编程环境

本篇博客中图片加载的实现,是在海思SDK的基础上进行开发的,所以里面有很多结构体和函数都是来自于海思的SDK,另外,又因为图片的处理是基于了openCV的库,所以本次开发是在海思的大环境上,加上openCV进行开发的。

png图片可以实现背景透明化,所以这里主要通过操作png图片来实现。

二、实现步骤

  1. 准备一张png图片,将其转化成矩阵的形式,创建一个RGN区域,将图片贴入该区域中(该函数支持缩放)。
typedef struct PicInfo{
	unsigned int u32Width;
	unsigned int u32Height;
	string filename;
}PicInfo;

void load_png(Mat& rgnMat, double alpha, PicInfo *imgInfo)
{
	Mat img;
	Size imgSize;

	//图片大小
	imgSize.width = imgInfo->u32Width;
	imgSize.height= imgInfo->u32Height;

	//获取车辆图标文件文件格式
	char *ext = strrchr(imgInfo->filename.c_str(), '.');
	if(ext == NULL)
		return;
	//车辆图标文件为png文件
	if(strcmp(ext,".png") == 0)
	{
		//读取图标文件
		img = imread(imgInfo->filename.c_str(),IMREAD_UNCHANGED);
		SV_S32 imgw = img.cols;
		SV_S32 imgH = img.rows;

		if(imgw==0 ||imgH==0)
		{
				WriteLog(SV_AVS_ALG_LOG,"ERR:sv_alg_new:GenerateNewVehicleRgn:SV_CarModelLoad:LoadCarModel:Image is NULL");
				return;
		}
		//图标文件缩放
		resize(img, img, Size(imgSize.width, imgSize.height));
	}

	Mat uimg(img.size(), CV_8UC4, Scalar(0,0,0, alpha));
	Point root_points[1][4];
	root_points[0][0] = Point(0,0);
	root_points[0][1] = Point(0,0);
	root_points[0][2] = Point(0,0);
	root_points[0][3] = Point(0,0);

	const Point* ppt[1] = {root_points[0]};
	int npt=4;
	fillPoly(uimg, ppt, &npt, 1, Scalar(0,0,0,255));
	BGRA* ptr = (BGRA*)img.ptr(0);
	BGRA* ptrtemp = (BGRA*)uimg.ptr(0);
	for(int i=0;i
  1. 将其转化成BMP图片的格式,用于HI_MPI_RGN_SetBitMap的调用(因为海思图片的OSD的图片加载是用BMP进行的);
void LoadPng(svRgnBitMap** vehicleRgn, PicInfo *picInfo)
{
	Mat osdImage;
	svBITMAP_S bitmap;
	SV_CarModelLoad _carModel;

	load_png(osdImage, 168, picInfo);
	bitmap.pData = osdImage.ptr(0);
	bitmap.u32Height=osdImage.size().height;
	bitmap.u32Width =osdImage.size().width;
	bitmap.enPixelFormat =SV_PIXEL_FORMAT_RGB_8888;

	svBITMAP_S* outBitMap = new svBITMAP_S;
	outBitMap->u32Width = bitmap.u32Width;
	outBitMap->u32Height = bitmap.u32Height;
	SV_U32 size = bitmap.u32Width * bitmap.u32Height * 4;
	outBitMap->pData = malloc(size);
	memcpy(outBitMap->pData,bitmap.pData,size);
	outBitMap->enPixelFormat=bitmap.enPixelFormat;

	svRgnBitMap* carRgn = new svRgnBitMap;
	carRgn->Bitmaps=outBitMap;
	
	//图片起点坐标
	carRgn->stx = 2*(picInfo->x>>1);
	carRgn->sty = 2*(picInfo->y>>1);
	*vehicleRgn = carRgn;

	return ;
}

int main(void)
{
	//创建海思RGN区域

	//配置图片参数

	//加载png图片,并转化为BMP格式
	LoadPng(&_newVehicleRgn,  picInfo);
	//加载位图
	if(_newVehicleRgn != NULL)
	{
		BITMAP_S* bitmap =(BITMAP_S*)_newVehicleRgn->Bitmaps;
		s32Ret = HI_MPI_RGN_SetBitMap(Handle[i],(const BITMAP_S*)bitmap);
		if(s32Ret<0)
		{
			printf("HI_MPI_RGN_SetBitMap failed with %x\n", s32Ret);
			HI_MPI_RGN_Destroy(Handle[i]);
			return (s32Ret);
		}
		free(bitmap->pData);
		delete bitmap;
		delete _newVehicleRgn;
	}
}

你可能感兴趣的:(海思开发)