/*========================================================================= * 图像pj演示:2022-08-09 *========================================================================= */ #include #include #include //ubuntu必须必须头文件 #include #include #include #include #include #include #include #include #include #include #include #include #include //#include using namespace std; using namespace cv; //表头文件 : #include #include #include #include //========================================================= //======================================================================================================= //c++ 矩形数据结构定义 typedef struct MPOSITION { int left; //目标左上角x坐标 int top; //目标左上角y坐标 int right; //目标相对于左上角x坐标水平宽度 int bottom; //目标相对于左上角y坐标垂直高度 int width; //目标相对于左上角x坐标水平宽度 int height; //目标相对于左上角y坐标垂直高度 }MIMGPOS; // 视频源数据结构头文件 typedef struct MIMGMAP1 { int total;//视频源总数;例如:MAX_IMGGROUP_NUM =64; int h; //视频源行数;例如:row=8; int w; //视频源列数;例如:col=6; int idxw;//视频源缩引图像列数; int idxh;//视频源缩引图像行数; int num; //视频源编号;例如:MAX_IMGGROUP_NUM =64,num:0~63; MIMGPOS rr; //视频源图像在屏幕上的位置;例如:rr.left=1;rr.top=1,rr.right=420,rr.bottom=475 //CRect rr; char ipstr[20];//IP地址 char usensme[20];//用户名称 char passwd[20];//用户口令 int channel;//通道编号 int nport; //视频源端口:该编号一般不能重复 int kh; //1:可见光 ,2:红外 int dtype; //驱动类型,本系统可以分成N类:1:海康 ,2:集光,3:丰泰达;4:圣格尔 int logflag; //已登录成功为1;否则为0 int selflag; //已选中作做入侵检测处理:1;否则为0 long lUserID; //海康登录句柄 long m_lPlayHandle; //海康登录播放句柄 long hPlayWnd;//播放窗口的句柄,为NULL表示不播放图象 unsigned char mbufo[1920 * 1080 * 3];//拼接显示用 unsigned char mbufe[1920 * 1080 * 3];//鱼眼校正用 unsigned char mbufi[1920 * 1080 * 3];//拼接用 //unsigned char mbufo[1 * 1* 3]; }IMGMAP1; //设置窗口数 //多画面列数;例如8列。代表一行排列8视频源 int IMGCOLS = 1; //多画面行数;例如8行。代表屏幕共排列8行视频源 int IMGROWS = 1; // 多画面行数;视频源数据结构 //多画面总共变量 int MAX_IMGGROUP_NUM = 1; #define MAX_IMGS_NUM 15 IMGMAP1 imgs[MAX_IMGS_NUM]; //IMGMAP2 imgs2[MAX_IMGS_NUM]; //IMGMAP3 imgs3[MAX_IMGS_NUM]; int multimg_w = 0; int multimg_h = 0; //CRect rr; //MIMGPOS rr; Mat timg, himg; //多视频源启动显示标识 int mmultimg_flag = 0; //0:未启动;1:已启动 //双击放大显示单个特殊图像 int mmultimg_dispsingles = 0; //0:未启动;1:已启动 //======================================================== //14路拼接数据结构定义 typedef struct MPINDATAN { int cnt;//结构内数据个数 int bh; int xz; int yz; int width; int height; }PINDATAN; //14路拼接数据结构定义 typedef struct MPINDATA { int cnt;//结构内数据个数 int bh; int xz; int yz; int width; int height; }PINDATA; PINDATA pindatan[20]; int GetpinjeConfig(void); //// 用于拼接图像 //#define FCNT 2 //#define XDIM 960 //1500//图像宽度 //#define YDIM 1080//图像高度 //int m_Width = 960; //int m_Height = 1080; //int znum = 0; #define FCNT 2 //水平向拼接帧数 //#define VCNT 7 //垂直向拼接帧数 #define VCNT 7 //垂直向拼接帧数 #define XDIM 1920 //拼接图像水平向基本像元阵列个数定义 #define YDIM 1080 //拼接图像垂直向基本像元阵列个数定义 int m_Width = 1920;//拼接图像水平向基本像元阵列个数变量 int m_Height = 1080;//拼接图像垂直向基本像元阵列个数定义变量 int znum = 0;//图像拼接垂直与水平向拼接个数变量 //拼接图像 unsigned char pinbufo[XDIM*YDIM*FCNT*3]; //横向图像拼接缓存设置 //XDIM*YDIM*3为横向拼接单帧图像缓存定义尺寸 //FCNT为横向拼接图像缓存定义尺寸, //例如:横向2帧图像相拼,则FCNT定义为2; //横向10帧图像相拼,则FCNT定义为10; //横向N(N为大于1的正整数)帧图像相拼,则FCNT定义为N; unsigned char mpinbufo[XDIM*YDIM*FCNT*VCNT*3];//垂直拼接缓存设置 //XDIM*YDIM*3为垂直向拼接单帧图像缓存定义尺寸 //FCNT为横向拼接图像缓存定义尺寸, //VCNT为垂直向拼接图像条数缓存定义尺寸, //垂直向拼接的基本思想为: //第一步首先进行横向拼接,拼接完成后的拼接图像定义为一条横向图像 //(例如:横向2帧拼接后的图像定义为一条横向图像) //第二步是在第一步的基础上,进而对横向拼接完成后的数条横向图像进行垂直向再拼接, unsigned char bufo00[XDIM*YDIM * 3];//图像显示 unsigned char bufo01[XDIM*YDIM * 3];//图像显示 unsigned char bufi[XDIM*YDIM * 3]; //单帧图像中间缓存变量定义 unsigned char bufi1[XDIM*YDIM * 3]; //单帧图像中间缓存变量定义 //================================================================= //鱼眼校正函数 //X光应用部分 //鱼眼校正全局变量 char modname[60] = "amParam.dat"; int imgsize = 0; // x映像结构 Mat mapx; // y映像结构 Mat mapy; int initFisheye() { char imgname[60]; //char modname[60]; //char outname[60]; //固有矩阵 cv::Matx33d intrinsic_matrix; //畸变系数 cv::Vec4d distortion_coeffs; Size image_size; //string datFileName = "C:\\pic1\\gen\\amParam.dat"; //sprintf_s(modname, "C:\\pic1\\gen\\amParam.dat"); //printf("\n modname=%s \n ", modname); FILE *camParam; camParam=fopen(modname, "rb"); if (camParam == NULL) { //std::cout << "can not create data file: " << modname << " !!!" << std::endl; printf("\n can not create data file:%s \n ", modname); //CString str; //str.Format(_T("鱼眼校正模型打开失败,请检查模型路径"), modname); //AfxMessageBox(str); getchar(); return false; } fread(&intrinsic_matrix, sizeof(cv::Matx33d), 1, camParam); fread(&distortion_coeffs, sizeof(cv::Vec4d), 1, camParam); fread(&image_size, sizeof(Size), 1, camParam); fclose(camParam); // x映像结构 mapx = Mat(image_size, CV_32FC1); // y映像结构 mapy = Mat(image_size, CV_32FC1); //旋转映像结构 Mat R = Mat::eye(3, 3, CV_32F); //鱼眼校正初始化 //initUndistortRectifyMap(intrinsic_matrix, distortion_coeffs, R, intrinsic_matrix, image_size, CV_32FC1, mapx, mapy); fisheye::initUndistortRectifyMap(intrinsic_matrix, distortion_coeffs, R, intrinsic_matrix, image_size, CV_32FC1, mapx, mapy); //准备鱼眼校正后的图像结构 Mat t ////Mat t = testImage.clone(); ////cv::remap(testImage, t, mapx, mapy, INTER_LINEAR); ////进行鱼眼校正处理, ////testImage需要校正带图像,他、t校正后的图像 //remap(testImage, t, mapx, mapy, INTER_LINEAR); //imshow("testImage", testImage); //imshow("t", t); //printf("\n outname=%s \n ", outname); //imwrite(outname, t); //cout << "保存结束" << endl; //AfxMessageBox(_T("fisheye init successfully !!!")); return 1; } //鱼眼校正函数 int fisheyehandle(Mat &s, Mat &d) { Mat m; m = s.clone(); //imshow("testImage", s); remap(s,m,mapx,mapy,INTER_LINEAR); d = m.clone(); //imshow("dddd", d); //imshow("dddddt", d); //waitKey(0); m.release(); return 1; } int GetpinjeConfig(void) { FILE *fp; int bhn; int xz, yz, width, height; //errno_t err; //char fisheyeconfig[160]; fp=fopen("fisheyecfg.txt", "r");///home/li/lix/ //printf("\nfp=%d",fp); if (fp == NULL) { printf("\n畸变校正参数文件 打开失败 !!!,请检查参数文件路径\n"); //AfxMessageBox(_T("畸变校正参数文件 打开失败 !!!,请检查参数文件路径")); return 0; } for (int i = 1; i<17; i++) { int ret = fscanf(fp, "%d %d %d %d %d", &bhn, &xz, &yz, &width, &height); if (ret < 0) break; pindatan[i].xz = xz; pindatan[i].yz = yz; pindatan[i].width = width; pindatan[i].height = height; pindatan[i].bh = bhn; pindatan[0].cnt++; //printf("\n i=%d lx=%d ly=%d width=%d height=%d cnt=%d" // , i, pindatan[i].xz, pindatan[i].yz, pindatan[i].width, pindatan[i].height, pindatan[0].cnt); ////FILE *fpp; //char rett[200]; //fopen_s(&fpp, "c:\\GetpinjeConfig-log.txt", "a"); ////sprintf_s(rett, "\n j=%d i=%d 识别该帧图像所用的时间为:%0.3fms", j, i, time0*1000.0); //sprintf_s(rett, "\n i=%d lx=%d ly=%d width=%d height=%d cnt=%d" // , i, pindatan[i].xz, pindatan[i].yz, pindatan[i].width, pindatan[i].height, pindatan[0].cnt); //fwrite(rett, strlen(rett), 1, fpp); //fclose(fpp); } //getchar(); return 1; } //图像插入另一幅图像中心 void center_insert1(Mat &s, Mat &d,int h,int w) { int i, j; //CString str; //str.Format(_T(" h=%d w=%d"), h, w); //AfxMessageBox(str); int xx = (1920 - w) / 2; int yy = (1080 - h) / 2; for (i = 0; i < h; i++) { for (j = 0; j < w*3; j++) { d.at(i+(yy), j+(xx)*3) = s.at(i, j); } } //////FILE *fpp; //////char rett[200]; //////fopen_s(&fpp, "c:\\Fun2Proc-time-log.txt", "a"); ////////sprintf_s(rett, "\n j=%d i=%d 识别该帧图像所用的时间为:%0.3fms", j, i, time0*1000.0); //////sprintf_s(rett, "\n i=%d 所用的时间为:%0.3fms h_width=%d h_height=%d", i, time0*1000.0, h_width, h_height); //////fwrite(rett, strlen(rett), 1, fpp); //////fclose(fpp); //memcpy(&d.data[0], &dd[0], h*2*w *2*3); ////imshow("center_inser", d); //imwrite("c:\\avi\\center_insertd.bmp", d); //imwrite("c:\\avi\\center_inserts.bmp", s); //imshow("center_inser11", d); //waitKey(1000); } //================================================================= //pinjie funtion unsigned char mrpp[1920 * 1080 * 3]; int imgpinh(int znum, unsigned char *buf) { for (int hi = 0, j = 0; hi(getTickCount()); // pDlg->hmylock.lock(); time10 = static_cast(getTickCount()); int h_width = m_Width; int h_height = m_Height; int multimg_w, multimg_h, w, h; multimg_w = imgs[1].idxw=m_Width; multimg_h = imgs[1].idxh= m_Height; w = imgs[1].w = m_Width; h = imgs[1].h = m_Height; ////================================================ memcpy(&d1.data[0], &imgs[1].mbufe[0], m_Width*m_Height * 3); //鱼眼校正 resize(d1, src1, Size(1524, 800), 0, 0, INTER_LINEAR); center_insert1(src1, d1, 800, 1524); //dImg31 = dImg32.clone(); remap(d1, src1, mapx, mapy, INTER_LINEAR); m_roi1 = src1(cv::Rect(pindatan[1].xz, pindatan[1].yz, pindatan[1].width, pindatan[1].height)); resize(m_roi1, d1, Size(1920, 1080), 0, 0, INTER_LINEAR); //imshow("mimgpin-1",d1); memcpy(&imgs[1].mbufi[0], &d1.data[0], m_Width*m_Height * 3); /*resize(d1, dImg31, Size(1524, 800), 0, 0, INTER_LINEAR); center_insert1(dImg31, dImg32, 800, 1524); dImg31 = dImg32.clone(); remap(dImg32, dImg31, mapx, mapy, INTER_LINEAR); m_roi1 = dImg31(cv::Rect(pindatan[1].xz, pindatan[1].yz, pindatan[1].width, pindatan[1].height)); resize(m_roi1, d1, Size(1920, 1080), 0, 0, INTER_LINEAR); //imshow("mimgpin-1",d1); memcpy(&imgs[1].mbufi[0], &d1.data[0], m_Width*m_Height * 3); */ //dImg31.release(); //dImg32.release(); time10 = ((double)getTickCount() - time10) / getTickFrequency(); ttime10=ttime10+time10; printf("\n jj=%d fisheye correct所用的时间为:%0.3fms total=%0.3fms averge=%0.3fms\n",jj, time10*1000.00,ttime10*1000.00,ttime10*1000.0/5); //================================================ //resize(d1, src1, Size(multimg_w, multimg_h), 0, 0, INTER_LINEAR); //for (int ii = multimg_h, k = 0; ii >= 1; ii--, k++) { // memcpy(&imgs[1].mbufo[k*multimg_w * 3], &src1.data[(ii - 1)*multimg_w * 3], multimg_w * 3); //} //memcpy(imgs[1].mbufo, src.data, multimg_w*multimg_h * 3); //pDlg->hmylock.lock(); //sleep(1); //启动拼接线程 //SetEvent(pDlg->hx_hVideoEvent); //释放DecCBfun //ResetEvent(pDlg->x_14videoEvent); //time0 = ((double)getTickCount() - time0) / getTickFrequency(); ////FILE *fpp; //char rett[200]; //fopen_s(&fpp, "c:\\fyfunProc2-log.txt", "a"); //sprintf_s(rett, "\n 鱼眼校正1帧 所用的时间为:%0.3fms ",time0*1000.0); //fwrite(rett, strlen(rett), 1, fpp); //fclose(fpp); } imshow("mimgpin", d1); printf("\n multimg_w=%d multimg_=%d ",multimg_w, multimg_h); waitKey(10000); getchar(); return 0; //=================================================================== Rect rt; rt.x = 0; rt.y = 0; rt.height = x_Height - 2;// 1079; rt.width = x_Width - 2;// 1919; Mat src, dst,dd; Mat src11, dst11, dd1; Mat imgpin(1080, 1920*FCNT, CV_8UC3); Mat mimgpin(1080* VCNT, 1920* FCNT, CV_8UC3); printf("\n 1"); //memcpy(mrpp, imgrp.data, SRPH*SRPW * 3); //printf("\n 1imgrp,rows=%d cols=%d", imgrp.rows, imgrp.cols); printf("\n 3"); int h = 0; //记录起始时间 ttime=0.0; double time0; double time1; time0 = static_cast(getTickCount()); for(int hh=0;hh<5;hh++){ //记录起始时间 time1 = static_cast(getTickCount()); //printf("\n 拼接开始时间为:%0.3fms ms", time1*1000.0); //for (i = 1, j = 0; i < 15; i = i + 2, j++) { for (i = 1,j=0,h=0; i < 15; i=i+2,j++,h++) { sprintf(fname, "/home/li/lix/avi-10-2/himg%d.bmp",i+1); src = imread(fname, 1); if (i == 1 || i == 5 || i == 9 || i == 13) { rectangle(src, rt, Scalar(0, 0, 255), 8, LINE_8, 0); } else { rectangle(src, rt, Scalar(0, 255, 255),8, LINE_8, 0); } sprintf(fname1, "/home/li/lix/avi-10-2/himg%d.bmp", i+0); src1 = imread(fname1, 1); if (i == 1 || i == 5 || i == 9 || i == 13) { rectangle(src11, rt, Scalar(0, 255, 255), 8, LINE_8, 0); } else { rectangle(src11, rt, Scalar(0, 0, 255), 8, LINE_8, 0); } memcpy(bufi, src.data, src.rows*src.cols * 3); memcpy(bufi1,src11.data, src11.rows*src1.cols * 3); znum = 0; imgpinh(znum, &bufi[0]); znum = 1; imgpinh(znum, &bufi1[0]); memcpy(imgpin.data, pinbufo, 1920 * FCNT * 3 * 1080); //imshow("imgpin", imgpin); //imwrite("H:\\pic\\avi-10\\m.bmp", imgpin); //waitKey(1); mimgpinh(j, &pinbufo[0], 1920*FCNT, 1080); printf("\n hh=%d i=%d j=%d",hh,i,j); } memcpy(mimgpin.data, mpinbufo, 1080* VCNT * 1920*FCNT * 3); //// //for (int hi = 0, j = 0; hi(i, 0), detectionMat.at(i, 1), detectionMat.at(i, 2), detectionMat.at(i, 3) // , detectionMat.at(i, 4), detectionMat.at(i, 5), detectionMat.at(i, 6)); // fwrite(rett, strlen(rett), 1, fp); //fclose(fp); //FILE *fpp; //char rett[200]; //fopen_s(&fpp, "pinjietime14-log.txt", "a"); //sprintf(rett, "\n新算法拼接1帧所用的时间为:%0.3fms",time0*1000.0); //fwrite(rett, strlen(rett), 1, fpp); //fclose(fpp); printf("\n拼接结束!!!"); getchar(); return 0; } //======================================================================================