- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下面是我的源文件中的一个函数
cv::Mat* Retina::Preprocessing::create_mask(const cv::Mat *img, const uint8_t threshold) {
LOG_IF(ERROR, img->empty()) << "The input image is empty. Terminating Now!!";
cv::Mat green_channel(img->rows(), img->cols(), CV_8UC1); /*!< Green channel. */
/// Check number of channels in img and based on that find out the green channel.
switch (img->channels()){
/// For 3 channels, it is an RGB image and we use cv::mixChannels().
case(3): {
int from_to[] = {1,0};
cv::mixChannels(img, 1, &green_channel, 1, from_to, 1);
break;
}
/// For a single channel, we assume that it is the green channel.
case(1): {
green_channel = *img;
break;
}
/// Otherwise we are out of clue and throw an error.
default: {
LOG(ERROR)<<"Number of image channels found = "<< img->channels()<<". Terminating Now!!";
return nullptr; /*!< (unreachable code) Only given for completion */
}
}
cv::Mat mask(img->rows(), img->cols(), CV_8UC1);/*!< Empty mask image */
if (threshold == 0)/// Otsu's threshold is used
cv::threshold(green, mask, threshold, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
else /// We can use the provided threshold value
cv::threshold(green, mask, threshold, 255, CV_THRESH_BINARY);
LOG_IF(ERROR, mask.empty())<< "After thresholding, image became empty. Terminating Now!!";
std::vector<std::vector<cv::Point>> contours;
/// Get the contours in the binary mask.
cv::findContours(mask, *contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
size_t max_index{};
double prev_area{};
size_t index{};
/// Lambda function for a convenient one-liner std::for_each. SEE BELOW.
lambda_max_index = [max_index, prev_area, index] (std::vector<cv::Point> c){
double new_area { cv::contourArea(c) };
max_index = (new_area > prev_area) ? max_index = index : max_index;
++index;
};
/// For each contour compute its area, and over the loop find out the largest contour.
std::for_each(contours.begin(), contours.end(), lambda_max_index());
/// Iterate over each point and test if it lies inside the contour, and drive in it.
for(size_t row_pt = 0; row_pt < mask_out.rows(); ++row_pt){
for(size_t col_pt = 0; col_pt < mask_out.cols(); ++col_pt){
if (cv::pointPolygonTest(contours[max_index], cv::Point(row_pt, col_pt))>=0)
mask[row_pt, col_pt] = 255;
else
mask[row_pt, col_pt] = 0;
}
}
return &mask;
}
下面是我写的一个google单元测试文件。这是我第一次尝试使用 GoogleTest。
#include"PreProcessing.hxx"
#include<opencv2/highgui/highgui.hpp>
#include<gtest/gtest.h>
TEST(Fundus_Mask_Creation, NO_THRESHOLD) {
cv::Mat img(cv::imread(std::string("../data/test_img.jpg")));
cv::Mat* mask = Retina::Preprocessing::create_mask(&img);
ASSERT_TRUE(!(mask->empty()));
std::string winname("Input Image");
cv::namedWindow(winname);
cv::imshow(winname, img);
cv::waitKey(800);
cv::destroyWindow(winname);
winname = "Fundus Mask";
cv::namedWindow(winname);
cv::imshow(winname, *mask);
cv::waitKey(800);
}
TEST(Fundus_Mask_Creation, THRESHOLD) {
cv::Mat img(cv::imread(std::string("../data/test_img.jpg")));
std::uint8_t threshold{10};
cv::Mat* mask = Retina::Preprocessing::create_mask(&img, threshold);
ASSERT_TRUE(!(mask->empty()));
std::string winname("Input Image");
cv::namedWindow(winname);
cv::imshow(winname, img);
cv::waitKey(800);
cv::destroyWindow(winname);
winname = "Fundus Mask";
cv::namedWindow(winname);
cv::imshow(winname, *mask);
cv::waitKey(800);
}
我还想测试一下 glog 记录 "Number of image channels found = "<< img->channels()<<". Terminating Now!!!";
的情况
当我向它提供导致正确记录上述消息的输入时,我如何编写一个单元测试,它会成功运行(即原始源文件会记录一个 fatal error )?
最佳答案
您需要模拟 (googlemock) 来实现这一点。要启用模拟,请为 glog 调用创建一个薄包装类,以及此类将从中继承的接口(interface)(您需要它来启用模拟)。您可以以此为指导:
class IGlogWrapper {
public:
...
virtual void LogIf(int logMessageType, bool condition, const std::string &message) = 0;
...
};
class GlogWrapper : public IGlogWrapper {
public:
...
void LogIf(int logMessageType, bool condition, const std::string &message) override {
LOG_IF(logMessageType, condition) << message;
}
...
};
现在创建一个模拟类:
class GlogWrapperMock : public IGlogWrapper {
public:
...
MOCK_METHOD3(LogIf, void(int, bool, const std::string &));
...
};
并使您的 Retina::Preprocessing
类持有指向 IGlogWrapper
的指针,并通过此接口(interface)进行日志记录调用。现在您可以使用依赖注入(inject)将指针传递给 Retina::Preprocessing
类,该指针指向将使用 glog 的真实记录器类 GlogWrapper
,而在测试中,您将指针传递给模拟对象(GlogWrapperMock
的实例)。通过这种方式,您可以在具有两个 channel 的测试中创建一个图像,并在此模拟对象上设置期望以在这种情况下调用函数 LogIf
。以下示例使用公共(public)方法注入(inject)要使用的记录器:
using namespace testing;
TEST(Fundus_Mask_Creation, FAILS_FOR_TWO_CHANNEL_IMAGES) {
// create image with two channels
cv::Mat img( ... );
GlogWrapperMock glogMock;
Retina::Preprocessing::setLogger(&glogMock);
std::string expectedMessage = "Number of image channels found = 2. Terminating Now!!";
EXPECT_CALL(glogMock, LogIf(ERROR, _, expectedMessage).WillOnce(Return());
cv::Mat* mask = Retina::Preprocessing::create_mask(&img);
// Make additional assertions if necessary
...
}
有关模拟的更多信息,请查看文档:https://github.com/google/googletest/blob/master/googlemock/docs/ForDummies.md#getting-started另外,请注意,您发布的两个单元测试没有多大意义,因为您正在从磁盘加载图像并在断言后进行其他调用。单元测试应该简洁、快速并且与环境隔离。我建议额外阅读有关单元测试的内容,互联网上有很多资源。希望这对您有所帮助!
关于c++ - 在 Google UnitTest 中检测 glog 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37751661/
我有两个库A和B。库B是我自己的库,我需要其中的“windows.h”。另外,对于某些功能,我需要使用第三方库 A。A 使用 google 日志记录库,这就是问题: 第一个错误是这样的: Severi
我正在尝试在我的项目中实现 GLOG 库,但我只获得控制台输出,并且我不允许创建具有相关严重性的文件日志,这是我的代码:我正在为 linux (ubuntu) 开发 #include int mai
我在我的项目中使用 glog 标志 log_dir。最近我导入了 kubernetes 库并开始出现这种运行时 panic panic :./aaa.test 标志重新定义:log_dir May 1
我正在为我的 C++ 程序使用 google glog 库。我以前使用过 glog 库,所以我知道它应该在 CHECK 失败时打印出堆栈跟踪。但它不会为我的程序打印任何堆栈跟踪: #include
我是编程新手。我想在我的 Windows 10 系统上安装 Glog。我已按照在同名 Stack Overflow 线程中找到的说明进行操作。 我使用 cmake 和 Visual Studio 20
我正在尝试使用 glog 日志记录库 ( LINK )。在 VS2012 中(我使用的是 Windows 8 Professional 64 位(日语)),我使用单个 main.cpp 文件创建了一个
我在使用时使用 glog 进行日志记录: LOG(INFO) << "something"; 它按预期工作,但是当我像下面这样使用多个日志时,它不会记录,直到程序停止。当程序停止时,它会按预期记录所有
我正在使用谷歌 glog用于记录。我在路径为 dir/file.cc 的目录中有一个文件。 我只想在此文件中启用详细日志记录级别 5。所以我将 GLOG_vmodule="file=5" 导出到环境中
我正在使用 glog 库,但我无法将多条消息打印到文件。 当我使用这段代码时: std::string appPath = TUtil::ExePath() + "logs\\"; google::S
我正在使用 github.com/golang/glog ,并希望在运行时更改标志 stderrthreshold 或 V-leveled logging,即无需重新编译二进制文件并重新运行它。我搜索
有谁知道如何在 golang 源代码中设置/修改日志目录?我想在源代码中设置日志目录,而不是在命令行中设置 -log_dir= 最佳答案 这是我见过的一个技巧:在代码中设置标志。也非常适合从代码设置日
我在我的代码中使用 boost 程序选项 并尝试使用 glog(谷歌日志库)添加详细日志记录。 问题是 boost 捕获命令行选项,我不能使用 --v 标志来控制详细日志记录。是否有从代码中设置 mi
我有一个 C++ 库,它使用 glog CHECK 语句来设置运行时的各种条件。如果 CHECK 失败,glog 将终止程序。我的 python 程序需要这个 C++ 库中的一些函数,所以我为这些函数
我现在正在使用 google glog。我在调试程序的时候,每次进程重启,都会生成一个新的日志文件,由新的pid标识为文件名的后缀,像这样 ied_config.20131220-142934.452
如何更改 Google glog 中的输出目录? 我只找到了 google::SetLogDestination(google::LogSeverity, const char* path) 尝试过:
我开始一个 react-native 的新项目或者当我安装模块时在编译时出现错误,请帮助我如何解决这个问题。 最佳答案 希望这对您有帮助请按照以下步骤操作:- 1)请在node-modules文件夹中
问题很简单,我有这样一条日志: glog.v(5).Infof("xxx %v", getLogContent()) 但是我的getLogContent()是一个耗时的方法,所以我想问一下:getLo
我正在使用一个名为 caffe 的卷积神经网络框架,它在控制台中的输出由 Google-glog 提供。 .但是,当我尝试使用以下命令将输出保存到文件时: sh train_imagenet.sh |
目前,如果我输入 :Glog,我会得到当前文件的修订列表。 如果我输入 :Glog --,我会得到所有文件的提交列表。 我需要输入什么来获取当前文件的提交? 最佳答案 为了完整起见,将修订加载到缓冲区
我正在尝试使用 CMAKE 在 Ubuntu 20.04 中安装 openpose,但出现以下错误: Could NOT find Glog (missing: GLOG_INCLUDE_DIR GL
我是一名优秀的程序员,十分优秀!