- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
使用 OpenCV 和 C++ 计算混淆矩阵的首选方法是什么?
从...开始:
int TP = 0,FP = 0,FN = 0,TN = 0;
cv::Mat truth(60,60, CV_8UC1);
cv::Mat detections(60,60, CV_8UC1);
this->loadResults(truth, detections); // loadResults(cv::Mat& t, cv::Mat& d);
for(int r = 0; r < detections.rows; ++r)
for(int c = 0; c < detections.cols; ++c)
{
int d,t;
d = detection.at<unsigned char>(r,c);
t = truth.at<unsigned char>(r,c);
if(d&&t) ++TP;
if(d&&!t) ++FP;
if(!d&&t) ++FN;
if(!d&&!t) ++TN;
}
{
cv::Mat truePos = detection.mul(truth);
TP = cv::countNonZero(truePos)
}
{
cv::Mat falsePos = detection.mul(~truth);
FP = cv::countNonZero(falsePos )
}
{
cv::Mat falseNeg = truth.mul(~detection);
FN = cv::countNonZero(falseNeg )
}
{
cv::Mat trueNeg = (~truth).mul(~detection);
TN = cv::countNonZero(trueNeg )
}
auto lambda = [&, truth,TP,FP,FN,TN](unsigned char d, const int pos[]){
cv::Point2i pt(pos[1], pos[0]);
char t = truth.at<unsigned char>(pt);
if(d&&t) ++TP;
if(d&&!t) ++FP;
if(!d&&t) ++FN;
if(!d&&!t) ++TN;
};
detection.forEach(lambda);
最佳答案
简而言之,这三个都不是。
在开始之前,让我们定义一个简单的结构来保存我们的结果:
struct result_t
{
int TP;
int FP;
int FN;
int TN;
};
cv::Mat1b
明确表示我们只需要
CV_8UC1
类型的垫子:
result_t conf_mat_x(cv::Mat1b truth, cv::Mat1b detections);
result_t conf_mat_1a(cv::Mat1b truth, cv::Mat1b detections)
{
CV_Assert(truth.size == detections.size);
result_t result = { 0 };
for (int r(0); r < detections.rows; ++r) {
for (int c(0); c < detections.cols; ++c) {
int d(detections.at<uchar>(r, c));
int t(truth.at<uchar>(r, c));
if (d&&t) { ++result.TP; }
if (d&&!t) { ++result.FP; }
if (!d&&t) { ++result.FN; }
if (!d&&!t) { ++result.TN; }
}
}
return result;
}
#0: min=120.017 mean=123.258 TP=4192029 FP=4195489 TN=4195118 FN=4194580 Total=16777216
cv::Mat::at
也可以增加一些开销。
result_t conf_mat_2a(cv::Mat1b truth, cv::Mat1b detections)
{
CV_Assert(truth.size == detections.size);
result_t result = { 0 };
{
cv::Mat truePos = detections.mul(truth);
result.TP = cv::countNonZero(truePos);
}
{
cv::Mat falsePos = detections.mul(~truth);
result.FP = cv::countNonZero(falsePos);
}
{
cv::Mat falseNeg = truth.mul(~detections);
result.FN = cv::countNonZero(falseNeg);
}
{
cv::Mat trueNeg = (~truth).mul(~detections);
result.TN = cv::countNonZero(trueNeg);
}
return result;
}
#1: min=63.993 mean=68.674 TP=4192029 FP=4195489 TN=4195118 FN=4194580 Total=16777216
bitwise_and
也可以完成这项工作,并且可能会节省一点时间。
truePos
分配一个新矩阵,
falsePos
,
falseNeg
和
trueNeg
,我们可以重用相同的
cv::Mat
对于所有 4 种情况。由于形状和数据类型将始终相同,这意味着只会发生 1 次分配而不是 4 次。
result_t conf_mat_2b(cv::Mat1b truth, cv::Mat1b detections)
{
CV_Assert(truth.size == detections.size);
result_t result = { 0 };
cv::Mat temp;
cv::bitwise_and(detections, truth, temp);
result.TP = cv::countNonZero(temp);
cv::bitwise_and(detections, ~truth, temp);
result.FP = cv::countNonZero(temp);
cv::bitwise_and(~detections, truth, temp);
result.FN = cv::countNonZero(temp);
cv::bitwise_and(~detections, ~truth, temp);
result.TN = cv::countNonZero(temp);
return result;
}
#2: min=50.995 mean=52.440 TP=4192029 FP=4195489 TN=4195118 FN=4194580 Total=16777216
conf_mat_2a
相比,所需时间减少了约 20% .
~truth
和
~detections
两次。因此,我们也可以通过重用它们来消除这些操作以及 2 个额外的分配。
result_t conf_mat_2c(cv::Mat1b truth, cv::Mat1b detections)
{
CV_Assert(truth.size == detections.size);
result_t result = { 0 };
cv::Mat inv_truth(~truth);
cv::Mat inv_detections(~detections);
cv::Mat temp;
cv::bitwise_and(detections, truth, temp);
result.TP = cv::countNonZero(temp);
cv::bitwise_and(detections, inv_truth, temp);
result.FP = cv::countNonZero(temp);
cv::bitwise_and(inv_detections, truth, temp);
result.FN = cv::countNonZero(temp);
cv::bitwise_and(inv_detections, inv_truth, temp);
result.TN = cv::countNonZero(temp);
return result;
}
#3: min=37.997 mean=38.569 TP=4192029 FP=4195489 TN=4195118 FN=4194580 Total=16777216
conf_mat_2a
相比,所需时间减少了约 40% .
element_count == rows * cols
哪里rows
和 cols
表示cv::Mat
的高度和宽度(我们可以使用 cv::Mat::total()
)。 TP + FP + FN + TN == element_count
因为每个元素恰好属于 4 个集合中的 1 个。 positive_count
是 detections
中非零元素的数量. negative_count
是 detections
中零元素的数量. positive_count + negative_count == element_count
因为每个元素恰好属于 2 个集合中的 1 个 TP + FP == positive_count
TN + FN == negative_count
TN
使用简单的算术,从而消除一个
bitwise_and
和一个
countNonZero
.我们可以类似地计算
FP
,消除另一个
bitwise_and
,并使用第二个
countNonZero
计算
positive_count
反而。
inv_truth
的两种用途,我们也可以删除它。
result_t conf_mat_2d(cv::Mat1b truth, cv::Mat1b detections)
{
CV_Assert(truth.size == detections.size);
result_t result = { 0 };
cv::Mat1b inv_detections(~detections);
int positive_count(cv::countNonZero(detections));
int negative_count(static_cast<int>(truth.total()) - positive_count);
cv::Mat1b temp;
cv::bitwise_and(truth, detections, temp);
result.TP = cv::countNonZero(temp);
result.FP = positive_count - result.TP;
cv::bitwise_and(truth, inv_detections, temp);
result.FN = cv::countNonZero(temp);
result.TN = negative_count - result.FN;
return result;
}
#4: min=22.494 mean=22.831 TP=4192029 FP=4195489 TN=4195118 FN=4194580 Total=16777216
conf_mat_2a
相比,所需时间减少了约 65% .
inv_detections
一次,我们可以重用
temp
存储它,摆脱更多的分配,并进一步减少内存占用。
result_t conf_mat_2e(cv::Mat1b truth, cv::Mat1b detections)
{
CV_Assert(truth.size == detections.size);
result_t result = { 0 };
int positive_count(cv::countNonZero(detections));
int negative_count(static_cast<int>(truth.total()) - positive_count);
cv::Mat1b temp;
cv::bitwise_and(truth, detections, temp);
result.TP = cv::countNonZero(temp);
result.FP = positive_count - result.TP;
cv::bitwise_not(detections, temp);
cv::bitwise_and(truth, temp, temp);
result.FN = cv::countNonZero(temp);
result.TN = negative_count - result.FN;
return result;
}
#5: min=16.999 mean=17.391 TP=4192029 FP=4195489 TN=4195118 FN=4194580 Total=16777216
conf_mat_2a
相比,所需时间减少了约 72% .
forEach
在输入的多个切片上并行运行该函数,并且缺乏任何同步。当前实现返回不正确的结果。
conf_mat_2e
利用
cv::parallel_for_
.在工作线程之间分配负载的最简单方法是逐行进行。
cv::Mat3i
来避免同步的需要。将持有
TP
,
FP
, 和
FN
对于每一行(回想一下
TN
可以从最后的其他 3 行计算出来)。由于每一行只由一个工作线程处理,我们不需要同步。处理完所有行后,一个简单的
cv::sum
会给我们一共
TP
,
FP
, 和
FN
.
TN
然后计算。
3 * rows
整数来存储中间结果。
class ParallelConfMat : public cv::ParallelLoopBody
{
public:
enum
{
TP = 0
, FP = 1
, FN = 2
};
ParallelConfMat(cv::Mat1b& truth, cv::Mat1b& detections, cv::Mat3i& result)
: truth_(truth)
, detections_(detections)
, result_(result)
{
}
ParallelConfMat& operator=(ParallelConfMat const&)
{
return *this;
};
virtual void operator()(cv::Range const& range) const
{
cv::Mat1b temp;
for (int r(range.start); r < range.end; r++) {
cv::Mat1b detections(detections_.row(r));
cv::Mat1b truth(truth_.row(r));
cv::Vec3i& result(result_.at<cv::Vec3i>(r));
int positive_count(cv::countNonZero(detections));
int negative_count(static_cast<int>(truth.total()) - positive_count);
cv::bitwise_and(truth, detections, temp);
result[TP] = cv::countNonZero(temp);
result[FP] = positive_count - result[TP];
cv::bitwise_not(detections, temp);
cv::bitwise_and(truth, temp, temp);
result[FN] = cv::countNonZero(temp);
}
}
private:
cv::Mat1b& truth_;
cv::Mat1b& detections_;
cv::Mat3i& result_; // TP, FP, FN per row
};
result_t conf_mat_4(cv::Mat1b truth, cv::Mat1b detections)
{
CV_Assert(truth.size == detections.size);
result_t result = { 0 };
cv::Mat3i partial_results(truth.rows, 1);
cv::parallel_for_(cv::Range(0, truth.rows)
, ParallelConfMat(truth, detections, partial_results));
cv::Scalar reduced_results = cv::sum(partial_results);
result.TP = static_cast<int>(reduced_results[ParallelConfMat::TP]);
result.FP = static_cast<int>(reduced_results[ParallelConfMat::FP]);
result.FN = static_cast<int>(reduced_results[ParallelConfMat::FN]);
result.TN = static_cast<int>(truth.total()) - result.TP - result.FP - result.FN;
return result;
}
#6: min=1.496 mean=1.966 TP=4192029 FP=4195489 TN=4195118 FN=4194580 Total=16777216
conf_mat_2a
相比,运行时间减少了约 97.5% .
#include <opencv2/opencv.hpp>
#include <chrono>
#include <iomanip>
using std::chrono::high_resolution_clock;
using std::chrono::duration_cast;
using std::chrono::microseconds;
struct result_t
{
int TP;
int FP;
int FN;
int TN;
};
/******** PASTE all the conf_mat_xx functions here *********/
int main()
{
int ROWS(4 * 1024), COLS(4 * 1024), ITERS(32);
cv::Mat1b truth(ROWS, COLS);
cv::randu(truth, 0, 2);
truth *= 255;
cv::Mat1b detections(ROWS, COLS);
cv::randu(detections, 0, 2);
detections *= 255;
typedef result_t(*conf_mat_fn)(cv::Mat1b, cv::Mat1b);
struct test_info
{
conf_mat_fn fn;
std::vector<double> d;
result_t r;
};
std::vector<test_info> info;
info.push_back({ conf_mat_1a });
info.push_back({ conf_mat_2a });
info.push_back({ conf_mat_2b });
info.push_back({ conf_mat_2c });
info.push_back({ conf_mat_2d });
info.push_back({ conf_mat_2e });
info.push_back({ conf_mat_4 });
// Warm-up
for (int n(0); n < info.size(); ++n) {
info[n].fn(truth, detections);
}
for (int i(0); i < ITERS; ++i) {
for (int n(0); n < info.size(); ++n) {
high_resolution_clock::time_point t1 = high_resolution_clock::now();
info[n].r = info[n].fn(truth, detections);
high_resolution_clock::time_point t2 = high_resolution_clock::now();
info[n].d.push_back(static_cast<double>(duration_cast<microseconds>(t2 - t1).count()) / 1000.0);
}
}
for (int n(0); n < info.size(); ++n) {
std::cout << "#" << n << ":"
<< std::fixed << std::setprecision(3)
<< "\tmin=" << *std::min_element(info[n].d.begin(), info[n].d.end())
<< "\tmean=" << cv::mean(info[n].d)[0]
<< "\tTP=" << info[n].r.TP
<< "\tFP=" << info[n].r.FP
<< "\tTN=" << info[n].r.TN
<< "\tFN=" << info[n].r.FN
<< "\tTotal=" << (info[n].r.TP + info[n].r.FP + info[n].r.TN + info[n].r.FN)
<< "\n";
}
}
#0: min=119.797 mean=121.769 TP=4192029 FP=4195489 TN=4195118 FN=4194580 Total=16777216
#1: min=64.130 mean=65.086 TP=4192029 FP=4195489 TN=4195118 FN=4194580 Total=16777216
#2: min=51.152 mean=51.758 TP=4192029 FP=4195489 TN=4195118 FN=4194580 Total=16777216
#3: min=37.781 mean=38.357 TP=4192029 FP=4195489 TN=4195118 FN=4194580 Total=16777216
#4: min=22.329 mean=22.637 TP=4192029 FP=4195489 TN=4195118 FN=4194580 Total=16777216
#5: min=17.029 mean=17.297 TP=4192029 FP=4195489 TN=4195118 FN=4194580 Total=16777216
#6: min=1.827 mean=2.017 TP=4192029 FP=4195489 TN=4195118 FN=4194580 Total=16777216
关于c++ - OpenCV C++。快速计算混淆矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53102166/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!