// bagPositioning_test.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include #include #include #include #include #include #include "SG_bagPositioning_Export.h" #include #ifdef _WIN32 #include #include "direct.h" #endif typedef struct { int nPointIdx; double x; double y; double z; float r; float g; float b; } SPointXYZRGB; SVzNL3DPoint _ptRotate(SVzNL3DPoint pt3D, double matrix3d[9]) { SVzNL3DPoint _r_pt; _r_pt.x = pt3D.x * matrix3d[0] + pt3D.y * matrix3d[1] + pt3D.z * matrix3d[2]; _r_pt.y = pt3D.x * matrix3d[3] + pt3D.y * matrix3d[4] + pt3D.z * matrix3d[5]; _r_pt.z = pt3D.x * matrix3d[6] + pt3D.y * matrix3d[7] + pt3D.z * matrix3d[8]; return _r_pt; } SVzNLPointXYZRGBA _ptRotate_RGBD(SVzNLPointXYZRGBA pt3D, double matrix3d[9]) { SVzNLPointXYZRGBA _r_pt; _r_pt.x = pt3D.x * matrix3d[0] + pt3D.y * matrix3d[1] + pt3D.z * matrix3d[2]; _r_pt.y = pt3D.x * matrix3d[3] + pt3D.y * matrix3d[4] + pt3D.z * matrix3d[5]; _r_pt.z = pt3D.x * matrix3d[6] + pt3D.y * matrix3d[7] + pt3D.z * matrix3d[8]; return _r_pt; } #define DATA_VER_OLD 0 #define DATA_VER_NEW 1 #define DATA_VER_FROM_CUSTOM 2 #define VZ_LASER_LINE_PT_MAX_NUM 4096 SVzNLXYZRGBDLaserLine* vzReadLaserScanPointFromFile_XYZRGB(const char* fileName, int* scanLineNum, float* scanV, int* dataCalib, int* scanMaxStamp, int* canClockUnit) { std::ifstream inputFile(fileName); std::string linedata; if (inputFile.is_open() == false) return NULL; SVzNLXYZRGBDLaserLine* _scanLines = NULL; int lines = 0; int dataElements = 4; int firstIndex = -1; int dataFileVer = DATA_VER_OLD; std::getline(inputFile, linedata); //第一行 int lineNum = 0; if (0 == strncmp("LineNum:", linedata.c_str(), 8)) { dataFileVer = DATA_VER_NEW; sscanf_s(linedata.c_str(), "LineNum:%d", &lines); if (lines == 0) return NULL; lineNum = lines; _scanLines = (SVzNLXYZRGBDLaserLine*)malloc(sizeof(SVzNLXYZRGBDLaserLine) * (lineNum + 1)); memset(_scanLines, 0, sizeof(SVzNLXYZRGBDLaserLine) * (lineNum + 1)); if (scanLineNum) *scanLineNum = lines; } else if (0 == strncmp("LineNum_", linedata.c_str(), 8)) { dataFileVer = DATA_VER_OLD; sscanf_s(linedata.c_str(), "LineNum_%d", &lines); if (lines == 0) return NULL; lineNum = lines; _scanLines = (SVzNLXYZRGBDLaserLine*)malloc(sizeof(SVzNLXYZRGBDLaserLine) * (lineNum + 1)); memset(_scanLines, 0, sizeof(SVzNLXYZRGBDLaserLine) * (lineNum + 1)); if (scanLineNum) *scanLineNum = lines; } if (_scanLines == NULL) return NULL; int ptNum = 0; int lineIdx = -1; int ptIdx = 0; SVzNLPointXYZRGBA* p3DPoint = NULL; if (dataFileVer == DATA_VER_NEW) { while (getline(inputFile, linedata)) { if (0 == strncmp("ScanSpeed:", linedata.c_str(), 10)) { double lineV = 0; sscanf_s(linedata.c_str(), "ScanSpeed:%lf", &lineV); if (scanV) *scanV = (float)lineV; } else if (0 == strncmp("PointAdjust:", linedata.c_str(), 12)) { int ptAdjusted = 0; sscanf_s(linedata.c_str(), "PointAdjust:%d", &ptAdjusted); if (dataCalib) *dataCalib = ptAdjusted; } else if (0 == strncmp("MaxTimeStamp:", linedata.c_str(), 13)) { unsigned int maxTimeStamp = 0; unsigned int timePerStamp = 0; sscanf_s(linedata.c_str(), "MaxTimeStamp:%u_%u", &maxTimeStamp, &timePerStamp); if (scanMaxStamp) *scanMaxStamp = maxTimeStamp; if (canClockUnit) *canClockUnit = timePerStamp; } else if (0 == strncmp("Line_", linedata.c_str(), 5)) { int lineIndex; unsigned int timeStamp; sscanf_s(linedata.c_str(), "Line_%d_%u_%d", &lineIndex, &timeStamp, &ptNum); if (firstIndex < 0) firstIndex = lineIndex; lineIndex = lineIndex - firstIndex; if ((lineIndex < 0) || (lineIndex >= lines)) break; //new Line lineIdx++; if (ptNum > 0) { p3DPoint = (SVzNLPointXYZRGBA*)malloc(sizeof(SVzNLPointXYZRGBA) * ptNum); memset(p3DPoint, 0, sizeof(SVzNLPointXYZRGBA) * ptNum); } else p3DPoint = NULL; _scanLines[lineIdx].nPointCnt = 0; _scanLines[lineIdx].nTimeStamp = timeStamp; _scanLines[lineIdx].p3DPoint = p3DPoint; } else if (0 == strncmp("{", linedata.c_str(), 1)) { float X, Y, Z; int imageY = 0; float leftX, leftY; float rightX, rightY; float r, g, b; sscanf_s(linedata.c_str(), "{%f,%f,%f,%f,%f,%f }-{%f,%f}-{%f,%f}", &X, &Y, &Z, &r, &g, &b, &leftX, &leftY, &rightX, &rightY); int id = _scanLines[lineIdx].nPointCnt; if (id < ptNum) { if (lineIdx == 537) int kkk = 1; p3DPoint[id].x = X; p3DPoint[id].y = Y; p3DPoint[id].z = Z; int nr = (int)(r * 255); int ng = (int)(g * 255); int nb = (int)(b * 255); nb <<= 8; nb += ng; nb <<= 8; nb += nr; p3DPoint[id].nRGB = nb; _scanLines[lineIdx].nPointCnt = id + 1; } } } } else if (dataFileVer == DATA_VER_OLD) { while (getline(inputFile, linedata)) { if (0 == strncmp("DataElements_", linedata.c_str(), 13)) { sscanf_s(linedata.c_str(), "DataElements_%d", &dataElements); if ((dataElements != 3) && (dataElements != 4)) break; } if (0 == strncmp("LineV_", linedata.c_str(), 6)) { double lineV = 0; sscanf_s(linedata.c_str(), "LineV_%lf", &lineV); } else if (0 == strncmp("Line_", linedata.c_str(), 5)) { int lineIndex; unsigned int timeStamp; sscanf_s(linedata.c_str(), "Line_%d_%u", &lineIndex, &timeStamp); #if 0 if (scanLineListTail == NULL) firstIndex = lineIndex; #endif lineIndex = lineIndex - firstIndex; if ((lineIndex < 0) || (lineIndex >= lines)) break; //new Line //new Line lineIdx++; p3DPoint = (SVzNLPointXYZRGBA*)malloc(sizeof(SVzNLPointXYZRGBA) * VZ_LASER_LINE_PT_MAX_NUM); memset(p3DPoint, 0, sizeof(SVzNLPointXYZRGBA) * VZ_LASER_LINE_PT_MAX_NUM); _scanLines[lineIdx].nPointCnt = 0; _scanLines[lineIdx].nTimeStamp = timeStamp; _scanLines[lineIdx].p3DPoint = p3DPoint; } else if (0 == strncmp("(", linedata.c_str(), 1)) { float X, Y, Z; int imageY = 0; if (dataElements == 4) sscanf_s(linedata.c_str(), "(%f,%f,%f,%d)", &X, &Y, &Z, &imageY); else sscanf_s(linedata.c_str(), "(%f,%f,%f)", &X, &Y, &Z); int id = _scanLines[lineIdx].nPointCnt; if (id < VZ_LASER_LINE_PT_MAX_NUM) { p3DPoint[id].x = X; p3DPoint[id].y = Y; p3DPoint[id].z = Z; p3DPoint[id].nRGB = 0; _scanLines[lineIdx].nPointCnt = id + 1; } } } } inputFile.close(); return _scanLines; } void vzReadLaserScanPointFromFile_XYZRGB_vector(const char* fileName, std::vector>& scanData) { std::ifstream inputFile(fileName); std::string linedata; if (inputFile.is_open() == false) return; std::vector< SPointXYZRGB> a_line; while (getline(inputFile, linedata)) { if (0 == strncmp("Line_", linedata.c_str(), 5)) //new line { if (a_line.size() > 0) { scanData.push_back(a_line); a_line.clear(); } } else if (0 == strncmp("{", linedata.c_str(), 1)) { float leftX, leftY; float rightX, rightY; SPointXYZRGB a_pt; sscanf_s(linedata.c_str(), "{ %lf, %lf, %lf, %f, %f, %f }-{ %f, %f }-{ %f, %f }", &a_pt.x, &a_pt.y, &a_pt.z, &a_pt.r, &a_pt.g, &a_pt.b, &leftX, &leftY, &rightX, &rightY); a_line.push_back(a_pt); } } if(a_line.size() > 0) scanData.push_back(a_line); inputFile.close(); return; } SVzNL3DLaserLine* vzReadLaserScanPointFromFile_XYZ(const char* fileName, int* scanLineNum, float* scanV, int* dataCalib, int* scanMaxStamp, int* canClockUnit) { std::ifstream inputFile(fileName); std::string linedata; if (inputFile.is_open() == false) return NULL; SVzNL3DLaserLine* _scanLines = NULL; int lines = 0; int dataElements = 4; int firstIndex = -1; int dataFileVer = DATA_VER_OLD; std::getline(inputFile, linedata); //第一行 int lineNum = 0; if (0 == strncmp("LineNum:", linedata.c_str(), 8)) { dataFileVer = DATA_VER_NEW; sscanf_s(linedata.c_str(), "LineNum:%d", &lines); if (lines == 0) return NULL; lineNum = lines; _scanLines = (SVzNL3DLaserLine*)malloc(sizeof(SVzNL3DLaserLine) * (lineNum + 1)); memset(_scanLines, 0, sizeof(SVzNL3DLaserLine) * (lineNum + 1)); if (scanLineNum) *scanLineNum = lines; } else if (0 == strncmp("LineNum_", linedata.c_str(), 8)) { dataFileVer = DATA_VER_OLD; sscanf_s(linedata.c_str(), "LineNum_%d", &lines); if (lines == 0) return NULL; lineNum = lines; _scanLines = (SVzNL3DLaserLine*)malloc(sizeof(SVzNL3DLaserLine) * (lineNum + 1)); memset(_scanLines, 0, sizeof(SVzNL3DLaserLine) * (lineNum + 1)); if (scanLineNum) *scanLineNum = lines; } if (_scanLines == NULL) return NULL; int ptNum = 0; int lineIdx = -1; int ptIdx = 0; SVzNL3DPosition* p3DPoint = NULL; if (dataFileVer == DATA_VER_NEW) { while (getline(inputFile, linedata)) { if (0 == strncmp("ScanSpeed:", linedata.c_str(), 10)) { double lineV = 0; sscanf_s(linedata.c_str(), "ScanSpeed:%lf", &lineV); if (scanV) *scanV = (float)lineV; } else if (0 == strncmp("PointAdjust:", linedata.c_str(), 12)) { int ptAdjusted = 0; sscanf_s(linedata.c_str(), "PointAdjust:%d", &ptAdjusted); if (dataCalib) *dataCalib = ptAdjusted; } else if (0 == strncmp("MaxTimeStamp:", linedata.c_str(), 13)) { unsigned int maxTimeStamp = 0; unsigned int timePerStamp = 0; sscanf_s(linedata.c_str(), "MaxTimeStamp:%u_%u", &maxTimeStamp, &timePerStamp); if (scanMaxStamp) *scanMaxStamp = maxTimeStamp; if (canClockUnit) *canClockUnit = timePerStamp; } else if (0 == strncmp("Line_", linedata.c_str(), 5)) { int lineIndex; unsigned int timeStamp; sscanf_s(linedata.c_str(), "Line_%d_%u_%d", &lineIndex, &timeStamp, &ptNum); if (firstIndex < 0) firstIndex = lineIndex; lineIndex = lineIndex - firstIndex; if ((lineIndex < 0) || (lineIndex >= lines)) break; //new Line lineIdx++; if (ptNum > 0) { p3DPoint = (SVzNL3DPosition*)malloc(sizeof(SVzNL3DPosition) * ptNum); memset(p3DPoint, 0, sizeof(SVzNL3DPosition) * ptNum); } else p3DPoint = NULL; _scanLines[lineIdx].nPositionCnt = 0; _scanLines[lineIdx].nTimeStamp = timeStamp; _scanLines[lineIdx].p3DPosition = p3DPoint; } else if (0 == strncmp("{", linedata.c_str(), 1)) { float X, Y, Z; int imageY = 0; float leftX, leftY; float rightX, rightY; sscanf_s(linedata.c_str(), "{%f,%f,%f}-{%f,%f}-{%f,%f}", &X, &Y, &Z, &leftX, &leftY, &rightX, &rightY); int id = _scanLines[lineIdx].nPositionCnt; if (id < ptNum) { p3DPoint[id].pt3D.x = X; p3DPoint[id].pt3D.y = Y; p3DPoint[id].pt3D.z = Z; _scanLines[lineIdx].nPositionCnt = id + 1; } } } } else if (dataFileVer == DATA_VER_OLD) { while (getline(inputFile, linedata)) { if (0 == strncmp("DataElements_", linedata.c_str(), 13)) { sscanf_s(linedata.c_str(), "DataElements_%d", &dataElements); if ((dataElements != 3) && (dataElements != 4)) break; } if (0 == strncmp("LineV_", linedata.c_str(), 6)) { double lineV = 0; sscanf_s(linedata.c_str(), "LineV_%lf", &lineV); } else if (0 == strncmp("Line_", linedata.c_str(), 5)) { int lineIndex; unsigned int timeStamp; sscanf_s(linedata.c_str(), "Line_%d_%u", &lineIndex, &timeStamp); #if 0 if (scanLineListTail == NULL) firstIndex = lineIndex; #endif lineIndex = lineIndex - firstIndex; if ((lineIndex < 0) || (lineIndex >= lines)) break; //new Line //new Line lineIdx++; p3DPoint = (SVzNL3DPosition*)malloc(sizeof(SVzNL3DPosition) * VZ_LASER_LINE_PT_MAX_NUM); memset(p3DPoint, 0, sizeof(SVzNL3DPosition) * VZ_LASER_LINE_PT_MAX_NUM); _scanLines[lineIdx].nPositionCnt = 0; _scanLines[lineIdx].nTimeStamp = timeStamp; _scanLines[lineIdx].p3DPosition = p3DPoint; } else if (0 == strncmp("(", linedata.c_str(), 1)) { float X, Y, Z; int imageY = 0; if (dataElements == 4) sscanf_s(linedata.c_str(), "(%f,%f,%f,%d)", &X, &Y, &Z, &imageY); else sscanf_s(linedata.c_str(), "(%f,%f,%f)", &X, &Y, &Z); int id = _scanLines[lineIdx].nPositionCnt; if (id < VZ_LASER_LINE_PT_MAX_NUM) { p3DPoint[id].pt3D.x = X; p3DPoint[id].pt3D.y = Y; p3DPoint[id].pt3D.z = Z; _scanLines[lineIdx].nPositionCnt = id + 1; } } } } inputFile.close(); return _scanLines; } SVzNL3DLaserLine* _convertToGridData_XYZRGB(SVzNLXYZRGBDLaserLine* laser3DPoints, int lineNum, double _F, double* camPoseR, int* outLineNum) { int min_y = 100000000; int max_y = -10000000; int validStartLine = -1; int validEndLine = -1; for (int line = 0; line < lineNum; line++) { if (laser3DPoints[line].nPointCnt > 0) { if (validStartLine < 0) { validStartLine = line; validEndLine = line; } else validEndLine = line; } for (int i = 0; i < laser3DPoints[line].nPointCnt; i++) { SVzNLPointXYZRGBA* a_pt = &laser3DPoints[line].p3DPoint[i]; if (a_pt->z > 1e-4) { double v = _F * a_pt->y / a_pt->z + 2000; a_pt->nRGB = (int)(v + 0.5); max_y = max_y < (int)a_pt->nRGB ? (int)a_pt->nRGB : max_y; min_y = min_y > (int)a_pt->nRGB ? (int)a_pt->nRGB : min_y; } } } if (min_y == 100000000) return NULL; int vldLineNum = validEndLine - validStartLine + 1; *outLineNum = vldLineNum; int pt_counter = max_y - min_y + 1; SVzNL3DLaserLine* gridData = (SVzNL3DLaserLine*)malloc(sizeof(SVzNL3DLaserLine) * (vldLineNum + 1)); memset(gridData, 0, sizeof(SVzNL3DLaserLine) * (vldLineNum + 1)); for (int line = validStartLine; line <= validEndLine; line++) { int gridLine = line - validStartLine; gridData[gridLine].nPositionCnt = pt_counter; gridData[gridLine].nTimeStamp = laser3DPoints[line].nTimeStamp; gridData[gridLine].p3DPosition = (SVzNL3DPosition*)malloc(sizeof(SVzNL3DPosition) * pt_counter); memset(gridData[gridLine].p3DPosition, 0, sizeof(SVzNL3DPosition) * pt_counter); for (int i = 0; i < laser3DPoints[line].nPointCnt; i++) { SVzNLPointXYZRGBA a_pt = laser3DPoints[line].p3DPoint[i]; if (a_pt.z > 1e-4) { int pt_id = a_pt.nRGB - min_y; SVzNL3DPoint tmp_pt = { a_pt.x, a_pt.y, a_pt.z }; SVzNL3DPoint r_pt = _ptRotate(tmp_pt, camPoseR); gridData[gridLine].p3DPosition[pt_id].pt3D.x = r_pt.x; gridData[gridLine].p3DPosition[pt_id].pt3D.y = r_pt.y; gridData[gridLine].p3DPosition[pt_id].pt3D.z = r_pt.z; } } } return gridData; } void _convertToGridData_XYZRGB_vector(std::vector>& scanData, double _F, std::vector>& scanData_grid) { int min_y = 100000000; int max_y = -10000000; int lineNum = scanData.size(); for (int line = 0; line < lineNum; line++) { std::vector< SPointXYZRGB>& a_line = scanData[line]; int nPointCnt = a_line.size(); for (int i = 0; i < nPointCnt; i++) { SPointXYZRGB* a_pt = &scanData[line][i]; if (a_pt->z > 1e-4) { double v = _F * a_pt->y / a_pt->z + 2000; a_pt->nPointIdx = (int)(v + 0.5); max_y = max_y < (int)a_pt->nPointIdx ? (int)a_pt->nPointIdx : max_y; min_y = min_y > (int)a_pt->nPointIdx ? (int)a_pt->nPointIdx : min_y; } } } if (min_y == 100000000) return; int pt_counter = max_y - min_y + 1; std::vector< SPointXYZRGB> gridData; gridData.resize(pt_counter); for (int i = 0; i < pt_counter; i++) gridData[i] = { 0, 0.0, 0.0, 0.0, 0.0f, 0.0f, 0.0f }; for (int line = 0; line < lineNum; line++) { std::vector< SPointXYZRGB>& a_line = scanData[line]; int nPointCnt = a_line.size(); for (int i = 0; i < nPointCnt; i++) { SPointXYZRGB a_pt = a_line[i]; if (a_pt.z > 1e-4) { int pt_id = a_pt.nPointIdx - min_y; gridData[pt_id]= a_pt; } } scanData_grid.push_back(gridData); } return; } void _outputCalibPara(char* fileName, SSG_planeCalibPara calibPara) { std::ofstream sw(fileName); char dataStr[250]; //调平矩阵 sprintf_s(dataStr, 250, "%g, %g, %g", calibPara.planeCalib[0], calibPara.planeCalib[1], calibPara.planeCalib[2]); sw << dataStr << std::endl; sprintf_s(dataStr, 250, "%g, %g, %g", calibPara.planeCalib[3], calibPara.planeCalib[4], calibPara.planeCalib[5]); sw << dataStr << std::endl; sprintf_s(dataStr, 250, "%g, %g, %g", calibPara.planeCalib[6], calibPara.planeCalib[7], calibPara.planeCalib[8]); sw << dataStr << std::endl; //地面高度 sprintf_s(dataStr, 250, "%g", calibPara.planeHeight); sw << dataStr << std::endl; //反向旋转矩阵 sprintf_s(dataStr, 250, "%g, %g, %g", calibPara.invRMatrix[0], calibPara.invRMatrix[1], calibPara.invRMatrix[2]); sw << dataStr << std::endl; sprintf_s(dataStr, 250, "%g, %g, %g", calibPara.invRMatrix[3], calibPara.invRMatrix[4], calibPara.invRMatrix[5]); sw << dataStr << std::endl; sprintf_s(dataStr, 250, "%g, %g, %g", calibPara.invRMatrix[6], calibPara.invRMatrix[7], calibPara.invRMatrix[8]); sw << dataStr << std::endl; sw.close(); } SSG_planeCalibPara _readCalibPara(char* fileName) { //设置初始结果 double initCalib[9] = { 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0 }; SSG_planeCalibPara planePara; for (int i = 0; i < 9; i++) planePara.planeCalib[i] = initCalib[i]; planePara.planeHeight = -1.0; for (int i = 0; i < 9; i++) planePara.invRMatrix[i] = initCalib[i]; std::ifstream inputFile(fileName); std::string linedata; if (inputFile.is_open() == false) return planePara; //调平矩阵 std::getline(inputFile, linedata); sscanf_s(linedata.c_str(), "%lf, %lf, %lf", &planePara.planeCalib[0], &planePara.planeCalib[1], &planePara.planeCalib[2]); std::getline(inputFile, linedata); sscanf_s(linedata.c_str(), "%lf, %lf, %lf", &planePara.planeCalib[3], &planePara.planeCalib[4], &planePara.planeCalib[5]); std::getline(inputFile, linedata); sscanf_s(linedata.c_str(), "%lf, %lf, %lf", &planePara.planeCalib[6], &planePara.planeCalib[7], &planePara.planeCalib[8]); //地面高度 std::getline(inputFile, linedata); sscanf_s(linedata.c_str(), "%lf", &planePara.planeHeight); //反向旋转矩阵 std::getline(inputFile, linedata); sscanf_s(linedata.c_str(), "%lf, %lf, %lf", &planePara.invRMatrix[0], &planePara.invRMatrix[1], &planePara.invRMatrix[2]); std::getline(inputFile, linedata); sscanf_s(linedata.c_str(), "%lf, %lf, %lf", &planePara.invRMatrix[3], &planePara.invRMatrix[4], &planePara.invRMatrix[5]); std::getline(inputFile, linedata); sscanf_s(linedata.c_str(), "%lf, %lf, %lf", &planePara.invRMatrix[6], &planePara.invRMatrix[7], &planePara.invRMatrix[8]); inputFile.close(); return planePara; } void _outputScanDataFile_self(char* fileName, SVzNL3DLaserLine* scanData, int lineNum, float lineV, int maxTimeStamp, int clockPerSecond) { std::ofstream sw(fileName); sw << "LineNum:" << lineNum << std::endl; sw << "DataType: 0" << std::endl; sw << "ScanSpeed:" << lineV << std::endl; sw << "PointAdjust: 1" << std::endl; sw << "MaxTimeStamp:" << maxTimeStamp << "_" << clockPerSecond << std::endl; for (int line = 0; line < lineNum; line++) { sw << "Line_" << line << "_" << scanData[line].nTimeStamp << "_" << scanData[line].nPositionCnt << std::endl; for (int i = 0; i < scanData[line].nPositionCnt; i++) { SVzNL3DPosition* pt3D = &scanData[line].p3DPosition[i]; float x = (float)pt3D->pt3D.x; float y = (float)pt3D->pt3D.y; float z = (float)pt3D->pt3D.z; sw << "{" << x << "," << y << "," << z << "}-"; sw << "{0,0}-{0,0}" << std::endl; } } sw.close(); } void _outputScanDataFile_SPointXYZRGB_vector(char* fileName, std::vector>& scanData) { std::ofstream sw(fileName); int lineNum = scanData.size(); sw << "LineNum:" << lineNum << std::endl; sw << "DataType: 0" << std::endl; sw << "ScanSpeed: 0" << std::endl; sw << "PointAdjust: 1" << std::endl; sw << "MaxTimeStamp: 0_0" << std::endl; for (int line = 0; line < lineNum; line++) { int nPositionCnt = scanData[line].size(); sw << "Line_" << line << "_0_" << nPositionCnt << std::endl; for (int i = 0; i < nPositionCnt; i++) { SPointXYZRGB* pt3D = &scanData[line][i]; float x = (float)pt3D->x; float y = (float)pt3D->y; float z = (float)pt3D->z; char str[250]; sprintf_s(str, "{ %lf, %lf, %lf, %f, %f, %f } - { 0, 0 } - { 0, 0 }", pt3D->x, pt3D->y, pt3D->z, pt3D->r, pt3D->g, pt3D->b); sw << str << std::endl; } } sw.close(); } typedef struct { int r; int g; int b; }SG_color; void _outputScanDataFile_RGBD_obj(char* fileName, SVzNL3DLaserLine* scanData, int lineNum, float lineV, int maxTimeStamp, int clockPerSecond, std::vector& objOps) { std::ofstream sw(fileName); int realLines = lineNum; if (objOps.size() > 0) realLines++; sw << "LineNum:" << realLines << std::endl; sw << "DataType: 0" << std::endl; sw << "ScanSpeed:" << lineV << std::endl; sw << "PointAdjust: 1" << std::endl; sw << "MaxTimeStamp:" << maxTimeStamp << "_" << clockPerSecond << std::endl; int maxLineIndex = 0; int max_stamp = 0; SG_color rgb = { 0, 0, 0 }; SG_color objColor[8] = { {245,222,179},//淡黄色 {210,105, 30},//巧克力色 {240,230,140},//黄褐色 {135,206,235},//天蓝色 {250,235,215},//古董白 {189,252,201},//薄荷色 {221,160,221},//梅红色 {188,143,143},//玫瑰红色 }; int size = 1; int nTimeStamp = 0; for (int line = 0; line < lineNum; line++) { sw << "Line_" << line << "_" << scanData[line].nTimeStamp << "_" << scanData[line].nPositionCnt << std::endl; nTimeStamp = scanData[line].nTimeStamp; for (int i = 0; i < scanData[line].nPositionCnt; i++) { SVzNL3DPosition* pt3D = &scanData[line].p3DPosition[i]; int vType = pt3D->nPointIdx & 0xff; int hType = vType >> 4; int objId = (pt3D->nPointIdx >> 16)&0xffff; vType = vType & 0x0f; if(LINE_FEATURE_L_JUMP_H2L == vType) { rgb = { 255, 97, 0 }; size = 3; } else if (LINE_FEATURE_L_JUMP_L2H == vType) { rgb = { 255, 255, 0 }; size = 3; } else if (LINE_FEATURE_V_SLOPE == vType) { rgb = { 255, 0, 255}; size = 3; } else if (LINE_FEATURE_L_SLOPE_H2L == vType) { rgb = { 160, 82, 45 }; size = 3; } else if ((LINE_FEATURE_LINE_ENDING_0 == vType) || (LINE_FEATURE_LINE_ENDING_1 == vType)) { rgb = { 255, 0, 0 }; size = 3; } else if (LINE_FEATURE_L_SLOPE_L2H == vType) { rgb = { 233, 150, 122 }; size = 3; } else if (LINE_FEATURE_L_JUMP_H2L == hType) { rgb = { 0, 0, 255 }; size = 3; } else if ( LINE_FEATURE_L_JUMP_L2H == hType) { rgb = { 0, 255, 255}; size = 3; } else if ( LINE_FEATURE_V_SLOPE == hType) { rgb = { 0, 255, 0 }; size = 3; } else if ( LINE_FEATURE_L_SLOPE_H2L == hType) { rgb = { 85, 107, 47 }; size = 3; } else if ( LINE_FEATURE_L_SLOPE_L2H == hType) { rgb = { 0, 255, 154 }; size = 3; } else if ( (LINE_FEATURE_LINE_ENDING_0 == hType) || (LINE_FEATURE_LINE_ENDING_1 == hType)) { rgb = { 255, 0, 0 }; size = 3; } else if (objId > 0) //目标 { rgb = objColor[objId%8]; size = 5; } else { rgb = { 200, 200, 200 }; size = 1; } float x = (float)pt3D->pt3D.x; float y = (float)pt3D->pt3D.y; float z = (float)pt3D->pt3D.z; sw << "{" << x << "," << y << "," << z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << rgb.r << "," << rgb.g << "," << rgb.b << "," << size << " }" << std::endl; } } if (objOps.size() > 0) { int ptNum = objOps.size(); sw << "Line_" << lineNum << "_" << (nTimeStamp+ 1000) << "_" << ptNum << std::endl; for (int i = 0; i < objOps.size(); i++) { if(i == 0) rgb = { 255, 0, 0 }; else rgb = { 255, 255, 0 }; size = 25; float x = (float)objOps[i].centerPos.x; float y = (float)objOps[i].centerPos.y; float z = (float)objOps[i].centerPos.z; sw << "{" << x << "," << y << "," << z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << rgb.r << "," << rgb.g << "," << rgb.b << "," << size << " }" << std::endl; if (i == 0) { sw << "{" << x << "," << y << "," << z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << rgb.r << "," << rgb.g << "," << rgb.b << "," << size << " }" << std::endl; } } } //画出方向线 sw.close(); } void _outputScanDataFile_obj_vector(char* fileName, std::vector>scanData, std::vector& objOps) { int lineNum = scanData.size(); std::ofstream sw(fileName); int realLines = lineNum; if (objOps.size() > 0) realLines++; sw << "LineNum:" << realLines << std::endl; sw << "DataType: 0" << std::endl; sw << "ScanSpeed: 0" << std::endl; sw << "PointAdjust: 1" << std::endl; sw << "MaxTimeStamp: 0_0" << std::endl; int maxLineIndex = 0; int max_stamp = 0; SG_color rgb = { 0, 0, 0 }; SG_color objColor[8] = { {245,222,179},//淡黄色 {210,105, 30},//巧克力色 {240,230,140},//黄褐色 {135,206,235},//天蓝色 {250,235,215},//古董白 {189,252,201},//薄荷色 {221,160,221},//梅红色 {188,143,143},//玫瑰红色 }; int size = 1; int nTimeStamp = 0; for (int line = 0; line < lineNum; line++) { int nPositionCnt = scanData[line].size(); sw << "Line_" << line << "_0_" << nPositionCnt << std::endl; for (int i = 0; i < nPositionCnt; i++) { SVzNL3DPosition* pt3D = &scanData[line][i]; int vType = pt3D->nPointIdx & 0xff; int hType = vType >> 4; int objId = (pt3D->nPointIdx >> 16) & 0xffff; vType = vType & 0x0f; if (LINE_FEATURE_L_JUMP_H2L == vType) { rgb = { 255, 97, 0 }; size = 3; } else if (LINE_FEATURE_L_JUMP_L2H == vType) { rgb = { 255, 255, 0 }; size = 3; } else if (LINE_FEATURE_V_SLOPE == vType) { rgb = { 255, 0, 255 }; size = 3; } else if (LINE_FEATURE_L_SLOPE_H2L == vType) { rgb = { 160, 82, 45 }; size = 3; } else if ((LINE_FEATURE_LINE_ENDING_0 == vType) || (LINE_FEATURE_LINE_ENDING_1 == vType)) { rgb = { 255, 0, 0 }; size = 3; } else if (LINE_FEATURE_L_SLOPE_L2H == vType) { rgb = { 233, 150, 122 }; size = 3; } else if (LINE_FEATURE_L_JUMP_H2L == hType) { rgb = { 0, 0, 255 }; size = 3; } else if (LINE_FEATURE_L_JUMP_L2H == hType) { rgb = { 0, 255, 255 }; size = 3; } else if (LINE_FEATURE_V_SLOPE == hType) { rgb = { 0, 255, 0 }; size = 3; } else if (LINE_FEATURE_L_SLOPE_H2L == hType) { rgb = { 85, 107, 47 }; size = 3; } else if (LINE_FEATURE_L_SLOPE_L2H == hType) { rgb = { 0, 255, 154 }; size = 3; } else if ((LINE_FEATURE_LINE_ENDING_0 == hType) || (LINE_FEATURE_LINE_ENDING_1 == hType)) { rgb = { 255, 0, 0 }; size = 3; } else if (objId > 0) //目标 { rgb = objColor[objId % 8]; size = 5; } else { rgb = { 200, 200, 200 }; size = 1; } float x = (float)pt3D->pt3D.x; float y = (float)pt3D->pt3D.y; float z = (float)pt3D->pt3D.z; sw << "{" << x << "," << y << "," << z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << rgb.r << "," << rgb.g << "," << rgb.b << "," << size << " }" << std::endl; } } if (objOps.size() > 0) { int ptNum = objOps.size(); sw << "Line_" << lineNum << "_0_" << ptNum << std::endl; for (int i = 0; i < objOps.size(); i++) { if (i == 0) rgb = { 255, 0, 0 }; else rgb = { 255, 255, 0 }; size = 25; float x = (float)objOps[i].centerPos.x; float y = (float)objOps[i].centerPos.y; float z = (float)objOps[i].centerPos.z; sw << "{" << x << "," << y << "," << z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << rgb.r << "," << rgb.g << "," << rgb.b << "," << size << " }" << std::endl; if (i == 0) { sw << "{" << x << "," << y << "," << z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << rgb.r << "," << rgb.g << "," << rgb.b << "," << size << " }" << std::endl; } } } //画出方向线 sw.close(); } void _outputRGBDScanDataFile_RGBD_obj(char* fileName, SVzNLXYZRGBDLaserLine* scanData, int lineNum, float lineV, int maxTimeStamp, int clockPerSecond, std::vector& objOps) { std::ofstream sw(fileName); int realLines = lineNum; if (objOps.size() > 0) realLines++; sw << "LineNum:" << realLines << std::endl; sw << "DataType: 0" << std::endl; sw << "ScanSpeed:" << lineV << std::endl; sw << "PointAdjust: 1" << std::endl; sw << "MaxTimeStamp:" << maxTimeStamp << "_" << clockPerSecond << std::endl; int maxLineIndex = 0; int max_stamp = 0; SG_color rgb = { 0, 0, 0 }; SG_color objColor[8] = { {245,222,179},//淡黄色 {210,105, 30},//巧克力色 {240,230,140},//黄褐色 {135,206,235},//天蓝色 {250,235,215},//古董白 {189,252,201},//薄荷色 {221,160,221},//梅红色 {188,143,143},//玫瑰红色 }; int size = 1; int nTimeStamp = 0; for (int line = 0; line < lineNum; line++) { sw << "Line_" << line << "_" << scanData[line].nTimeStamp << "_" << scanData[line].nPointCnt << std::endl; nTimeStamp = scanData[line].nTimeStamp; for (int i = 0; i < scanData[line].nPointCnt; i++) { SVzNLPointXYZRGBA* pt3D = &scanData[line].p3DPoint[i]; #if 0 int vType = pt3D->nRGB & 0xff; int hType = vType >> 4; int objId = (pt3D->nRGB >> 16) & 0xffff; vType = vType & 0x0f; if (LINE_FEATURE_L_JUMP_H2L == vType) { rgb = { 255, 97, 0 }; size = 3; } else if (LINE_FEATURE_L_JUMP_L2H == vType) { rgb = { 255, 255, 0 }; size = 3; } else if (LINE_FEATURE_V_SLOPE == vType) { rgb = { 255, 0, 255 }; size = 3; } else if (LINE_FEATURE_L_SLOPE_H2L == vType) { rgb = { 160, 82, 45 }; size = 3; } else if ((LINE_FEATURE_LINE_ENDING_0 == vType) || (LINE_FEATURE_LINE_ENDING_1 == vType)) { rgb = { 255, 0, 0 }; size = 3; } else if (LINE_FEATURE_L_SLOPE_L2H == vType) { rgb = { 233, 150, 122 }; size = 3; } else if (LINE_FEATURE_L_JUMP_H2L == hType) { rgb = { 0, 0, 255 }; size = 3; } else if (LINE_FEATURE_L_JUMP_L2H == hType) { rgb = { 0, 255, 255 }; size = 3; } else if (LINE_FEATURE_V_SLOPE == hType) { rgb = { 0, 255, 0 }; size = 3; } else if (LINE_FEATURE_L_SLOPE_H2L == hType) { rgb = { 85, 107, 47 }; size = 3; } else if (LINE_FEATURE_L_SLOPE_L2H == hType) { rgb = { 0, 255, 154 }; size = 3; } else if ((LINE_FEATURE_LINE_ENDING_0 == hType) || (LINE_FEATURE_LINE_ENDING_1 == hType)) { rgb = { 255, 0, 0 }; size = 3; } else if (objId > 0) //目标 { rgb = objColor[objId % 8]; size = 5; } else #endif { rgb = { 200, 200, 200 }; size = 1; } float x = (float)pt3D->x; float y = (float)pt3D->y; float z = (float)pt3D->z; sw << "{" << x << "," << y << "," << z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << rgb.r << "," << rgb.g << "," << rgb.b << "," << size << " }" << std::endl; } } if (objOps.size() > 0) { int ptNum = objOps.size(); sw << "Line_" << lineNum << "_" << (nTimeStamp + 1000) << "_" << ptNum << std::endl; for (int i = 0; i < objOps.size(); i++) { if (i == 0) rgb = { 255, 0, 0 }; else rgb = { 255, 255, 0 }; size = 25; float x = (float)objOps[i].centerPos.x; float y = (float)objOps[i].centerPos.y; float z = (float)objOps[i].centerPos.z; sw << "{" << x << "," << y << "," << z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << rgb.r << "," << rgb.g << "," << rgb.b << "," << size << " }" << std::endl; if (i == 0) { sw << "{" << x << "," << y << "," << z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << rgb.r << "," << rgb.g << "," << rgb.b << "," << size << " }" << std::endl; } } } sw.close(); } void _outputScanDataFile_RGBD_sideBagObj(char* fileName, SVzNL3DLaserLine* scanData, int lineNum, float lineV, int maxTimeStamp, int clockPerSecond, std::vector& objOps) { std::ofstream sw(fileName); int realLines = lineNum; if (objOps.size() > 0) realLines++; sw << "LineNum:" << realLines << std::endl; sw << "DataType: 0" << std::endl; sw << "ScanSpeed:" << lineV << std::endl; sw << "PointAdjust: 1" << std::endl; sw << "MaxTimeStamp:" << maxTimeStamp << "_" << clockPerSecond << std::endl; int maxLineIndex = 0; int max_stamp = 0; SG_color rgb = { 0, 0, 0 }; SG_color objColor[8] = { {245,222,179},//淡黄色 {210,105, 30},//巧克力色 {240,230,140},//黄褐色 {135,206,235},//天蓝色 {250,235,215},//古董白 {189,252,201},//薄荷色 {221,160,221},//梅红色 {188,143,143},//玫瑰红色 }; int size = 1; int nTimeStamp = 0; for (int line = 0; line < lineNum; line++) { int nPntCount = 0; for (int i = 0; i < scanData[line].nPositionCnt; i++) { SVzNL3DPosition* pt3D = &scanData[line].p3DPosition[i]; if (pt3D->pt3D.z > 1e-4) nPntCount++; } sw << "Line_" << line << "_" << scanData[line].nTimeStamp << "_" << nPntCount << std::endl; nTimeStamp = scanData[line].nTimeStamp; for (int i = 0; i < scanData[line].nPositionCnt; i++) { SVzNL3DPosition* pt3D = &scanData[line].p3DPosition[i]; if (pt3D->pt3D.z < 1e-4) continue; int vType = pt3D->nPointIdx & 0xff; int hType = vType >> 4; int objId = (pt3D->nPointIdx >> 16) & 0xffff; vType = vType & 0x0f; if (LINE_FEATURE_L_JUMP_H2L == vType) { rgb = { 255, 97, 0 }; size = 3; } else if (LINE_FEATURE_L_JUMP_L2H == vType) { rgb = { 255, 255, 0 }; size = 3; } else if (LINE_FEATURE_V_SLOPE == vType) { rgb = { 255, 0, 255 }; size = 3; } else if (LINE_FEATURE_L_SLOPE_H2L == vType) { rgb = { 160, 82, 45 }; size = 3; } else if ((LINE_FEATURE_LINE_ENDING_0 == vType) || (LINE_FEATURE_LINE_ENDING_1 == vType)) { rgb = { 255, 0, 0 }; size = 3; } else if (LINE_FEATURE_L_SLOPE_L2H == vType) { rgb = { 233, 150, 122 }; size = 3; } else if (LINE_FEATURE_L_JUMP_H2L == hType) { rgb = { 0, 0, 255 }; size = 3; } else if (LINE_FEATURE_L_JUMP_L2H == hType) { rgb = { 0, 255, 255 }; size = 3; } else if (LINE_FEATURE_V_SLOPE == hType) { rgb = { 0, 255, 0 }; size = 3; } else if (LINE_FEATURE_L_SLOPE_H2L == hType) { rgb = { 85, 107, 47 }; size = 3; } else if (LINE_FEATURE_L_SLOPE_L2H == hType) { rgb = { 0, 255, 154 }; size = 3; } else if ((LINE_FEATURE_LINE_ENDING_0 == hType) || (LINE_FEATURE_LINE_ENDING_1 == hType)) { rgb = { 255, 0, 0 }; size = 3; } else if (objId > 0) //目标 { rgb = objColor[objId % 8]; size = 5; } else { rgb = { 200, 200, 200 }; size = 1; } float x = (float)pt3D->pt3D.x; float y = (float)pt3D->pt3D.y; float z = (float)pt3D->pt3D.z; sw << "{" << x << "," << y << "," << z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << rgb.r << "," << rgb.g << "," << rgb.b << "," << size << " }" << std::endl; } } if (objOps.size() > 0) { int ptNum = objOps.size(); sw << "Line_" << lineNum << "_" << (nTimeStamp + 1000) << "_" << ptNum << std::endl; for (int i = 0; i < objOps.size(); i++) { if (i == 0) rgb = { 255, 0, 0 }; else rgb = { 255, 255, 0 }; size = 25; float x = (float)objOps[i].graspPos.x; float y = (float)objOps[i].graspPos.y; float z = (float)objOps[i].graspPos.z; sw << "{" << x << "," << y << "," << z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << rgb.r << "," << rgb.g << "," << rgb.b << "," << size << " }" << std::endl; if (i == 0) { sw << "{" << x << "," << y << "," << z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << rgb.r << "," << rgb.g << "," << rgb.b << "," << size << " }" << std::endl; } } } sw.close(); } void EulerRpyToRotation1(double rpy[3], double matrix3d[9]) { double cos0 = cos(rpy[0]*PI/180); double sin0 = sin(rpy[0]*PI/180); double cos1 = cos(rpy[1]*PI/180); double sin1 = sin(rpy[1]*PI/180); double cos2 = cos(rpy[2]*PI/180); double sin2 = sin(rpy[2]*PI/180); matrix3d[0] = cos2 * cos1; matrix3d[1] = cos2 * sin1 * sin0 - sin2 * cos0; matrix3d[2] = cos2 * sin1 * cos0 + sin2 * sin0; matrix3d[3] = sin2 * cos1; matrix3d[4] = sin2 * sin1 * sin0 + cos2 * cos0; matrix3d[5] = sin2 * sin1 * cos0 - cos2 * sin0; matrix3d[6] = -sin1; matrix3d[7] = cos1 * sin0; matrix3d[8] = cos1 * cos0; return; } void _rotateCloudPts(SVzNL3DLaserLine* scanData, int lineNum, double matrix3d[9], std::vector>& rotateLines, SVzNLRangeD* rx_range, SVzNLRangeD* ry_range) { rx_range->min = 0; rx_range->max = -1; ry_range->min = 0; ry_range->max = -1; for (int line = 0; line < lineNum; line++) { std::vector< SVzNL3DPosition> linePts; for (int i = 0; i < scanData[line].nPositionCnt; i++) { SVzNL3DPosition* pt3D = &scanData[line].p3DPosition[i]; if (pt3D->pt3D.z < 1e-4) continue; SVzNL3DPosition r_pt; r_pt.pt3D = _ptRotate(pt3D->pt3D, matrix3d); r_pt.nPointIdx = pt3D->nPointIdx; if (rx_range->max < rx_range->min) { rx_range->min = r_pt.pt3D.x; rx_range->max = r_pt.pt3D.x; } else { if (rx_range->min > r_pt.pt3D.x) rx_range->min = r_pt.pt3D.x; if (rx_range->max < r_pt.pt3D.x) rx_range->max = r_pt.pt3D.x; } if (ry_range->max < ry_range->min) { ry_range->min = r_pt.pt3D.y; ry_range->max = r_pt.pt3D.y; } else { if (ry_range->min > r_pt.pt3D.y) ry_range->min = r_pt.pt3D.y; if (ry_range->max < r_pt.pt3D.y) ry_range->max = r_pt.pt3D.y; } linePts.push_back(r_pt); } rotateLines.push_back(linePts); } } void _rotateCloudPts_RGBD(SVzNLXYZRGBDLaserLine* scanData, int lineNum, double matrix3d[9], std::vector>& rotateLines, SVzNLRangeD* rx_range, SVzNLRangeD* ry_range) { rx_range->min = 0; rx_range->max = -1; ry_range->min = 0; ry_range->max = -1; for (int line = 0; line < lineNum; line++) { std::vector< SVzNLPointXYZRGBA> linePts; for (int i = 0; i < scanData[line].nPointCnt; i++) { SVzNLPointXYZRGBA* pt3D = &scanData[line].p3DPoint[i]; if (pt3D->z < 1e-4) continue; SVzNLPointXYZRGBA r_pt; r_pt = _ptRotate_RGBD(*pt3D, matrix3d); if (rx_range->max < rx_range->min) { rx_range->min = r_pt.x; rx_range->max = r_pt.x; } else { if (rx_range->min > r_pt.x) rx_range->min = r_pt.x; if (rx_range->max < r_pt.x) rx_range->max = r_pt.x; } if (ry_range->max < ry_range->min) { ry_range->min = r_pt.y; ry_range->max = r_pt.y; } else { if (ry_range->min > r_pt.y) ry_range->min = r_pt.y; if (ry_range->max < r_pt.y) ry_range->max = r_pt.y; } linePts.push_back(r_pt); } rotateLines.push_back(linePts); } } void _XOYprojection( cv::Mat& img, std::vector>& dataLines, std::vector& objOps, const double x_scale, const double y_scale, const SVzNLRangeD x_range, const SVzNLRangeD y_range, bool drawDirAngle, int dirAngleLen) { int x_skip = 16; int y_skip = 16; cv::Vec3b rgb = cv::Vec3b(0, 0, 0); cv::Vec3b objColor[8] = { {245,222,179},//淡黄色 {210,105, 30},//巧克力色 {240,230,140},//黄褐色 {135,206,235},//天蓝色 {250,235,215},//古董白 {189,252,201},//薄荷色 {221,160,221},//梅红色 {188,143,143},//玫瑰红色 }; int size = 1; for (int line = 0; line < dataLines.size(); line++) { std::vector< SVzNL3DPosition>& a_line = dataLines[line]; for (int i = 0; i < a_line.size(); i++) { SVzNL3DPosition* pt3D = &a_line[i]; if (pt3D->pt3D.z < 1e-4) continue; int vType = pt3D->nPointIdx & 0xff; int hType = vType >> 4; int objId = (pt3D->nPointIdx >> 16) & 0xff; vType = vType & 0x0f; if (LINE_FEATURE_L_JUMP_H2L == vType) { rgb = { 255, 97, 0 }; size = 2; } else if (LINE_FEATURE_L_JUMP_L2H == vType) { rgb = { 255, 255, 0 }; size = 2; } else if (LINE_FEATURE_V_SLOPE == vType) { rgb = { 255, 0, 255 }; size = 2; } else if (LINE_FEATURE_L_SLOPE_H2L == vType) { rgb = { 160, 82, 45 }; size = 2; } else if ((LINE_FEATURE_LINE_ENDING_0 == vType) || (LINE_FEATURE_LINE_ENDING_1 == vType)) { rgb = { 255, 0, 0 }; size = 2; } else if (LINE_FEATURE_L_SLOPE_L2H == vType) { rgb = { 233, 150, 122 }; size = 2; } else if (LINE_FEATURE_L_JUMP_H2L == hType) { rgb = { 0, 0, 255 }; size = 2; } else if (LINE_FEATURE_L_JUMP_L2H == hType) { rgb = { 0, 255, 255 }; size = 2; } else if (LINE_FEATURE_V_SLOPE == hType) { rgb = { 0, 255, 0 }; size = 2; } else if (LINE_FEATURE_L_SLOPE_H2L == hType) { rgb = { 85, 107, 47 }; size = 2; } else if (LINE_FEATURE_L_SLOPE_L2H == hType) { rgb = { 0, 255, 154 }; size = 2; } else if ((LINE_FEATURE_LINE_ENDING_0 == hType) || (LINE_FEATURE_LINE_ENDING_1 == hType)) { rgb = { 255, 0, 0 }; size = 2; } else if (objId > 0) //目标 { rgb = objColor[objId % 8]; size = 1; } else { rgb = { 150, 150, 150 }; size = 1; } double x = pt3D->pt3D.x; double y = pt3D->pt3D.y; int px = (int)((x - x_range.min) / x_scale + x_skip); int py = (int)((y - y_range.min) / y_scale + y_skip); if (size == 1) img.at(py, px) = cv::Vec3b(rgb[2], rgb[1], rgb[0]); else cv::circle(img, cv::Point(px, py), size, cv::Scalar(rgb[2], rgb[1], rgb[0]), -1); } } if (objOps.size() > 0) { for (int i = 0; i < objOps.size(); i++) { if (i == 0) { rgb = { 255, 0, 0 }; size = 20; } else { rgb = { 255, 255, 0 }; size = 10; } int px = (int)((objOps[i].centerPos.x - x_range.min) / x_scale + x_skip); int py = (int)((objOps[i].centerPos.y - y_range.min) / y_scale + y_skip); cv::circle(img, cv::Point(px, py), size, cv::Scalar(rgb[2], rgb[1], rgb[0]), -1); if (true == drawDirAngle) { //画线 double R = (double)dirAngleLen / 2.0; const double deg2rad = PI / 180.0; const double yaw = objOps[i].centerPos.z_yaw * deg2rad; double cy = cos(yaw); double sy = sin(yaw); double x1 = objOps[i].centerPos.x + R * cy; double y1 = objOps[i].centerPos.y - R * sy; double x2 = objOps[i].centerPos.x - R * cy; double y2 = objOps[i].centerPos.y + R * sy; int px1 = (int)((x1 - x_range.min) / x_scale + x_skip); int py1 = (int)((y1 - y_range.min) / y_scale + y_skip); int px2 = (int)((x2 - x_range.min) / x_scale + x_skip); int py2 = (int)((y2 - y_range.min) / y_scale + y_skip); cv::line(img, cv::Point(px1, py1), cv::Point(px2, py2), cv::Scalar(rgb[2], rgb[1], rgb[0]), 2); } } } } void _XOYprojection_RGBD( cv::Mat& img, std::vector>& dataLines, std::vector& objOps, const double x_scale, const double y_scale, const SVzNLRangeD x_range, const SVzNLRangeD y_range, bool drawDirAngle, int dirAngleLen) { int x_skip = 16; int y_skip = 16; cv::Vec3b rgb = cv::Vec3b(0, 0, 0); cv::Vec3b objColor[8] = { {245,222,179},//淡黄色 {210,105, 30},//巧克力色 {240,230,140},//黄褐色 {135,206,235},//天蓝色 {250,235,215},//古董白 {189,252,201},//薄荷色 {221,160,221},//梅红色 {188,143,143},//玫瑰红色 }; int size = 1; for (int line = 0; line < dataLines.size(); line++) { std::vector< SVzNLPointXYZRGBA>& a_line = dataLines[line]; for (int i = 0; i < a_line.size(); i++) { SVzNLPointXYZRGBA* pt3D = &a_line[i]; if (pt3D->z < 1e-4) continue; int nRGB = pt3D->nRGB; int r = nRGB & 0xff; nRGB >>= 8; int g = nRGB & 0xff; nRGB >>= 8; int b = nRGB & 0xff; rgb[0] = r; rgb[1] = g; rgb[2] = b; size = 1; double x = pt3D->x; double y = pt3D->y; int px = (int)((x - x_range.min) / x_scale + x_skip); int py = (int)((y - y_range.min) / y_scale + y_skip); if ((px == 666) && (py == 828)) int kkk = 1; if (size == 1) img.at(py, px) = cv::Vec3b(rgb[2], rgb[1], rgb[0]); else cv::circle(img, cv::Point(px, py), size, cv::Scalar(rgb[2], rgb[1], rgb[0]), -1); } } if (objOps.size() > 0) { for (int i = 0; i < objOps.size(); i++) { if (i == 0) { rgb = { 255, 0, 0 }; size = 20; } else { rgb = { 255, 255, 0 }; size = 10; } int px = (int)((objOps[i].centerPos.x - x_range.min) / x_scale + x_skip); int py = (int)((objOps[i].centerPos.y - y_range.min) / y_scale + y_skip); cv::circle(img, cv::Point(px, py), size, cv::Scalar(rgb[2], rgb[1], rgb[0]), -1); if (true == drawDirAngle) { //画线 double R = (double)dirAngleLen / 2.0; const double deg2rad = PI / 180.0; const double yaw = objOps[i].centerPos.z_yaw * deg2rad; double cy = cos(yaw); double sy = sin(yaw); double arrowLen = R/3; double arrowAngle = 30; double ca = cos(arrowAngle * deg2rad); double sa = sin(arrowAngle * deg2rad); SVzNL2DPointD endingPt[4]; endingPt[0] = { R, 0 }; endingPt[1] = { -R, 0 }; endingPt[2] = { R - arrowLen * ca, -arrowLen * sa }; endingPt[3] = { R - arrowLen * ca, arrowLen * sa }; for (int m = 0; m < 4; m++) { double tmp_x = endingPt[m].x * cy - endingPt[m].y * sy; double tmp_y = -endingPt[m].x * sy - endingPt[m].y * cy; endingPt[m].x = tmp_x + objOps[i].centerPos.x; endingPt[m].y = tmp_y + objOps[i].centerPos.y; } int px1 = (int)((endingPt[0].x - x_range.min) / x_scale + x_skip); int py1 = (int)((endingPt[0].y - y_range.min) / y_scale + y_skip); int px2 = (int)((endingPt[1].x - x_range.min) / x_scale + x_skip); int py2 = (int)((endingPt[1].y - y_range.min) / y_scale + y_skip); cv::line(img, cv::Point(px1, py1), cv::Point(px2, py2), cv::Scalar(rgb[2], rgb[1], rgb[0]), 2); int px3 = (int)((endingPt[2].x - x_range.min) / x_scale + x_skip); int py3 = (int)((endingPt[2].y - y_range.min) / y_scale + y_skip); int px4 = (int)((endingPt[3].x - x_range.min) / x_scale + x_skip); int py4 = (int)((endingPt[3].y - y_range.min) / y_scale + y_skip); if (objOps[i].orienFlag == 1) { cv::line(img, cv::Point(px1, py1), cv::Point(px3, py3), cv::Scalar(0, 255, 0), 2); cv::line(img, cv::Point(px1, py1), cv::Point(px4, py4), cv::Scalar(0, 255, 0), 2); //cv::circle(img, cv::Point(px1, py1), 5, cv::Scalar(0, 255, 0), -1); } else if (objOps[i].orienFlag == 2) { cv::line(img, cv::Point(px1, py1), cv::Point(px3, py3), cv::Scalar(0, 0, 255), 2); cv::line(img, cv::Point(px1, py1), cv::Point(px4, py4), cv::Scalar(0, 0, 255), 2); //cv::circle(img, cv::Point(px1, py1), 5, cv::Scalar(0, 0, 255), -1); } } } } } void _XOYprojection_sideBagInfo(cv::Mat& img, std::vector>& dataLines, std::vector& objOps, const double x_scale, const double y_scale, const SVzNLRangeD x_range, const SVzNLRangeD y_range) { int x_skip = 16; int y_skip = 16; cv::Vec3b rgb = cv::Vec3b(0, 0, 0); cv::Vec3b objColor[8] = { {245,222,179},//淡黄色 {210,105, 30},//巧克力色 {240,230,140},//黄褐色 {135,206,235},//天蓝色 {250,235,215},//古董白 {189,252,201},//薄荷色 {221,160,221},//梅红色 {188,143,143},//玫瑰红色 }; int size = 1; for (int line = 0; line < dataLines.size(); line++) { std::vector< SVzNL3DPosition>& a_line = dataLines[line]; for (int i = 0; i < a_line.size(); i++) { SVzNL3DPosition* pt3D = &a_line[i]; if (pt3D->pt3D.z < 1e-4) continue; int vType = pt3D->nPointIdx & 0xff; int hType = vType >> 4; int objId = (pt3D->nPointIdx >> 16) & 0xffff; vType = vType & 0x0f; if (LINE_FEATURE_L_JUMP_H2L == vType) { rgb = { 255, 97, 0 }; size = 2; } else if (LINE_FEATURE_L_JUMP_L2H == vType) { rgb = { 255, 255, 0 }; size = 2; } else if (LINE_FEATURE_V_SLOPE == vType) { rgb = { 255, 0, 255 }; size = 2; } else if (LINE_FEATURE_L_SLOPE_H2L == vType) { rgb = { 160, 82, 45 }; size = 2; } else if ((LINE_FEATURE_LINE_ENDING_0 == vType) || (LINE_FEATURE_LINE_ENDING_1 == vType)) { rgb = { 255, 0, 0 }; size = 2; } else if (LINE_FEATURE_L_SLOPE_L2H == vType) { rgb = { 233, 150, 122 }; size = 2; } else if (LINE_FEATURE_L_JUMP_H2L == hType) { rgb = { 0, 0, 255 }; size = 2; } else if (LINE_FEATURE_L_JUMP_L2H == hType) { rgb = { 0, 255, 255 }; size = 2; } else if (LINE_FEATURE_V_SLOPE == hType) { rgb = { 0, 255, 0 }; size = 2; } else if (LINE_FEATURE_L_SLOPE_H2L == hType) { rgb = { 85, 107, 47 }; size = 2; } else if (LINE_FEATURE_L_SLOPE_L2H == hType) { rgb = { 0, 255, 154 }; size = 2; } else if ((LINE_FEATURE_LINE_ENDING_0 == hType) || (LINE_FEATURE_LINE_ENDING_1 == hType)) { rgb = { 255, 0, 0 }; size = 2; } else if ( (objId > 0) &&( objId< 1000)) //目标 { rgb = objColor[objId % 8]; size = 3; } else { rgb = { 150, 150, 150 }; size = 1; } double x = pt3D->pt3D.x; double y = pt3D->pt3D.y; int px = (int)((x - x_range.min) / x_scale + x_skip); int py = (int)((y - y_range.min) / y_scale + y_skip); if (size == 1) img.at(py, px) = cv::Vec3b(rgb[2], rgb[1], rgb[0]); else cv::circle(img, cv::Point(px, py), size, cv::Scalar(rgb[2], rgb[1], rgb[0]), -1); } } if (objOps.size() > 0) { for (int i = 0; i < objOps.size(); i++) { if (i == 0) { rgb = { 255, 0, 0 }; size = 20; } else { rgb = { 255, 255, 0 }; size = 10; } int px = (int)((objOps[i].graspPos.x - x_range.min) / x_scale + x_skip); int py = (int)((objOps[i].graspPos.y - y_range.min) / y_scale + y_skip); cv::circle(img, cv::Point(px, py), size, cv::Scalar(rgb[2], rgb[1], rgb[0]), -1); //画ROI size = 3; cv::Point2d vec2d[4]; vec2d[0].x = objOps[i].objROI.left; vec2d[0].y = objOps[i].objROI.top; vec2d[1].x = objOps[i].objROI.right; vec2d[1].y = objOps[i].objROI.top; vec2d[2].x = objOps[i].objROI.right; vec2d[2].y = objOps[i].objROI.bottom; vec2d[3].x = objOps[i].objROI.left; vec2d[3].y = objOps[i].objROI.bottom; cv::Point vec[4]; for (int j = 0; j < 4; j++) { vec[j].x = (int)((vec2d[j].x - x_range.min) / x_scale + x_skip); vec[j].y = (int)((vec2d[j].y - y_range.min) / y_scale + y_skip); } for (int j = 0; j < 4; j++) { int nxtIdx = (j + 1) % 4; cv::line(img, vec[j], vec[nxtIdx], cv::Scalar(rgb[2], rgb[1], rgb[0]), size); } //画倾角 double r = 50; double angle = objOps[i].graspPos.z_yaw; angle = -angle * PI / 180; cv::Point2d line_pt[2]; line_pt[0].x = (int)(r * cos(angle) + px); line_pt[0].y = (int)(-r * sin(angle) + py); line_pt[1].x = (int)(-r * cos(angle) + px); line_pt[1].y = (int)(r * sin(angle) + py); cv::line(img, line_pt[0], line_pt[1], cv::Scalar(rgb[2], rgb[1], rgb[0]), size); } } } void _genXOYProjectionImage(cv::String& fileName, SVzNL3DLaserLine* scanData, int lineNum, std::vector& objOps, double rpy[3], double dirLen) { //统计X和Y的范围 std::vector> scan_lines; SVzNLRangeD x_range = {0, -1}; SVzNLRangeD y_range = {0, -1}; for (int line = 0; line < lineNum; line++) { std::vector< SVzNL3DPosition> a_line; for (int i = 0; i < scanData[line].nPositionCnt; i++) { SVzNL3DPosition* pt3D = &scanData[line].p3DPosition[i]; if (pt3D->pt3D.z < 1e-4) continue; a_line.push_back(*pt3D); if (x_range.max < x_range.min) { x_range.min = pt3D->pt3D.x; x_range.max = pt3D->pt3D.x; } else { if(x_range.min > pt3D->pt3D.x) x_range.min = pt3D->pt3D.x; if(x_range.max < pt3D->pt3D.x) x_range.max = pt3D->pt3D.x; } if (y_range.max < y_range.min) { y_range.min = pt3D->pt3D.y; y_range.max = pt3D->pt3D.y; } else { if (y_range.min > pt3D->pt3D.y) y_range.min = pt3D->pt3D.y; if (y_range.max < pt3D->pt3D.y) y_range.max = pt3D->pt3D.y; } } scan_lines.push_back(a_line); } int imgRows = 992; int imgCols = 1056; double y_rows = 960.0; double x_cols = 1024.0; cv::Mat img = cv::Mat::zeros(imgRows, imgCols, CV_8UC3); //计算投影比例 double x_scale = (x_range.max - x_range.min) / x_cols; double y_scale = (y_range.max - y_range.min) / y_rows; if (x_scale < y_scale) x_scale = y_scale; else y_scale = x_scale; int angleDrawLen = dirLen / x_scale; _XOYprojection(img, scan_lines, objOps, x_scale, y_scale, x_range, y_range, true, angleDrawLen); //旋转视角显示 double matrix3d[9]; EulerRpyToRotation1(rpy, matrix3d); std::vector r_objOps; r_objOps.insert(r_objOps.end(), objOps.begin(), objOps.end()); for (int i = 0; i < objOps.size(); i++) { SVzNL3DPoint c_pt = { objOps[i].centerPos .x, objOps[i].centerPos .y, objOps[i].centerPos .z}; SVzNL3DPoint r_c_pt = _ptRotate(c_pt, matrix3d); r_objOps[i].centerPos.x = r_c_pt.x; r_objOps[i].centerPos.y = r_c_pt.y; r_objOps[i].centerPos.z = r_c_pt.z; } std::vector> rotateLines; SVzNLRangeD rx_range, ry_range; _rotateCloudPts(scanData, lineNum, matrix3d, rotateLines, &rx_range, &ry_range); cv::Mat r_img = cv::Mat::zeros(imgRows, imgCols, CV_8UC3); //计算投影比例 double rx_scale = (rx_range.max - rx_range.min) / x_cols; double ry_scale = (ry_range.max - ry_range.min) / y_rows; _XOYprojection(r_img, rotateLines, r_objOps, rx_scale, ry_scale, rx_range, ry_range, false, angleDrawLen); cv::Mat dis_img; cv::hconcat(img, r_img, dis_img); cv::imwrite(fileName, dis_img); return; } void _RGBDto2DImage(char* fileName, SVzNLXYZRGBDLaserLine* scanData, int lineNum) { cv::String imgName(fileName); //统计X和Y的范围 SVzNLRangeD x_range = { 0, -1 }; SVzNLRangeD y_range = { 0, -1 }; for (int line = 0; line < lineNum; line++) { for (int i = 0; i < scanData[line].nPointCnt; i++) { SVzNLPointXYZRGBA* pt3D = &scanData[line].p3DPoint[i]; if (pt3D->z < 1e-4) continue; if (x_range.max < x_range.min) { x_range.min = pt3D->x; x_range.max = pt3D->x; } else { if (x_range.min > pt3D->x) x_range.min = pt3D->x; if (x_range.max < pt3D->x) x_range.max = pt3D->x; } if (y_range.max < y_range.min) { y_range.min = pt3D->y; y_range.max = pt3D->y; } else { if (y_range.min > pt3D->y) y_range.min = pt3D->y; if (y_range.max < pt3D->y) y_range.max = pt3D->y; } } } int x_skip = 16; int y_skip = 16; int imgRows = 992; int imgCols = 1056; double y_rows = (double)(imgRows - y_skip *2); double x_cols = (double)(imgCols - x_skip *2); cv::Mat img = cv::Mat::zeros(imgRows, imgCols, CV_8UC3); //计算投影比例 double x_scale = (x_range.max - x_range.min) / x_cols; double y_scale = (y_range.max - y_range.min) / y_rows; if (x_scale < y_scale) x_scale = y_scale; else y_scale = x_scale; int size = 1; cv::Vec3b rgb = cv::Vec3b(0, 0, 0); for (int line = 0; line < lineNum; line++) { for (int i = 0; i < scanData[line].nPointCnt; i++) { SVzNLPointXYZRGBA* pt3D = &scanData[line].p3DPoint[i]; if (pt3D->z < 1e-4) continue; int nRGB = pt3D->nRGB; int r = nRGB & 0xff; nRGB >>= 8; int g = nRGB & 0xff; nRGB >>= 8; int b = nRGB & 0xff; rgb[0] = r; rgb[1] = g; rgb[2] = b; size = 1; double x = pt3D->x; double y = pt3D->y; int px = (int)((x - x_range.min) / x_scale + x_skip); int py = (int)((y - y_range.min) / y_scale + y_skip); if ((px == 666) && (py == 828)) int kkk = 1; if (size == 1) img.at(py, px) = cv::Vec3b(rgb[2], rgb[1], rgb[0]); else cv::circle(img, cv::Point(px, py), size, cv::Scalar(rgb[2], rgb[1], rgb[0]), -1); } } cv::imwrite(imgName, img); return; } void _genXOYProjectionImage_RGBD(cv::String& fileName, SVzNLXYZRGBDLaserLine* scanData, int lineNum, std::vector& objOps, double rpy[3], double dirLen) { //统计X和Y的范围 std::vector> scan_lines; SVzNLRangeD x_range = { 0, -1 }; SVzNLRangeD y_range = { 0, -1 }; for (int line = 0; line < lineNum; line++) { std::vector< SVzNLPointXYZRGBA> a_line; for (int i = 0; i < scanData[line].nPointCnt; i++) { SVzNLPointXYZRGBA* pt3D = &scanData[line].p3DPoint[i]; if (pt3D->z < 1e-4) continue; a_line.push_back(*pt3D); if (x_range.max < x_range.min) { x_range.min = pt3D->x; x_range.max = pt3D->x; } else { if (x_range.min > pt3D->x) x_range.min = pt3D->x; if (x_range.max < pt3D->x) x_range.max = pt3D->x; } if (y_range.max < y_range.min) { y_range.min = pt3D->y; y_range.max = pt3D->y; } else { if (y_range.min > pt3D->y) y_range.min = pt3D->y; if (y_range.max < pt3D->y) y_range.max = pt3D->y; } } scan_lines.push_back(a_line); } int imgRows = 992; int imgCols = 1056; double y_rows = 960.0; double x_cols = 1024.0; cv::Mat img = cv::Mat::zeros(imgRows, imgCols, CV_8UC3); //计算投影比例 double x_scale = (x_range.max - x_range.min) / x_cols; double y_scale = (y_range.max - y_range.min) / y_rows; if (x_scale < y_scale) x_scale = y_scale; else y_scale = x_scale; int angleDrawLen = dirLen / x_scale; _XOYprojection_RGBD(img, scan_lines, objOps, x_scale, y_scale, x_range, y_range, true, angleDrawLen); //旋转视角显示 double matrix3d[9]; EulerRpyToRotation1(rpy, matrix3d); std::vector r_objOps; r_objOps.insert(r_objOps.end(), objOps.begin(), objOps.end()); for (int i = 0; i < objOps.size(); i++) { SVzNL3DPoint c_pt = { objOps[i].centerPos.x, objOps[i].centerPos.y, objOps[i].centerPos.z }; SVzNL3DPoint r_c_pt = _ptRotate(c_pt, matrix3d); r_objOps[i].centerPos.x = r_c_pt.x; r_objOps[i].centerPos.y = r_c_pt.y; r_objOps[i].centerPos.z = r_c_pt.z; } std::vector> rotateLines; SVzNLRangeD rx_range, ry_range; _rotateCloudPts_RGBD(scanData, lineNum, matrix3d, rotateLines, &rx_range, &ry_range); cv::Mat r_img = cv::Mat::zeros(imgRows, imgCols, CV_8UC3); //计算投影比例 double rx_scale = (rx_range.max - rx_range.min) / x_cols; double ry_scale = (ry_range.max - ry_range.min) / y_rows; _XOYprojection_RGBD(r_img, rotateLines, r_objOps, rx_scale, ry_scale, rx_range, ry_range, false, angleDrawLen); cv::Mat dis_img; cv::hconcat(img, r_img, dis_img); cv::imwrite(fileName, dis_img); return; } void _genXOYProjectionImage_sideBagInfo(cv::String& fileName, SVzNL3DLaserLine* scanData, int lineNum, std::vector& objOps, double rpy[3]) { //统计X和Y的范围 std::vector> scan_lines; SVzNLRangeD x_range = { 0, -1 }; SVzNLRangeD y_range = { 0, -1 }; for (int line = 0; line < lineNum; line++) { std::vector< SVzNL3DPosition> a_line; for (int i = 0; i < scanData[line].nPositionCnt; i++) { SVzNL3DPosition* pt3D = &scanData[line].p3DPosition[i]; if (pt3D->pt3D.z < 1e-4) continue; a_line.push_back(*pt3D); if (x_range.max < x_range.min) { x_range.min = pt3D->pt3D.x; x_range.max = pt3D->pt3D.x; } else { if (x_range.min > pt3D->pt3D.x) x_range.min = pt3D->pt3D.x; if (x_range.max < pt3D->pt3D.x) x_range.max = pt3D->pt3D.x; } if (y_range.max < y_range.min) { y_range.min = pt3D->pt3D.y; y_range.max = pt3D->pt3D.y; } else { if (y_range.min > pt3D->pt3D.y) y_range.min = pt3D->pt3D.y; if (y_range.max < pt3D->pt3D.y) y_range.max = pt3D->pt3D.y; } } scan_lines.push_back(a_line); } double x_scale = 1.0; double y_scale = 1.0; int x_rows = int((x_range.max - x_range.min) / x_scale); int y_rows = int((y_range.max - y_range.min) / y_scale); if (x_rows % 2 > 0) x_rows += 1; if (y_rows % 2 > 0) y_rows += 1; int imgRows = y_rows + 32; int imgCols = x_rows + 32; cv::Mat img = cv::Mat::zeros(imgRows, imgCols, CV_8UC3); //计算投影比例 _XOYprojection_sideBagInfo(img, scan_lines, objOps, x_scale, y_scale, x_range, y_range); #if 0 //旋转视角显示 double matrix3d[9]; EulerRpyToRotation1(rpy, matrix3d); std::vector r_objOps; r_objOps.insert(r_objOps.end(), objOps.begin(), objOps.end()); for (int i = 0; i < objOps.size(); i++) { SVzNL3DPoint c_pt = { objOps[i].graspPos.x, objOps[i].graspPos.y, objOps[i].graspPos.z }; SVzNL3DPoint r_c_pt = _ptRotate(c_pt, matrix3d); r_objOps[i].graspPos.x = r_c_pt.x; r_objOps[i].graspPos.y = r_c_pt.y; r_objOps[i].graspPos.z = r_c_pt.z; } std::vector> rotateLines; SVzNLRangeD rx_range, ry_range; _rotateCloudPts(scanData, lineNum, matrix3d, rotateLines, &rx_range, &ry_range); cv::Mat r_img = cv::Mat::zeros(imgRows, imgCols, CV_8UC3); //计算投影比例 double rx_scale = (rx_range.max - rx_range.min) / x_cols; double ry_scale = (ry_range.max - ry_range.min) / y_rows; _XOYprojection_sideBagInfo(r_img, rotateLines, r_objOps, rx_scale, ry_scale, rx_range, ry_range); cv::Mat dis_img; cv::hconcat(img, r_img, dis_img); cv::imwrite(fileName, dis_img); #else cv::Mat rot; cv::rotate(img, rot, cv::ROTATE_90_CLOCKWISE); // 顺时针90°旋转 cv::flip(rot, img, 1); // 左右翻转 cv::imwrite(fileName, img); #endif return; } //量化成640*640左右大小的图像。 void project2DWithInterpolate(cv::Mat& img, SVzNL3DLaserLine* scanData, int lineNum, double fixed_xy_scale, double z_scale) { double namedSize = 640.0;//目标图像最大尺度为640像素 SVzNLRangeD x_range = { 0, -1 }; SVzNLRangeD y_range = { 0, -1 }; SVzNLRangeD z_range = { 0, -1 }; for (int line = 0; line < lineNum; line++) { for (int i = 0; i < scanData[line].nPositionCnt; i++) { SVzNL3DPosition* pt3D = &scanData[line].p3DPosition[i]; if (pt3D->pt3D.z < 1e-4) continue; if (x_range.max < x_range.min) { x_range.min = pt3D->pt3D.x; x_range.max = pt3D->pt3D.x; } else { if (x_range.min > pt3D->pt3D.x) x_range.min = pt3D->pt3D.x; if (x_range.max < pt3D->pt3D.x) x_range.max = pt3D->pt3D.x; } if (y_range.max < y_range.min) { y_range.min = pt3D->pt3D.y; y_range.max = pt3D->pt3D.y; } else { if (y_range.min > pt3D->pt3D.y) y_range.min = pt3D->pt3D.y; if (y_range.max < pt3D->pt3D.y) y_range.max = pt3D->pt3D.y; } if (z_range.max < z_range.min) { z_range.min = pt3D->pt3D.z; z_range.max = pt3D->pt3D.z; } else { if (z_range.min > pt3D->pt3D.z) z_range.min = pt3D->pt3D.z; if (z_range.max < pt3D->pt3D.z) z_range.max = pt3D->pt3D.z; } } } double xy_scale = fixed_xy_scale; if (fixed_xy_scale < 1e-4) { double x_scale = (x_range.max - x_range.min) / namedSize; double y_scale = (y_range.max - y_range.min) / namedSize; double xy_scale; if (x_scale < y_scale) xy_scale = y_scale; else xy_scale = x_scale; } int img_rows = (int)((y_range.max - y_range.min) / xy_scale)+1; if(img_rows %2 >0) img_rows +=1; int img_cols = (int)((x_range.max - x_range.min) / xy_scale)+ 1; if (img_cols % 2 > 0) img_cols += 1; img = cv::Mat::zeros(img_rows, img_cols, CV_8UC1); int polateWin = 5; for (int line = 0; line < lineNum; line++) { //同时进行垂直插值 int pre_py = -1; SVzNL3DPosition* pre_pt3D = NULL; int pre_i = -1; for (int i = 0; i < scanData[line].nPositionCnt; i++) { SVzNL3DPosition* pt3D = &scanData[line].p3DPosition[i]; if (pt3D->pt3D.z < 1e-4) continue; double x = pt3D->pt3D.x; double y = pt3D->pt3D.y; int px = (int)((x - x_range.min) / xy_scale); int py = (int)((y - y_range.min) / xy_scale); int value = (int)((pt3D->pt3D.z - z_range.min) / z_scale); if (value > 254) value = 254; value = 255 - value; img.at(py, px) = (uchar)value; //检查是否需要插值 if ( (py > pre_py + 1)&& (py <= pre_py + polateWin + 1) && (pre_py >= 0) && (pre_i >= 0) && (i == pre_i + 1)) //3D点连续,量化点不连续,插值 { double dist = double(py - pre_py); for (int iy = pre_py + 1; iy < py; iy++) { double k1 = (double)(iy - pre_py) / dist; double k0 = 1 - k1; double inter_x = pre_pt3D->pt3D.x * k0 + pt3D->pt3D.x * k1; double inter_y = pre_pt3D->pt3D.y * k0 + pt3D->pt3D.y * k1; double inter_z = pre_pt3D->pt3D.z * k0 + pt3D->pt3D.z * k1; int polate_px = (int)((inter_x - x_range.min) / xy_scale); int polate_py = (int)((inter_y - y_range.min) / xy_scale); int polate_value = (int)((inter_z - z_range.min) / z_scale); if (polate_value > 254) polate_value = 254; polate_value = 255 - polate_value; img.at(polate_py, polate_px) = (uchar)polate_value; } } pre_i = i; pre_py = py; pre_pt3D = pt3D; } } //水平插值 for (int y = 0; y < img.rows; y++) { int pre_x = -1; uchar pre_value = 0; for (int x = 0; x < img.cols; x++) { uchar value = img.at(y, x); if (value > 0) { if ((x > pre_x + 1) && (x <= pre_x + polateWin + 1) &&(pre_x >= 0)) //水平不连续,插值 { double dist = double(x - pre_x); for (int ix = pre_x + 1; ix < x; ix++) { double k1 = (double)(ix - pre_x) / dist; double k0 = 1 - k1; double inter_data = (double)pre_value * k0 + (double)value * k1; if (inter_data > 255) inter_data = 255; uchar polate_value = (uchar)inter_data; img.at(y, ix) = polate_value; } } pre_x = x; pre_value = value; } } } } void _outputScanDataFile_removeZeros(char* fileName, SVzNL3DLaserLine* scanData, int lineNum, float lineV, int maxTimeStamp, int clockPerSecond) { std::ofstream sw(fileName); sw << "LineNum:" << lineNum << std::endl; sw << "DataType: 0" << std::endl; sw << "ScanSpeed:" << lineV << std::endl; sw << "PointAdjust: 1" << std::endl; sw << "MaxTimeStamp:" << maxTimeStamp << "_" << clockPerSecond << std::endl; for (int line = 0; line < lineNum; line++) { int realNum = 0; for (int i = 0; i < scanData[line].nPositionCnt; i++) { if (scanData[line].p3DPosition[i].pt3D.z > 1e-4) realNum++; } sw << "Line_" << line << "_" << scanData[line].nTimeStamp << "_" << realNum << std::endl; for (int i = 0; i < scanData[line].nPositionCnt; i++) { if (scanData[line].p3DPosition[i].pt3D.z > 1e-4) { SVzNL3DPosition* pt3D = &scanData[line].p3DPosition[i]; float x = (float)pt3D->pt3D.x; float y = (float)pt3D->pt3D.y; float z = (float)pt3D->pt3D.z; sw << "{ " << x << "," << y << "," << z << " }-"; sw << "{0,0}-{0,0}" << std::endl; } } } sw.close(); } #define TEST_CONVERT_TO_GRID 0 #define TEST_CONVERT_RGBD_TO_GRID 0 #define TEST_COMPUTE_GRASP_POINT 1 #define TEST_COMPUTE_CALIB_PARA 0 #define TEST_GROUP 21 #define TEST_TOP_VIEW_GROUP (TEST_GROUP - 8) #define TEST_TOP_ORIEN_GROUP (TEST_GROUP - 6) int main() { #if TEST_CONVERT_RGBD_TO_GRID char _scan_dir[256]; sprintf_s(_scan_dir, "F:\\ShangGu\\编织袋数据\\编织袋RGBD识别方向_2\\"); char _in_file[256]; double _F = 1247.95;// 1231.2; //1729.0;; //f for (int i = 1; i <= 28; i++) { sprintf_s(_in_file, "%sLaserLine%d.txt", _scan_dir, i); std::vector> scanData; vzReadLaserScanPointFromFile_XYZRGB_vector(_in_file, scanData); if (scanData.size() == 0) continue; std::vector> gridData; _convertToGridData_XYZRGB_vector(scanData, _F, gridData); char _out_file[256]; sprintf_s(_out_file, "%s%d-RGB点云.txt", _scan_dir, i); _outputScanDataFile_SPointXYZRGB_vector(_out_file, gridData); printf("%s: convert done!\n", _in_file); } #endif #if TEST_CONVERT_TO_GRID //将数据转换成栅格格式格式 char _scan_dir[256]; int lineNum = 0; float lineV = 0.0f; int dataCalib = 0; int maxTimeStamp = 0; int clockPerSecond = 0; sprintf_s(_scan_dir, "F:\\ShangGu\\编织袋数据\\编织袋RGBD识别方向_2\\"); char _scan_file[256]; double _F = 1247.95;// 1231.2; //1729.0;; //f for (int i = 1; i <= 28; i++) { sprintf_s(_scan_file, "%sLaserLine%d.txt", _scan_dir, i); SVzNLXYZRGBDLaserLine* laser3DPoints_RGBD = vzReadLaserScanPointFromFile_XYZRGB(_scan_file, &lineNum, &lineV, &dataCalib, &maxTimeStamp, &clockPerSecond); if (laser3DPoints_RGBD == NULL) continue; #if 0 double rpy[3] = { 0,-18, 0 }; double camPoseR[9]; EulerRpyToRotation1(rpy, camPoseR); #else double camPoseR[9] = { 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0 }; #endif int vldLineNum = 0; SVzNL3DLaserLine* gridData = _convertToGridData_XYZRGB(laser3DPoints_RGBD, lineNum, _F, camPoseR, &vldLineNum); //生成水平扫描数据 int hLineNum = gridData[0].nPositionCnt; SVzNL3DLaserLine* hScanData = (SVzNL3DLaserLine*)malloc(sizeof(SVzNL3DLaserLine) * hLineNum); memset(hScanData, 0, sizeof(SVzNL3DLaserLine) * hLineNum); for (int hLine = 0; hLine < hLineNum; hLine++) { hScanData[hLine].nPositionCnt = vldLineNum; hScanData[hLine].nTimeStamp = hLine * 1000; hScanData[hLine].p3DPosition = (SVzNL3DPosition*)malloc(sizeof(SVzNL3DPosition) * lineNum); memset(hScanData[hLine].p3DPosition, 0, sizeof(SVzNL3DPosition) * lineNum); for (int m = 0; m < vldLineNum; m++) { hScanData[hLine].p3DPosition[m].nPointIdx = m; hScanData[hLine].p3DPosition[m].pt3D.x = gridData[m].p3DPosition[hLine].pt3D.y; hScanData[hLine].p3DPosition[m].pt3D.y = gridData[m].p3DPosition[hLine].pt3D.x; hScanData[hLine].p3DPosition[m].pt3D.z = gridData[m].p3DPosition[hLine].pt3D.z; } } char _out_file[256]; sprintf_s(_out_file, "%sLaserLine%d_grid.txt", _scan_dir, i); _outputScanDataFile_self(_out_file, gridData, vldLineNum, lineV, maxTimeStamp, clockPerSecond); sprintf_s(_out_file, "%sLaserLine%d_hScanData.txt", _scan_dir, i); _outputScanDataFile_removeZeros(_out_file, hScanData, hLineNum, lineV, maxTimeStamp, clockPerSecond); printf("%s: convert done!\n", _scan_file); } #endif #if TEST_COMPUTE_CALIB_PARA char _calib_datafile[256]; sprintf_s(_calib_datafile, "F:\\ShangGu\\编织袋数据\\编织袋RGBD识别方向\\底面点云\\底面点云.txt"); int lineNum = 0; float lineV = 0.0f; int dataCalib = 0; int maxTimeStamp = 0; int clockPerSecond = 0; SVzNL3DLaserLine* laser3DPoints = vzReadLaserScanPointFromFile_XYZ(_calib_datafile, &lineNum, &lineV, &dataCalib, &maxTimeStamp, &clockPerSecond); if (laser3DPoints) { SSG_planeCalibPara calibPara = sg_getBagBaseCalibPara( laser3DPoints, lineNum); //结果进行验证 for (int i = 0; i < lineNum; i++) { if (i == 14) int kkk = 1; //行处理 //调平,去除地面 sg_lineDataR(&laser3DPoints[i], calibPara.planeCalib, -1);// calibPara.planeHeight); } // char calibFile[250]; sprintf_s(calibFile, "F:\\ShangGu\\编织袋数据\\编织袋RGBD识别方向\\ground_calib_para.txt"); _outputCalibPara(calibFile, calibPara); char _out_file[256]; sprintf_s(_out_file, "F:\\ShangGu\\编织袋数据\\编织袋RGBD识别方向\\LaserLine1_grid_calib.txt"); _outputScanDataFile_self(_out_file, laser3DPoints, lineNum, lineV, maxTimeStamp, clockPerSecond); printf("%s: calib done!\n", _calib_datafile); } #endif #if TEST_COMPUTE_GRASP_POINT const char* dataPath[TEST_GROUP] = { "F:\\ShangGu\\编织袋数据\\点云1\\", //0 "F:\\ShangGu\\编织袋数据\\点云2\\", //1 "F:\\ShangGu\\编织袋数据\\点云3\\", //2 "F:\\ShangGu\\编织袋数据\\点云4_广东\\", //3 "F:\\ShangGu\\编织袋数据\\点云5_广东\\", //4 "F:\\ShangGu\\编织袋数据\\点云6_河南平顶山\\", //5 "F:\\ShangGu\\编织袋数据\\点云7_广东1214\\", //6 "F:\\ShangGu\\编织袋数据\\点云8_广东1213-1215数据\\", //7 "F:\\ShangGu\\编织袋数据\\点云9_广东1219数据\\", //8 "F:\\ShangGu\\编织袋数据\\点云10\\", //9 "F:\\ShangGu\\编织袋数据\\点云11_盐袋\\", //10 "F:\\ShangGu\\编织袋数据\\点云12_盐袋\\", //11 "F:\\ShangGu\\编织袋数据\\点云13_现场测试\\", //12 "F:\\ShangGu\\编织袋数据\\编织袋RGBD识别方向\\", //13 "F:\\ShangGu\\编织袋数据\\编织袋RGBD识别方向_2\\", //14 "F:\\ShangGu\\编织袋数据\\侧抓数据\\", //15 "F:\\ShangGu\\编织袋数据\\侧抓数据_现场\\20250419\\", //16 "F:\\ShangGu\\编织袋数据\\侧抓数据_现场\\20250420-1\\", //17 "F:\\ShangGu\\编织袋数据\\侧抓数据_现场\\20250420-2\\", //18 "F:\\ShangGu\\编织袋数据\\侧抓数据_现场\\20250420-3\\", //19 "F:\\ShangGu\\编织袋数据\\侧抓数据_现场\\20250420-4\\", //20 }; SVzNLRange fileIdx[TEST_GROUP] = { {0,176},{1,200},{1,166},{122,141},{1,65}, {1,29},{108,135},{0,200}, {1,200}, {1,12}, {2,4}, {1,5}, {1,1}, {1,21},{1,28}, {3,3}, {1,51}, {4,83}, {1,74}, {1,61}, {1,84} }; SSG_planeCalibPara poseCalibPara; //初始化成单位阵 poseCalibPara.planeCalib[0] = 1.0; poseCalibPara.planeCalib[1] = 0.0; poseCalibPara.planeCalib[2] = 0.0; poseCalibPara.planeCalib[3] = 0.0; poseCalibPara.planeCalib[4] = 1.0; poseCalibPara.planeCalib[5] = 0.0; poseCalibPara.planeCalib[6] = 0.0; poseCalibPara.planeCalib[7] = 0.0; poseCalibPara.planeCalib[8] = 1.0; poseCalibPara.planeHeight = -1.0; for (int i = 0; i < 9; i++) poseCalibPara.invRMatrix[i] = poseCalibPara.planeCalib[i]; char _scan_file[256]; SG_bagPositionParam algoParam; int endGroup = TEST_GROUP - 1; for (int grp = 14; grp <= 14; grp++) { if (grp < 10) { algoParam.bagParam.bagL = 650; //袋子长65cm algoParam.bagParam.bagW = 450; //袋子宽40cm algoParam.bagParam.bagH = 160; //袋子高16cm algoParam.growParam.maxLineSkipNum = 5; algoParam.growParam.yDeviation_max = 20.0; algoParam.growParam.maxSkipDistance = 20.0; algoParam.growParam.zDeviation_max = algoParam.bagParam.bagH / 2; //袋子高度1/2 algoParam.growParam.minLTypeTreeLen = 50.0; //mm algoParam.growParam.minVTypeTreeLen = 50.0; //mm } else if ( (grp >= 10) && (grp <= 11)) { algoParam.bagParam.bagL = 150; //袋子长65cm algoParam.bagParam.bagW = 110; //袋子宽40cm algoParam.bagParam.bagH = 20; //袋子高16cm algoParam.growParam.maxLineSkipNum = 5; algoParam.growParam.yDeviation_max = 20.0; algoParam.growParam.maxSkipDistance = 20.0; algoParam.growParam.zDeviation_max = algoParam.bagParam.bagH / 2; //袋子高度1/2 algoParam.growParam.minLTypeTreeLen = 50.0; //mm algoParam.growParam.minVTypeTreeLen = 50.0; //mm } else if ( (grp >= 13) && (grp <= 14)) { algoParam.bagParam.bagL = 750; //袋子长65cm algoParam.bagParam.bagW = 450; //袋子宽40cm algoParam.bagParam.bagH = 160; //袋子高16cm algoParam.growParam.maxLineSkipNum = 5; algoParam.growParam.yDeviation_max = 20.0; algoParam.growParam.maxSkipDistance = 20.0; algoParam.growParam.zDeviation_max = algoParam.bagParam.bagH / 2; //袋子高度1/2 algoParam.growParam.minLTypeTreeLen = 50.0; //mm algoParam.growParam.minVTypeTreeLen = 50.0; //mm } else { algoParam.bagParam.bagL = 650; //袋子长65cm algoParam.bagParam.bagW = 450; //袋子宽40cm algoParam.bagParam.bagH = 160; //袋子高16cm algoParam.growParam.maxLineSkipNum = 5; algoParam.growParam.yDeviation_max = 20.0; algoParam.growParam.maxSkipDistance = 20.0; algoParam.growParam.zDeviation_max = algoParam.bagParam.bagH / 2; //袋子高度1/2 algoParam.growParam.minLTypeTreeLen = 50.0; //mm algoParam.growParam.minVTypeTreeLen = 50.0; //mm } #if BAG_ALGO_USE_CORNER_FEATURE algoParam.cornerParam.cornerTh = 30; //45度角 algoParam.cornerParam.scale = algoParam.bagParam.bagH / 8; // 15; // algoParam.bagParam.bagH / 8; algoParam.cornerParam.minEndingGap = algoParam.bagParam.bagW / 4; algoParam.cornerParam.jumpCornerTh_1 = 60; algoParam.cornerParam.jumpCornerTh_2 = 15; #else algoParam.slopeParam.LSlopeZWin = 10.0; algoParam.slopeParam.validSlopeH = 10.0; algoParam.slopeParam.minLJumpH = 20.0; algoParam.slopeParam.minEndingGap = algoParam.bagParam.bagW / 4; ///当没有中间袋子,只有左右袋子时,有两组Ending。此参数确定两个ending最小间距 algoParam.valleyPara.valleyMinH = 10.0; algoParam.valleyPara.valleyMaxW = 80.0; //取袋子宽度的1/5 #endif if (grp == 7) { char calibFile[250]; sprintf_s(calibFile, "F:\\上古\\编织袋数据\\点云8_广东1213-1215数据\\ground_calib_para.txt"); poseCalibPara = _readCalibPara(calibFile); } else if (grp == 10) { char calibFile[250]; sprintf_s(calibFile, "F:\\ShangGu\\编织袋数据\\点云11_盐袋\\ground_calib_para.txt"); poseCalibPara = _readCalibPara(calibFile); } else if (grp == 11) { char calibFile[250]; sprintf_s(calibFile, "F:\\ShangGu\\编织袋数据\\点云12_盐袋\\ground_calib_para.txt"); poseCalibPara = _readCalibPara(calibFile); } else if (grp == 12) { char calibFile[250]; sprintf_s(calibFile, "F:\\ShangGu\\编织袋数据\\点云13_现场测试\\ground_calib_para.txt"); poseCalibPara = _readCalibPara(calibFile); } else if ( grp == 13) { char calibFile[250]; sprintf_s(calibFile, "F:\\ShangGu\\编织袋数据\\编织袋RGBD识别方向\\ground_calib_para.txt"); poseCalibPara = _readCalibPara(calibFile); } else if (grp == 14) { char calibFile[250]; sprintf_s(calibFile, "F:\\ShangGu\\编织袋数据\\编织袋RGBD识别方向_2\\ground_calib_para.txt"); poseCalibPara = _readCalibPara(calibFile); } for (int fidx = fileIdx[grp].nMin; fidx <= fileIdx[grp].nMax; fidx++) { //fidx = 10; if (grp < TEST_TOP_VIEW_GROUP) //正面抓取 { int lineNum = 0; float lineV = 0.0f; int dataCalib = 0; int maxTimeStamp = 0; int clockPerSecond = 0; sprintf_s(_scan_file, "%sLaserLine%d_grid.txt", dataPath[grp], fidx); SVzNL3DLaserLine* laser3DPoints = vzReadLaserScanPointFromFile_XYZ(_scan_file, &lineNum, &lineV, &dataCalib, &maxTimeStamp, &clockPerSecond); if (laser3DPoints == NULL) continue; algoParam.filterParam.continuityTh = 20.0; //噪声滤除。当相邻点的z跳变大于此门限时,检查是否为噪声。若长度小于outlierLen, 视为噪声 algoParam.filterParam.outlierTh = 5; long t1 = GetTickCount64(); #if 0 int errCode = 0; std::vector all_vLineFeatures; std::vector> all_vLineNoises; for (int i = 0; i < lineNum; i++) { if (i == 14) int kkk = 1; //行处理 sg_lineDataR(&laser3DPoints[i], camPoseR); //sg_bagPositioning_lineProc(&laser3DPoints[i], i, &errCode, all_vLineFeatures, all_vLineNoises, algoParam); } std::vector objOps; sg_getBagPosition(laser3DPoints, lineNum, all_vLineFeatures, all_vLineNoises, algoParam, objOps); #else for (int i = 0; i < lineNum; i++) { if (i == 14) int kkk = 1; //行处理 //调平,去除地面 sg_lineDataR(&laser3DPoints[i], poseCalibPara.planeCalib, poseCalibPara.planeHeight); } std::vector objOps; sg_getBagPosition(laser3DPoints, lineNum, algoParam, poseCalibPara, objOps); #endif long t2 = GetTickCount64(); char _dbg_file[256]; #if 1 sprintf_s(_dbg_file, "%sresult\\LaserLine%d_result.txt", dataPath[grp], fidx); _outputScanDataFile_RGBD_obj(_dbg_file, laser3DPoints, lineNum, lineV, maxTimeStamp, clockPerSecond, objOps); sprintf_s(_dbg_file, "%sresult\\LaserLine%d_result_img.png", dataPath[grp], fidx); cv::String imgName(_dbg_file); double rpy[3] = { -30, 15, 0 }; //{ 0,-45, 0 }; // double angleDrawLen = algoParam.bagParam.bagL / 3; _genXOYProjectionImage(imgName, laser3DPoints, lineNum, objOps, rpy, angleDrawLen); #endif #if 0 //量化成2D图像作为训练样本 cv::Mat project2D_img; double z_scale = 3.0; double xy_scale = 2.0; project2DWithInterpolate(project2D_img, laser3DPoints, lineNum, xy_scale, z_scale); cv::Mat project2D_color; cv::applyColorMap(project2D_img, project2D_color, cv::COLORMAP_JET); sprintf_s(_dbg_file, "%simg2D_JET\\LaserLine%d_img2D.png", dataPath[grp], fidx); cv::String img2DName(_dbg_file); cv::imwrite(img2DName, project2D_color); cv::Mat project2D_color_HOT; cv::applyColorMap(project2D_img, project2D_color_HOT, cv::COLORMAP_HOT); sprintf_s(_dbg_file, "%simg2D\\LaserLine%d_img2D.png", dataPath[grp], fidx); cv::String img2DName_HOT(_dbg_file); cv::imwrite(img2DName_HOT, project2D_color_HOT); #endif printf("%s: %d(ms)!\n", _scan_file, (int)(t2 - t1)); } else if (grp < TEST_TOP_ORIEN_GROUP) { double FBVldPtRatioTh; SSG_hsvCmpParam colorCmpParam; //色度和色饱和度比较门限,小于门限为同一颜色 colorCmpParam.hueTh = 15.0; colorCmpParam.saturateTh = 120.0; //60 colorCmpParam.FBVldPtRatioTh = 0.075; //正反两面有效颜色点的比例门限 colorCmpParam.frontVldPtGreater = true; //true:有效颜色比例高的点的是正面;false:有效颜色比例高的点的是反面 colorCmpParam.front_upVldPtGreater = false;//true:有效颜色比例高的点的是正面朝上;false:有效颜色比例高的点的是正面朝下 colorCmpParam.back_upVldPtGreater = true; //true:有效颜色比例高的点的是反面朝上;false:有效颜色比例高的点的是反面朝下 RGB rgbColorPattern = {36, 165, 208}; //用于区分袋子方向的颜色 double frontColorTemplate[RGN_HIST_SIZE] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; double backColorTemplate[RGN_HIST_SIZE] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; int lineNum = 0; float lineV = 0.0f; int dataCalib = 0; int maxTimeStamp = 0; int clockPerSecond = 0; sprintf_s(_scan_file, "%s%d-RGB点云.txt", dataPath[grp], fidx); SVzNLXYZRGBDLaserLine* laser3DPoints = vzReadLaserScanPointFromFile_XYZRGB( _scan_file, &lineNum, &lineV, &dataCalib, &maxTimeStamp, &clockPerSecond); if (laser3DPoints == NULL) continue; algoParam.filterParam.continuityTh = 20.0; //噪声滤除。当相邻点的z跳变大于此门限时,检查是否为噪声。若长度小于outlierLen, 视为噪声 algoParam.filterParam.outlierTh = 5; long t1 = GetTickCount64(); for (int i = 0; i < lineNum; i++) { if (i == 14) int kkk = 1; //行处理 //调平,去除地面 sg_lineDataR_RGBD(&laser3DPoints[i], poseCalibPara.planeCalib, poseCalibPara.planeHeight); } #if 0 //产生一个调平后的2D图像,核对颜色 char _tst_file[256]; sprintf_s(_tst_file, "%sresult\\LaserLine%d_2D_img.png", dataPath[grp], fidx); _RGBDto2DImage(_tst_file, laser3DPoints, lineNum); #endif int errCode = 0; std::vector objOps; #if OUTPUT_DEBUG std::vector> debug_bagPositionCloud; #endif sg_getBagPositionAndOrientation( laser3DPoints, lineNum, algoParam, poseCalibPara, colorCmpParam, rgbColorPattern, frontColorTemplate, backColorTemplate, objOps, #if OUTPUT_DEBUG debug_bagPositionCloud, #endif &errCode); long t2 = GetTickCount64(); char _dbg_file[256]; #if 1 #if OUTPUT_DEBUG sprintf_s(_dbg_file, "%sresult\\LaserLine%d_bagPosition_result.txt", dataPath[grp], fidx); _outputScanDataFile_obj_vector(_dbg_file, debug_bagPositionCloud, objOps); #endif sprintf_s(_dbg_file, "%sresult\\LaserLine%d_result.txt", dataPath[grp], fidx); _outputRGBDScanDataFile_RGBD_obj(_dbg_file, laser3DPoints, lineNum, lineV, maxTimeStamp, clockPerSecond, objOps); sprintf_s(_dbg_file, "%sresult\\LaserLine%d_result_img.png", dataPath[grp], fidx); cv::String imgName(_dbg_file); double rpy[3] = { -30, 15, 0 }; //{ 0,-45, 0 }; // double angleDrawLen = algoParam.bagParam.bagL / 3; _genXOYProjectionImage_RGBD(imgName, laser3DPoints, lineNum, objOps, rpy, angleDrawLen); #endif #if 0 //量化成2D图像作为训练样本 cv::Mat project2D_img; double z_scale = 3.0; double xy_scale = 2.0; project2DWithInterpolate(project2D_img, laser3DPoints, lineNum, xy_scale, z_scale); cv::Mat project2D_color; cv::applyColorMap(project2D_img, project2D_color, cv::COLORMAP_JET); sprintf_s(_dbg_file, "%simg2D_JET\\LaserLine%d_img2D.png", dataPath[grp], fidx); cv::String img2DName(_dbg_file); cv::imwrite(img2DName, project2D_color); cv::Mat project2D_color_HOT; cv::applyColorMap(project2D_img, project2D_color_HOT, cv::COLORMAP_HOT); sprintf_s(_dbg_file, "%simg2D\\LaserLine%d_img2D.png", dataPath[grp], fidx); cv::String img2DName_HOT(_dbg_file); cv::imwrite(img2DName_HOT, project2D_color_HOT); #endif printf("%s: %d(ms)!\n", _scan_file, (int)(t2 - t1)); } else// if (grp >= TEST_TOP_VIEW_GROUP) //侧面抓取 { int lineNum = 0; float lineV = 0.0f; int dataCalib = 0; int maxTimeStamp = 0; int clockPerSecond = 0; sprintf_s(_scan_file, "%sLaserLine%d_grid.txt", dataPath[grp], fidx); SVzNL3DLaserLine* laser3DPoints = vzReadLaserScanPointFromFile_XYZ(_scan_file, &lineNum, &lineV, &dataCalib, &maxTimeStamp, &clockPerSecond); if (laser3DPoints == NULL) continue; algoParam.filterParam.continuityTh = 5.0; //噪声滤除。当相邻点的距离大于此门限时,检查是否为噪声。若长度小于outlierLen, 视为噪声 algoParam.filterParam.outlierTh = 5; algoParam.bagParam.bagL = 650; //袋子长65cm algoParam.bagParam.bagW = 450; //袋子宽40cm algoParam.bagParam.bagH = 80; //袋子高16cm algoParam.growParam.maxLineSkipNum = 5; algoParam.growParam.yDeviation_max = 20.0; algoParam.growParam.zDeviation_max = 8.0; long t1 = GetTickCount64(); int errCode = 0; SSG_stackBaseParam stackBaseParam = {200, 260, 550}; SSG_treeGrowParam stackBaseGrowParam; memset(&stackBaseGrowParam, 0, sizeof(SSG_treeGrowParam)); stackBaseGrowParam.maxLineSkipNum = 10; stackBaseGrowParam.yDeviation_max = 20.0; stackBaseGrowParam.zDeviation_max = 20.0; SSG_6DOF stackBasePosition; sg_getSideBagStackBasePosition( laser3DPoints, lineNum, stackBaseParam, stackBaseGrowParam, &stackBasePosition //垛的托盘的中心位置和角度 ); //行列转置。侧面扫描时激光线水平,所以要按列处理 std::vector objOps; //sg_sideBagPosition(laser3DPoints, lineNum, algoParam, objOps); long t2 = GetTickCount64(); char _dbg_file[256]; sprintf_s(_dbg_file, "%sresult\\LaserLine%d_result.txt", dataPath[grp], fidx); _outputScanDataFile_RGBD_sideBagObj(_dbg_file, laser3DPoints, lineNum, lineV, maxTimeStamp, clockPerSecond, objOps); sprintf_s(_dbg_file, "%sresult\\LaserLine%d_result_img.png", dataPath[grp], fidx); cv::String imgName(_dbg_file); double rpy[3] = { -30, 15, 0 }; //{ 0,-45, 0 }; // _genXOYProjectionImage_sideBagInfo(imgName, laser3DPoints, lineNum, objOps, rpy); printf("%s: %d(ms)!\n", _scan_file, (int)(t2 - t1)); } } } #endif printf("all done!\n"); } // 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单 // 调试程序: F5 或调试 >“开始调试”菜单 // 入门使用技巧: // 1. 使用解决方案资源管理器窗口添加/管理文件 // 2. 使用团队资源管理器窗口连接到源代码管理 // 3. 使用输出窗口查看生成输出和其他消息 // 4. 使用错误列表窗口查看错误 // 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目 // 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件