我们在现实中要把两张图片或横或竖地排列在一起,则首先要有一个平整的桌面或者一块平整的面板,这在编程中也一样的,而这个平整的面就是图层,或者可以理解为一张更大的矩形图片,这个矩形图片大到可以放下很多张你要拼接的图片,然后,在这张矩形图片的基础上,我们再进一步把要拼接的图片贴上去,这就是实现了图片的拼接。而我们只需要加载这个最大的矩形图片,就能得到出我们想要看到的拼接效果。
本篇博客中图片拼接的编程环境在上一篇博客《海思篇之基于openCV的png图片加载》中已经介绍,这里就不再重复。
png图片可以实现背景透明化,所以这里主要通过操作png图片来实现,另外,本次的图片拼接是可以实现图片在任意位置的拼接,但是没办法实现两张图片的叠加。
1.加载png图片,并将其转化为BMP格式的矩阵。
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
2.创建RGN区域(也就是图层),将图片排成一列,并贴到RGN区域。
//将3张图片拼接成一排
void SplitPicToColsFor3(svBITMAP_S &bitmaps, RECT_SV rectPoint, PicInfo *picInfo, SV_U32 mode)
{
SV_U32 i;
SV_U32 num;
Point_SV point;
SV_U32 flag[3] = {0,0,0};
Mat roiImg[3]; //ROI
Mat osdImg[3]; //图标
//创建图标对象
num = 3;
for(i=0; i=0 && point.y>=0 && ((point.x+osdImg[i].cols)<=osdImage.cols) && ((point.y+osdImg[i].rows)<=osdImage.rows))
{
roiImg[i] = osdImage( Rect( point.x, point.y, osdImg[i].cols, osdImg[i].rows ) );
osdImg[i].copyTo(roiImg[i]); //将图标拷贝到指定的区域
}
}
}
bitmaps.pData = osdImage.ptr(0);
bitmaps.u32Height=osdImage.size().height;
bitmaps.u32Width =osdImage.size().width;
bitmaps.enPixelFormat =SV_PIXEL_FORMAT_RGB_8888;
}
3.将图层矩阵(矩形图片)数据存储到海思可以调用的结构体成员中,用于海思HI_MPI_RGN_SetBitMap函数的调用。
void Load_Cols(svRgnBitMap** vehicleRgn, PicInfo *picInfo)
{
Mat osdImage;
svBITMAP_S bitmap;
SV_CarModelLoad _carModel;
SplitPicToColsFor3(bitmap, rectPoint, picInfo, dir);
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*(rectPoint.s32X>>1);
carRgn->sty=2*(rectPoint.s32Y>>1);
*vehicleRgn = carRgn;
return ;
}
4.调用加载效果。
int main(void)
{
//创建海思RGN区域
//配置图片参数
//加载png图片,并转化为BMP格式
Load_Cols(&_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;
}
}