#define U_SEGSt(a) \ gettimeofday(&tvsv, 0); \ a = tvsv.tv_sec + tvsv.tv_usec / 1000000.0 struct timeval tvsv; double t1sv, t2sv, t0sv, t3sv; void tic_initsv() { U_SEGSt(t0sv); } void toc_finalsv(double &time) { U_SEGSt(t3sv); time = (t3sv - t0sv) / 1; } void ticsv() { U_SEGSt(t1sv); } void tocsv() { U_SEGSt(t2sv); } // std::cout << (t2sv - t1sv)/1 << std::endl;}这段代码是一组用于测量 时间间隔 的辅助函数。以下是对代码的解释:
总结来说,这段代码定义了一些用于测量时间的函数和宏。这些函数使用gettimeofday函数获取当前时间,并将其转换为秒数。通过记录起始时间戳和结束时间戳,可以计算时间间隔。
2、SegmentDynObject::SegmentDynObject() 是一个类构造函数,用于初始化 SegmentDynObject 类的对象。
SegmentDynObject::SegmentDynObject() { std::cout << "Importing Mask R-CNN Settings..." << std::endl; ImportSettings(); std::string x; setenv("PYTHONPATH", this->py_path.c_str(), 1); x = getenv("PYTHONPATH"); Py_Initialize(); this->cvt = new NDArrayConverter(); this->py_module = PyImport_ImportModule(this->module_name.c_str()); assert(this->py_module != NULL); this->py_class = PyObject_GetAttrString(this->py_module, this->class_name.c_str()); assert(this->py_class != NULL); this->net = PyInstance_New(this->py_class, NULL, NULL); assert(this->net != NULL); std::cout << "Creating net instance..." << std::endl; cv::Mat image = cv::Mat::zeros(480, 640, CV_8UC3); // Be careful with size!! std::cout << "Loading net parameters..." << std::endl; GetSegmentation(image); }下面是对代码的逐行解释:
综上所述,SegmentDynObject::SegmentDynObject() 构造函数主要进行了一系列初始化操作,包括导入设置,设置环境变量,初始化 Python 解释器,导入 Python 模块,并创建 Python 类的对象。最后,它创建一个黑色图像对象并调用 GetSegmentation 函数对图像进行分割处理。
3、SegmentDynObject::~SegmentDynObject() 是类的析构函数,用于释放类对象所使用的资源和内存,在对象销毁时被调用。
SegmentDynObject::~SegmentDynObject() { delete this->py_module; delete this->py_class; delete this->net; delete this->cvt; }下面是对代码的逐行解释:
通过在析构函数中使用 delete 操作符释放对象所占用的内存,可以确保在对象销毁时释放资源,防止内存泄漏和资源泄漏。
4、这段代码定义了 SegmentDynObject 类的一个成员函数 GetSegmentation(),用于获取图像的分割结果。
cv::Mat SegmentDynObject::GetSegmentation(cv::Mat &image, std::string dir, std::string name) { cv::Mat seg = cv::imread(dir + "/" + name, CV_LOAD_IMAGE_UNCHANGED); if (seg.empty()) { PyObject *py_image = cvt->toNDArray(image.clone()); assert(py_image != NULL); PyObject *py_mask_image = PyObject_CallMethod(this->net, const_cast<char *>(this->get_dyn_seg.c_str()), "(O)", py_image); seg = cvt->toMat(py_mask_image).clone(); seg.cv::Mat::convertTo(seg, CV_8U); // 0 background y 1 foreground if (dir.compare("no_save") != 0) { DIR *_dir = opendir(dir.c_str()); if (_dir) { closedir(_dir); } else if (ENOENT == errno) { const int check = mkdir(dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); if (check == -1) { std::string str = dir; str.replace(str.end() - 6, str.end(), ""); mkdir(str.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); } } cv::imwrite(dir + "/" + name, seg); } } return seg; }下面是代码的逐行解释:
总体而言,GetSegmentation() 函数的作用是读取指定的图像文件,然后使用存储在 this->net 对象中的算法对图像进行分割,并返回分割的结果。如果指定的目录不存在,则会尝试创建目录,并将分割结果保存为图像文件。
5、这段代码定义了 SegmentDynObject 类的一个成员函数 ImportSettings(),用于从一个 YAML 文件中导入一些设置参数。
void SegmentDynObject::ImportSettings() { std::string strSettingsFile = "./Examples/RGB-D/MaskSettings.yaml"; cv::FileStorage fs(strSettingsFile.c_str(), cv::FileStorage::READ); fs["py_path"] >> this->py_path; fs["module_name"] >> this->module_name; fs["class_name"] >> this->class_name; fs["get_dyn_seg"] >> this->get_dyn_seg; // std::cout << " py_path: "<< this->py_path << std::endl; // std::cout << " module_name: "<< this->module_name << std::endl; // std::cout << " class_name: "<< this->class_name << std::endl; // std::cout << " get_dyn_seg: "<< this->get_dyn_seg << std::endl; }下面是代码的逐行解释:
总体而言,ImportSettings() 函数的作用是打开指定的 YAML 文件,读取其中的设置参数,并将它们分别赋值给 SegmentDynObject 类的成员变量 py_path、module_name、class_name 和 get_dyn_seg。
免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删