Skip to content

Instantly share code, notes, and snippets.

@tmattio
Created January 25, 2017 03:43
Show Gist options
  • Save tmattio/74528673d271e3131c2ea2a2adaf5c8c to your computer and use it in GitHub Desktop.
Save tmattio/74528673d271e3131c2ea2a2adaf5c8c to your computer and use it in GitHub Desktop.
A google test that ensure that an image is not corrupted when passing it through a tbb flow graph
#include <gtest/gtest.h>
#include <tbb/flow_graph.h>
#include <opencv2/core.hpp>
using namespace tbb::flow;
/**
* Simple functor that return the inverted matrix.
*/
struct pass_image {
cv::Mat operator()(cv::Mat image) {
cv::bitwise_not(image, image);
return image;
}
};
/**
* Generate a random images of size 2000x2000.
* This will stop when it has generated the number of images passed in the
* ctor.
*/
class generate_image {
public:
explicit generate_image(int max_count) : max_count_(max_count) {}
bool operator()(cv::Mat &image) {
if (count_ < max_count_) {
cv::Mat1b mat(2000, 2000);
cv::randu(mat, cv::Scalar(0), cv::Scalar(255));
image = mat;
++count_;
return true;
}
return false;
}
private:
int count_ = 0;
int max_count_;
};
/**
* Test that we can pass a cv::Mat in a tbb graph without any corruption.
* This will create a graph that generate an image, pass it through some non
* corrupting processing functions and compare that the final result is the
* same as the input.
*/
TEST(tbb, non_currupted) {
for (int i = 0; i < 10; ++i) {
tbb::flow::graph g;
source_node<cv::Mat> source_node(g, generate_image(50));
function_node<cv::Mat, cv::Mat> pass_image_node_1(g, unlimited,
pass_image());
function_node<cv::Mat, cv::Mat> pass_image_node_2(g, unlimited,
pass_image());
function_node<cv::Mat, cv::Mat> pass_image_node_3(g, unlimited,
pass_image());
function_node<cv::Mat, cv::Mat> pass_image_node_4(g, unlimited,
pass_image());
function_node<cv::Mat, cv::Mat> pass_image_node_5(g, unlimited,
pass_image());
join_node<tuple<cv::Mat, cv::Mat>> join_node(g);
function_node<tuple<cv::Mat, cv::Mat>> compare_node(
g, unlimited, [](const tuple<cv::Mat, cv::Mat> &t) {
cv::Mat diff;
cv::compare(std::get<0>(t), std::get<1>(t), diff, cv::CMP_NE);
auto difference = cv::countNonZero(diff);
ASSERT_EQ(difference, 0);
});
make_edge(source_node, pass_image_node_1);
make_edge(pass_image_node_1, pass_image_node_2);
make_edge(pass_image_node_2, pass_image_node_3);
make_edge(pass_image_node_3, pass_image_node_4);
make_edge(pass_image_node_4, input_port<0>(join_node));
make_edge(source_node, input_port<1>(join_node));
make_edge(join_node, compare_node);
g.wait_for_all();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment