feat: 切换后端至PaddleOCR-NCNN,切换工程为CMake

1.项目后端整体迁移至PaddleOCR-NCNN算法,已通过基本的兼容性测试
2.工程改为使用CMake组织,后续为了更好地兼容第三方库,不再提供QMake工程
3.重整权利声明文件,重整代码工程,确保最小化侵权风险

Log: 切换后端至PaddleOCR-NCNN,切换工程为CMake
Change-Id: I4d5d2c5d37505a4a24b389b1a4c5d12f17bfa38c
This commit is contained in:
wangzhengyang
2022-05-10 09:54:44 +08:00
parent ecdd171c6f
commit 718c41634f
10018 changed files with 3593797 additions and 186748 deletions

View File

@ -0,0 +1,197 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2019 Intel Corporation
#include "../test_precomp.hpp"
#include <unordered_set>
#include <thread>
#include "executor/conc_queue.hpp"
namespace opencv_test
{
using namespace cv::gapi;
TEST(ConcQueue, PushPop)
{
own::concurrent_bounded_queue<int> q;
for (int i = 0; i < 100; i++)
{
q.push(i);
}
for (int i = 0; i < 100; i++)
{
int x;
q.pop(x);
EXPECT_EQ(x, i);
}
}
TEST(ConcQueue, TryPop)
{
own::concurrent_bounded_queue<int> q;
int x = 0;
EXPECT_FALSE(q.try_pop(x));
q.push(1);
EXPECT_TRUE(q.try_pop(x));
EXPECT_EQ(1, x);
}
TEST(ConcQueue, Clear)
{
own::concurrent_bounded_queue<int> q;
for (int i = 0; i < 10; i++)
{
q.push(i);
}
q.clear();
int x = 0;
EXPECT_FALSE(q.try_pop(x));
}
// In this test, every writer thread produces its own range of integer
// numbers, writing those to a shared queue.
//
// Every reader thread pops elements from the queue (until -1 is
// reached) and stores those in its own associated set.
//
// Finally, the master thread waits for completion of all other
// threads and verifies that all the necessary data is
// produced/obtained.
namespace
{
using StressParam = std::tuple<int // Num writer threads
,int // Num elements per writer
,int // Num reader threads
,std::size_t>; // Queue capacity
constexpr int STOP_SIGN = -1;
constexpr int BASE = 1000;
}
struct ConcQueue_: public ::testing::TestWithParam<StressParam>
{
using Q = own::concurrent_bounded_queue<int>;
using S = std::unordered_set<int>;
static void writer(int base, int writes, Q& q)
{
for (int i = 0; i < writes; i++)
{
q.push(base + i);
}
q.push(STOP_SIGN);
}
static void reader(Q& q, S& s)
{
int x = 0;
while (true)
{
q.pop(x);
if (x == STOP_SIGN) return;
s.insert(x);
}
}
};
TEST_P(ConcQueue_, Test)
{
int num_writers = 0;
int num_writes = 0;
int num_readers = 0;
std::size_t capacity = 0u;
std::tie(num_writers, num_writes, num_readers, capacity) = GetParam();
CV_Assert(num_writers < 20);
CV_Assert(num_writes < BASE);
Q q;
if (capacity)
{
// see below (2)
CV_Assert(static_cast<int>(capacity) > (num_writers - num_readers));
q.set_capacity(capacity);
}
// Start reader threads
std::vector<S> storage(num_readers);
std::vector<std::thread> readers;
for (S& s : storage)
{
readers.emplace_back(reader, std::ref(q), std::ref(s));
}
// Start writer threads, also pre-generate reference numbers
S reference;
std::vector<std::thread> writers;
for (int w = 0; w < num_writers; w++)
{
writers.emplace_back(writer, w*BASE, num_writes, std::ref(q));
for (int r = 0; r < num_writes; r++)
{
reference.insert(w*BASE + r);
}
}
// Every writer puts a STOP_SIGN at the end,
// There are three cases:
// 1) num_writers == num_readers
// every reader should get its own STOP_SIGN from any
// of the writers
//
// 2) num_writers > num_readers
// every reader will get a STOP_SIGN but there're more
// STOP_SIGNs may be pushed to the queue - and if this
// number exceeds capacity, writers block (to a deadlock).
// The latter situation must be avoided at parameters level.
// [a] Also not every data produced by writers will be consumed
// by a reader in this case. Master thread will read the rest
//
// 3) num_readers > num_writers
// in this case, some readers will stuck and will never get
// a STOP_SIGN. Master thread will push extra STOP_SIGNs to the
// queue.
// Solution to (2a)
S remnants;
if (num_writers > num_readers)
{
int extra = num_writers - num_readers;
while (extra)
{
int x = 0;
q.pop(x);
if (x == STOP_SIGN) extra--;
else remnants.insert(x);
}
}
// Solution to (3)
if (num_readers > num_writers)
{
int extra = num_readers - num_writers;
while (extra--) q.push(STOP_SIGN);
}
// Wait for completions
for (auto &t : readers) t.join();
for (auto &t : writers) t.join();
// Accumulate and validate the result
S result(remnants.begin(), remnants.end());
for (const auto &s : storage) result.insert(s.begin(), s.end());
EXPECT_EQ(reference, result);
}
INSTANTIATE_TEST_CASE_P(ConcQueueStress, ConcQueue_,
Combine( Values(1, 2, 4, 8, 16) // writers
, Values(1, 32, 96, 256) // writes
, Values(1, 2, 10) // readers
, Values(0u, 16u, 32u))); // capacity
} // namespace opencv_test

View File

@ -0,0 +1,175 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2018 Intel Corporation
#include "../test_precomp.hpp"
#include <opencv2/gapi/own/types.hpp>
namespace opencv_test
{
TEST(Point, CreateEmpty)
{
cv::gapi::own::Point p;
EXPECT_EQ(0, p.x);
EXPECT_EQ(0, p.y);
}
TEST(Point, CreateWithParams)
{
cv::gapi::own::Point p = {1, 2};
EXPECT_EQ(1, p.x);
EXPECT_EQ(2, p.y);
}
TEST(Point2f, CreateEmpty)
{
cv::gapi::own::Point2f p;
EXPECT_EQ(0.f, p.x);
EXPECT_EQ(0.f, p.y);
}
TEST(Point2f, CreateWithParams)
{
cv::gapi::own::Point2f p = {3.14f, 2.71f};
EXPECT_EQ(3.14f, p.x);
EXPECT_EQ(2.71f, p.y);
}
TEST(Rect, CreateEmpty)
{
cv::gapi::own::Rect r;
EXPECT_EQ(0, r.x);
EXPECT_EQ(0, r.y);
EXPECT_EQ(0, r.width);
EXPECT_EQ(0, r.height);
}
TEST(Rect, CreateWithParams)
{
cv::gapi::own::Rect r(1, 2, 3, 4);
EXPECT_EQ(1, r.x);
EXPECT_EQ(2, r.y);
EXPECT_EQ(3, r.width);
EXPECT_EQ(4, r.height);
}
TEST(Rect, CompareEqual)
{
cv::gapi::own::Rect r1(1, 2, 3, 4);
cv::gapi::own::Rect r2(1, 2, 3, 4);
EXPECT_TRUE(r1 == r2);
}
TEST(Rect, CompareDefaultEqual)
{
cv::gapi::own::Rect r1;
cv::gapi::own::Rect r2;
EXPECT_TRUE(r1 == r2);
}
TEST(Rect, CompareNotEqual)
{
cv::gapi::own::Rect r1(1, 2, 3, 4);
cv::gapi::own::Rect r2;
EXPECT_TRUE(r1 != r2);
}
TEST(Rect, Intersection)
{
cv::gapi::own::Rect r1(2, 2, 3, 3);
cv::gapi::own::Rect r2(3, 1, 3, 3);
cv::gapi::own::Rect intersect = r1 & r2;
EXPECT_EQ(3, intersect.x);
EXPECT_EQ(2, intersect.y);
EXPECT_EQ(2, intersect.width);
EXPECT_EQ(2, intersect.height);
}
TEST(Rect, AssignIntersection)
{
cv::gapi::own::Rect r1(2, 2, 3, 3);
cv::gapi::own::Rect r2(3, 1, 3, 3);
r1 &= r2;
EXPECT_EQ(3, r1.x);
EXPECT_EQ(2, r1.y);
EXPECT_EQ(2, r1.width);
EXPECT_EQ(2, r1.height);
}
TEST(Size, CreateEmpty)
{
cv::gapi::own::Size s;
EXPECT_EQ(0, s.width);
EXPECT_EQ(0, s.height);
}
TEST(Size, CreateWithParams)
{
cv::gapi::own::Size s(640, 480);
EXPECT_EQ(640, s.width);
EXPECT_EQ(480, s.height);
}
TEST(Size, AdditionAssignment)
{
cv::gapi::own::Size s1(1, 2);
cv::gapi::own::Size s2(2, 3);
s1 += s2;
EXPECT_EQ(3, s1.width);
EXPECT_EQ(5, s1.height);
}
TEST(Size, CompareEqual)
{
cv::gapi::own::Size s1(1, 2);
cv::gapi::own::Size s2(1, 2);
EXPECT_TRUE(s1 == s2);
EXPECT_FALSE(s1 != s2);
}
TEST(Size, CompareDefaultEqual)
{
cv::gapi::own::Size s1;
cv::gapi::own::Size s2;
EXPECT_TRUE(s1 == s2);
EXPECT_FALSE(s1 != s2);
}
TEST(Size, CompareNotEqual)
{
cv::gapi::own::Size s1(1, 2);
cv::gapi::own::Size s2(3, 4);
EXPECT_FALSE(s1 == s2);
EXPECT_TRUE(s1 != s2);
}
} // opencv_test

View File

@ -0,0 +1,156 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2020 Intel Corporation
#include "../test_precomp.hpp"
#include <unordered_set>
#include <thread>
#include "executor/last_value.hpp"
namespace opencv_test {
using namespace cv::gapi;
TEST(LastValue, PushPop) {
own::last_written_value<int> v;
for (int i = 0; i < 100; i++) {
v.push(i);
int x = 1;
v.pop(x);
EXPECT_EQ(x, i);
}
}
TEST(LastValue, TryPop) {
own::last_written_value<int> v;
int x = 0;
EXPECT_FALSE(v.try_pop(x));
v.push(1);
EXPECT_TRUE(v.try_pop(x));
EXPECT_EQ(1, x);
}
TEST(LastValue, Clear) {
own::last_written_value<int> v;
v.push(42);
v.clear();
int x = 0;
EXPECT_FALSE(v.try_pop(x));
}
TEST(LastValue, Overwrite) {
own::last_written_value<int> v;
v.push(42);
v.push(0);
int x = -1;
EXPECT_TRUE(v.try_pop(x));
EXPECT_EQ(0, x);
}
// In this test, every writer thread produces its own range of integer
// numbers, writing those to a shared queue.
//
// Every reader thread pops elements from the queue (until -1 is
// reached) and stores those in its own associated set.
//
// Finally, the master thread waits for completion of all other
// threads and verifies that all the necessary data is
// produced/obtained.
namespace {
using StressParam = std::tuple<int // Num writer threads
,int // Num elements per writer
,int>; // Num reader threads
constexpr int STOP_SIGN = -1;
constexpr int BASE = 1000;
}
struct LastValue_: public ::testing::TestWithParam<StressParam> {
using V = own::last_written_value<int>;
using S = std::unordered_set<int>;
static void writer(int base, int writes, V& v) {
for (int i = 0; i < writes; i++) {
if (i % 2) {
std::this_thread::sleep_for(std::chrono::milliseconds{1});
}
v.push(base + i);
}
v.push(STOP_SIGN);
}
static void reader(V& v, S& s) {
int x = 0;
while (true) {
v.pop(x);
if (x == STOP_SIGN) {
// If this thread was lucky enough to read this STOP_SIGN,
// push it back to v to make other possible readers able
// to read it again (note due to the last_written_value
// semantic, those STOP_SIGN could be simply lost i.e.
// overwritten.
v.push(STOP_SIGN);
return;
}
s.insert(x);
}
}
};
TEST_P(LastValue_, Test)
{
int num_writers = 0;
int num_writes = 0;
int num_readers = 0;
std::tie(num_writers, num_writes, num_readers) = GetParam();
CV_Assert(num_writers < 20);
CV_Assert(num_writes < BASE);
V v;
// Start reader threads
std::vector<S> storage(num_readers);
std::vector<std::thread> readers;
for (S& s : storage) {
readers.emplace_back(reader, std::ref(v), std::ref(s));
}
// Start writer threads, also pre-generate reference numbers
S reference;
std::vector<std::thread> writers;
for (int w = 0; w < num_writers; w++) {
writers.emplace_back(writer, w*BASE, num_writes, std::ref(v));
for (int r = 0; r < num_writes; r++) {
reference.insert(w*BASE + r);
}
}
// Wait for completions
for (auto &t : readers) t.join();
for (auto &t : writers) t.join();
// Validate the result. Some values are read, and the values are
// correct (i.e. such values have been written)
std::size_t num_values_read = 0u;
for (const auto &s : storage) {
num_values_read += s.size();
for (auto &x : s) {
EXPECT_TRUE(reference.count(x) > 0);
}
}
// NOTE: Some combinations may end-up in 0 values read
// it is normal, the main thing is that the test shouldn't hang!
EXPECT_LE(0u, num_values_read);
}
INSTANTIATE_TEST_CASE_P(LastValueStress, LastValue_,
Combine( Values(1, 2, 4, 8, 16) // writers
, Values(32, 96, 256) // writes
, Values(1, 2, 10))); // readers
} // namespace opencv_test

View File

@ -0,0 +1,641 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2018 Intel Corporation
#include "../test_precomp.hpp"
#include <opencv2/gapi/own/mat.hpp>
#include <opencv2/gapi/own/convert.hpp>
#include <opencv2/gapi/util/compiler_hints.hpp> //suppress_unused_warning
namespace opencv_test
{
using Mat = cv::gapi::own::Mat;
using Dims = std::vector<int>;
namespace {
inline std::size_t multiply_dims(Dims const& dims){
return std::accumulate(dims.begin(), dims.end(), static_cast<size_t>(1), std::multiplies<std::size_t>());
}
}
TEST(OwnMat, DefaultConstruction)
{
Mat m;
ASSERT_EQ(m.data, nullptr);
ASSERT_EQ(m.cols, 0);
ASSERT_EQ(m.rows, 0);
ASSERT_EQ(m.cols, 0);
ASSERT_EQ(m.type(), 0);
ASSERT_EQ(m.depth(), 0);
ASSERT_TRUE(m.dims.empty());
ASSERT_TRUE(m.empty());
}
TEST(OwnMat, Create)
{
auto size = cv::gapi::own::Size{32,16};
Mat m;
m.create(size, CV_8UC1);
ASSERT_NE(m.data, nullptr);
ASSERT_EQ((cv::gapi::own::Size{m.cols, m.rows}), size);
ASSERT_EQ(m.total(), static_cast<size_t>(size.height) * size.width);
ASSERT_EQ(m.type(), CV_8UC1);
ASSERT_EQ(m.depth(), CV_8U);
ASSERT_EQ(m.channels(), 1);
ASSERT_EQ(m.elemSize(), sizeof(uint8_t));
ASSERT_EQ(m.step, sizeof(uint8_t) * m.cols);
ASSERT_TRUE(m.dims.empty());
ASSERT_FALSE(m.empty());
}
TEST(OwnMat, CreateND)
{
Dims dims = {1,1,32,32};
Mat m;
m.create(dims, CV_32F);
ASSERT_NE(nullptr , m.data );
ASSERT_EQ((cv::gapi::own::Size{0,0}), (cv::gapi::own::Size{m.cols, m.rows}));
ASSERT_EQ(multiply_dims(dims), m.total());
ASSERT_EQ(CV_32F , m.type() );
ASSERT_EQ(CV_32F , m.depth() );
ASSERT_EQ(-1 , m.channels());
ASSERT_EQ(sizeof(float) , m.elemSize());
ASSERT_EQ(0u , m.step );
ASSERT_EQ(dims , m.dims );
ASSERT_FALSE(m.empty());
}
TEST(OwnMat, CreateOverload)
{
auto size = cv::gapi::own::Size{32,16};
Mat m;
m.create(size.height,size.width, CV_8UC1);
ASSERT_NE(m.data, nullptr);
ASSERT_EQ((cv::Size{m.cols, m.rows}), size);
ASSERT_EQ(m.total(), static_cast<size_t>(size.height) * size.width);
ASSERT_EQ(m.type(), CV_8UC1);
ASSERT_EQ(m.depth(), CV_8U);
ASSERT_EQ(m.channels(), 1);
ASSERT_EQ(m.elemSize(), sizeof(uint8_t));
ASSERT_EQ(m.step, sizeof(uint8_t) * m.cols);
ASSERT_TRUE(m.dims.empty());
ASSERT_FALSE(m.empty());
}
TEST(OwnMat, Create3chan)
{
auto size = cv::Size{32,16};
Mat m;
m.create(size, CV_8UC3);
ASSERT_NE(m.data, nullptr);
ASSERT_EQ((cv::Size{m.cols, m.rows}), size);
ASSERT_EQ(m.type(), CV_8UC3);
ASSERT_EQ(m.depth(), CV_8U);
ASSERT_EQ(m.channels(), 3);
ASSERT_EQ(m.elemSize(), 3 * sizeof(uint8_t));
ASSERT_EQ(m.step, 3* sizeof(uint8_t) * m.cols);
ASSERT_TRUE(m.dims.empty());
ASSERT_FALSE(m.empty());
}
struct NonEmptyMat {
cv::gapi::own::Size size{32,16};
Mat m;
NonEmptyMat() {
m.create(size, CV_8UC1);
}
};
struct OwnMatSharedSemantics : NonEmptyMat, ::testing::Test {};
namespace {
auto state_of = [](Mat const& mat) {
return std::make_tuple(
mat.data,
cv::Size{mat.cols, mat.rows},
mat.type(),
mat.depth(),
mat.channels(),
mat.dims,
mat.empty()
);
};
void ensure_mats_are_same(Mat const& copy, Mat const& m){
EXPECT_NE(copy.data, nullptr);
EXPECT_EQ(state_of(copy), state_of(m));
}
}
TEST_F(OwnMatSharedSemantics, CopyConstruction)
{
Mat copy(m);
ensure_mats_are_same(copy, m);
}
TEST_F(OwnMatSharedSemantics, CopyAssignment)
{
Mat copy;
copy = m;
ensure_mats_are_same(copy, m);
}
struct OwnMatMoveSemantics : NonEmptyMat, ::testing::Test {
Mat& moved_from = m;
decltype(state_of(moved_from)) initial_state = state_of(moved_from);
void ensure_state_moved_to(Mat const& moved_to)
{
EXPECT_EQ(state_of(moved_to), initial_state);
EXPECT_EQ(state_of(moved_from), state_of(Mat{}));
}
};
TEST_F(OwnMatMoveSemantics, MoveConstruction)
{
Mat moved_to(std::move(moved_from));
ensure_state_moved_to(moved_to);
}
TEST_F(OwnMatMoveSemantics, MoveAssignment)
{
Mat moved_to(std::move(moved_from));
ensure_state_moved_to(moved_to);
}
struct OwnMatNonOwningView : NonEmptyMat, ::testing::Test {
decltype(state_of(m)) initial_state = state_of(m);
void TearDown() override {
EXPECT_EQ(state_of(m), initial_state)<<"State of the source matrix changed?";
//ASAN should complain here if memory is freed here (e.g. by bug in non owning logic of own::Mat)
volatile uchar dummy = m.data[0];
cv::util::suppress_unused_warning(dummy);
}
};
TEST_F(OwnMatNonOwningView, Construction)
{
Mat non_owning_view(m.rows, m.cols, m.type(), static_cast<void*>(m.data));
ensure_mats_are_same(non_owning_view, m);
}
TEST_F(OwnMatNonOwningView, CopyConstruction)
{
Mat non_owning_view{m.rows, m.cols, m.type(), static_cast<void*>(m.data)};
Mat non_owning_view_copy = non_owning_view;
ensure_mats_are_same(non_owning_view_copy, m);
}
TEST_F(OwnMatNonOwningView, Assignment)
{
Mat non_owning_view{m.rows, m.cols, m.type(), static_cast<void*>(m.data)};
Mat non_owning_view_copy;
non_owning_view_copy = non_owning_view;
ensure_mats_are_same(non_owning_view_copy, m);
}
TEST(OwnMatConversion, WithStep)
{
constexpr int width = 8;
constexpr int height = 8;
constexpr int stepInPixels = 16;
std::array<int, height * stepInPixels> data;
for (size_t i = 0; i < data.size(); i++)
{
data[i] = static_cast<int>(i);
}
cv::Mat cvMat(cv::Size{width, height}, CV_32S, data.data(), stepInPixels * sizeof(int));
auto ownMat = to_own(cvMat);
auto cvMatFromOwn = cv::gapi::own::to_ocv(ownMat);
EXPECT_EQ(0, cvtest::norm(cvMat, cvMatFromOwn, NORM_INF))
<< cvMat << std::endl
<< (cvMat != cvMatFromOwn);
}
TEST(OwnMatConversion, WithND)
{
const Dims dims = {1,3,8,8};
std::vector<uint8_t> data(dims[0]*dims[1]*dims[2]*dims[3]);
for (size_t i = 0u; i < data.size(); i++)
{
data[i] = static_cast<uint8_t>(i);
}
cv::Mat cvMat(dims, CV_8U, data.data());
auto ownMat = to_own(cvMat);
auto cvMatFromOwn = cv::gapi::own::to_ocv(ownMat);
EXPECT_EQ(0, cv::norm(cvMat, cvMatFromOwn, NORM_INF))
<< cvMat << std::endl
<< (cvMat != cvMatFromOwn);
}
TEST(OwnMat, PtrWithStep)
{
constexpr int width = 8;
constexpr int height = 8;
constexpr int stepInPixels = 16;
std::array<int, height * stepInPixels> data;
for (size_t i = 0; i < data.size(); i++)
{
data[i] = static_cast<int>(i);
}
Mat mat(height, width, CV_32S, data.data(), stepInPixels * sizeof(int));
EXPECT_EQ(& data[0], reinterpret_cast<int*>(mat.ptr(0)));
EXPECT_EQ(& data[1], reinterpret_cast<int*>(mat.ptr(0, 1)));
EXPECT_EQ(& data[stepInPixels], reinterpret_cast<int*>(mat.ptr(1)));
EXPECT_EQ(& data[stepInPixels +1], reinterpret_cast<int*>(mat.ptr(1,1)));
auto const& cmat = mat;
EXPECT_EQ(& data[0], reinterpret_cast<const int*>(cmat.ptr(0)));
EXPECT_EQ(& data[1], reinterpret_cast<const int*>(cmat.ptr(0, 1)));
EXPECT_EQ(& data[stepInPixels], reinterpret_cast<const int*>(cmat.ptr(1)));
EXPECT_EQ(& data[stepInPixels +1], reinterpret_cast<const int*>(cmat.ptr(1,1)));
}
TEST(OwnMat, CopyToWithStep)
{
constexpr int width = 8;
constexpr int height = 8;
constexpr int stepInPixels = 16;
std::array<int, height * stepInPixels> data;
for (size_t i = 0; i < data.size(); i++)
{
data[i] = static_cast<int>(i);
}
Mat mat(height, width, CV_32S, data.data(), stepInPixels * sizeof(int));
Mat dst;
mat.copyTo(dst);
EXPECT_NE(mat.data, dst.data);
EXPECT_EQ(0, cvtest::norm(to_ocv(mat), to_ocv(dst), NORM_INF))
<< to_ocv(mat) << std::endl
<< (to_ocv(mat) != to_ocv(dst));
}
TEST(OwnMat, AssignNDtoRegular)
{
const auto sz = cv::gapi::own::Size{32,32};
const auto dims = Dims{1,3,224,224};
Mat a;
a.create(sz, CV_8U);
const auto *old_ptr = a.data;
ASSERT_NE(nullptr , a.data);
ASSERT_EQ(sz , (cv::gapi::own::Size{a.cols, a.rows}));
ASSERT_EQ(static_cast<size_t>(sz.width) * sz.height, a.total());
ASSERT_EQ(CV_8U , a.type());
ASSERT_EQ(CV_8U , a.depth());
ASSERT_EQ(1 , a.channels());
ASSERT_EQ(sizeof(uint8_t), a.elemSize());
ASSERT_EQ(static_cast<size_t>(sz.width), a.step);
ASSERT_TRUE(a.dims.empty());
Mat b;
b.create(dims, CV_32F);
a = b;
ASSERT_NE(nullptr , a.data);
ASSERT_NE(old_ptr , a.data);
ASSERT_EQ((cv::gapi::own::Size{0,0}), (cv::gapi::own::Size{a.cols, a.rows}));
ASSERT_EQ(multiply_dims(dims), a.total());
ASSERT_EQ(CV_32F , a.type());
ASSERT_EQ(CV_32F , a.depth());
ASSERT_EQ(-1 , a.channels());
ASSERT_EQ(sizeof(float), a.elemSize());
ASSERT_EQ(0u , a.step);
ASSERT_EQ(dims , a.dims);
}
TEST(OwnMat, AssignRegularToND)
{
const auto sz = cv::gapi::own::Size{32,32};
const auto dims = Dims{1,3,224,224};
Mat a;
a.create(dims, CV_32F);
const auto *old_ptr = a.data;
ASSERT_NE(nullptr , a.data);
ASSERT_EQ((cv::gapi::own::Size{0,0}), (cv::gapi::own::Size{a.cols, a.rows}));
ASSERT_EQ(multiply_dims(dims), a.total());
ASSERT_EQ(CV_32F , a.type());
ASSERT_EQ(CV_32F , a.depth());
ASSERT_EQ(-1 , a.channels());
ASSERT_EQ(sizeof(float), a.elemSize());
ASSERT_EQ(0u , a.step);
ASSERT_EQ(dims , a.dims);
Mat b;
b.create(sz, CV_8U);
a = b;
ASSERT_NE(nullptr , a.data);
ASSERT_NE(old_ptr , a.data);
ASSERT_EQ(sz , (cv::gapi::own::Size{a.cols, a.rows}));
ASSERT_EQ(static_cast<size_t>(sz.width) * sz.height, a.total());
ASSERT_EQ(CV_8U , a.type());
ASSERT_EQ(CV_8U , a.depth());
ASSERT_EQ(1 , a.channels());
ASSERT_EQ(sizeof(uint8_t), a.elemSize());
ASSERT_EQ(static_cast<size_t>(sz.width), a.step);
ASSERT_TRUE(a.dims.empty());
}
TEST(OwnMat, CopyNDtoRegular)
{
const auto sz = cv::gapi::own::Size{32,32};
const auto dims = Dims{1,3,224,224};
Mat a;
a.create(sz, CV_8U);
const auto *old_ptr = a.data;
ASSERT_NE(nullptr , a.data);
ASSERT_EQ(sz , (cv::gapi::own::Size{a.cols, a.rows}));
ASSERT_EQ(static_cast<size_t>(sz.width) * sz.height, a.total());
ASSERT_EQ(CV_8U , a.type());
ASSERT_EQ(CV_8U , a.depth());
ASSERT_EQ(1 , a.channels());
ASSERT_EQ(sizeof(uint8_t), a.elemSize());
ASSERT_EQ(static_cast<size_t>(sz.width), a.step);
ASSERT_TRUE(a.dims.empty());
Mat b;
b.create(dims, CV_32F);
b.copyTo(a);
ASSERT_NE(nullptr , a.data);
ASSERT_NE(old_ptr , a.data);
ASSERT_NE(b.data , a.data);
ASSERT_EQ((cv::gapi::own::Size{0,0}), (cv::gapi::own::Size{a.cols, a.rows}));
ASSERT_EQ(multiply_dims(dims), a.total());
ASSERT_EQ(CV_32F , a.type());
ASSERT_EQ(CV_32F , a.depth());
ASSERT_EQ(-1 , a.channels());
ASSERT_EQ(sizeof(float), a.elemSize());
ASSERT_EQ(0u , a.step);
ASSERT_EQ(dims , a.dims);
}
TEST(OwnMat, CopyRegularToND)
{
const auto sz = cv::gapi::own::Size{32,32};
const auto dims = Dims{1,3,224,224};
Mat a;
a.create(dims, CV_32F);
const auto *old_ptr = a.data;
ASSERT_NE(nullptr , a.data);
ASSERT_EQ((cv::gapi::own::Size{0,0}), (cv::gapi::own::Size{a.cols, a.rows}));
ASSERT_EQ(multiply_dims(dims), a.total());
ASSERT_EQ(CV_32F , a.type());
ASSERT_EQ(CV_32F , a.depth());
ASSERT_EQ(-1 , a.channels());
ASSERT_EQ(sizeof(float), a.elemSize());
ASSERT_EQ(0u , a.step);
ASSERT_EQ(dims , a.dims);
Mat b;
b.create(sz, CV_8U);
b.copyTo(a);
ASSERT_NE(nullptr , a.data);
ASSERT_NE(old_ptr , a.data);
ASSERT_NE(b.data , a.data);
ASSERT_EQ(sz , (cv::gapi::own::Size{a.cols, a.rows}));
ASSERT_EQ(static_cast<size_t>(sz.width) * sz.height, a.total());
ASSERT_EQ(CV_8U , a.type());
ASSERT_EQ(CV_8U , a.depth());
ASSERT_EQ(1 , a.channels());
ASSERT_EQ(sizeof(uint8_t), a.elemSize());
ASSERT_EQ(static_cast<size_t>(sz.width), a.step);
ASSERT_TRUE(a.dims.empty());
}
TEST(OwnMat, ScalarAssign32SC1)
{
constexpr int width = 8;
constexpr int height = 8;
constexpr int stepInPixels = 16;
std::array<int, height * stepInPixels> data;
for (size_t i = 0; i < data.size(); i++)
{
data[i] = static_cast<int>(i);
}
Mat mat(height, width, CV_32S, data.data(), stepInPixels * sizeof(data[0]));
mat = cv::gapi::own::Scalar{-1};
std::array<int, height * stepInPixels> expected;
for (size_t row = 0; row < height; row++)
{
for (size_t col = 0; col < stepInPixels; col++)
{
auto index = row*stepInPixels + col;
expected[index] = col < width ? -1 : static_cast<int>(index);
}
}
auto cmp_result_mat = (cv::Mat{height, stepInPixels, CV_32S, data.data()} != cv::Mat{height, stepInPixels, CV_32S, expected.data()});
EXPECT_EQ(0, cvtest::norm(cmp_result_mat, NORM_INF))
<< cmp_result_mat;
}
TEST(OwnMat, ScalarAssign8UC1)
{
constexpr int width = 8;
constexpr int height = 8;
constexpr int stepInPixels = 16;
std::array<uchar, height * stepInPixels> data;
for (size_t i = 0; i < data.size(); i++)
{
data[i] = static_cast<uchar>(i);
}
Mat mat(height, width, CV_8U, data.data(), stepInPixels * sizeof(data[0]));
mat = cv::gapi::own::Scalar{-1};
std::array<uchar, height * stepInPixels> expected;
for (size_t row = 0; row < height; row++)
{
for (size_t col = 0; col < stepInPixels; col++)
{
auto index = row*stepInPixels + col;
expected[index] = col < width ? cv::saturate_cast<uchar>(-1) : static_cast<uchar>(index);
}
}
auto cmp_result_mat = (cv::Mat{height, stepInPixels, CV_8U, data.data()} != cv::Mat{height, stepInPixels, CV_8U, expected.data()});
EXPECT_EQ(0, cvtest::norm(cmp_result_mat, NORM_INF))
<< cmp_result_mat;
}
TEST(OwnMat, ScalarAssignND)
{
std::vector<int> dims = {1,1000};
Mat m;
m.create(dims, CV_32F);
m = cv::gapi::own::Scalar{-1};
const float *ptr = reinterpret_cast<float*>(m.data);
for (auto i = 0u; i < m.total(); i++) {
EXPECT_EQ(-1.f, ptr[i]);
}
}
TEST(OwnMat, ScalarAssign8UC3)
{
constexpr auto cv_type = CV_8SC3;
constexpr int channels = 3;
constexpr int width = 8;
constexpr int height = 8;
constexpr int stepInPixels = 16;
std::array<schar, height * stepInPixels * channels> data;
for (size_t i = 0; i < data.size(); i+= channels)
{
data[i + 0] = static_cast<schar>(10 * i + 0);
data[i + 1] = static_cast<schar>(10 * i + 1);
data[i + 2] = static_cast<schar>(10 * i + 2);
}
Mat mat(height, width, cv_type, data.data(), channels * stepInPixels * sizeof(data[0]));
mat = cv::gapi::own::Scalar{-10, -11, -12};
std::array<schar, data.size()> expected;
for (size_t row = 0; row < height; row++)
{
for (size_t col = 0; col < stepInPixels; col++)
{
int index = static_cast<int>(channels * (row*stepInPixels + col));
expected[index + 0] = static_cast<schar>(col < width ? -10 : 10 * index + 0);
expected[index + 1] = static_cast<schar>(col < width ? -11 : 10 * index + 1);
expected[index + 2] = static_cast<schar>(col < width ? -12 : 10 * index + 2);
}
}
auto cmp_result_mat = (cv::Mat{height, stepInPixels, cv_type, data.data()} != cv::Mat{height, stepInPixels, cv_type, expected.data()});
EXPECT_EQ(0, cvtest::norm(cmp_result_mat, NORM_INF))
<< cmp_result_mat << std::endl
<< "data : " << std::endl
<< cv::Mat{height, stepInPixels, cv_type, data.data()} << std::endl
<< "expected : " << std::endl
<< cv::Mat{height, stepInPixels, cv_type, expected.data()} << std::endl;
}
TEST(OwnMat, ROIView)
{
constexpr int width = 8;
constexpr int height = 8;
constexpr int stepInPixels = 16;
std::array<uchar, height * stepInPixels> data;
for (size_t i = 0; i < data.size(); i++)
{
data[i] = static_cast<uchar>(i);
}
// std::cout<<cv::Mat{height, stepInPixels, CV_8U, data.data()}<<std::endl;
std::array<uchar, 4 * 4> expected;
for (size_t row = 0; row < 4; row++)
{
for (size_t col = 0; col < 4; col++)
{
expected[row*4 +col] = static_cast<uchar>(stepInPixels * (2 + row) + 2 + col);
}
}
Mat mat(height, width, CV_8U, data.data(), stepInPixels * sizeof(data[0]));
Mat roi_view (mat, cv::gapi::own::Rect{2,2,4,4});
// std::cout<<cv::Mat{4, 4, CV_8U, expected.data()}<<std::endl;
//
auto expected_cv_mat = cv::Mat{4, 4, CV_8U, expected.data()};
auto cmp_result_mat = (to_ocv(roi_view) != expected_cv_mat);
EXPECT_EQ(0, cvtest::norm(cmp_result_mat, NORM_INF))
<< cmp_result_mat << std::endl
<< to_ocv(roi_view) << std::endl
<< expected_cv_mat << std::endl;
}
TEST(OwnMat, CreateWithNegativeDims)
{
Mat own_mat;
ASSERT_ANY_THROW(own_mat.create(cv::Size{-1, -1}, CV_8U));
}
TEST(OwnMat, CreateWithNegativeWidth)
{
Mat own_mat;
ASSERT_ANY_THROW(own_mat.create(cv::Size{-1, 1}, CV_8U));
}
TEST(OwnMat, CreateWithNegativeHeight)
{
Mat own_mat;
ASSERT_ANY_THROW(own_mat.create(cv::Size{1, -1}, CV_8U));
}
TEST(OwnMat, ZeroHeightMat)
{
cv::GMat in, a, b, c, d;
std::tie(a, b, c, d) = cv::gapi::split4(in);
cv::GMat out = cv::gapi::merge3(a, b, c);
cv::Mat in_mat(cv::Size(8, 0), CV_8UC4);
cv::Mat out_mat(cv::Size(8, 8), CV_8UC3);
cv::GComputation comp(cv::GIn(in), cv::GOut(out));
ASSERT_ANY_THROW(comp.apply(cv::gin(in_mat), cv::gout(out_mat),
cv::compile_args(cv::gapi::core::fluid::kernels())));
}
TEST(OwnMat, ZeroWidthMat)
{
cv::GMat in, a, b, c, d;
std::tie(a, b, c, d) = cv::gapi::split4(in);
cv::GMat out = cv::gapi::merge3(a, b, c);
cv::Mat in_mat(cv::Size(0, 8), CV_8UC4);
cv::Mat out_mat(cv::Size(8, 8), CV_8UC3);
cv::GComputation comp(cv::GIn(in), cv::GOut(out));
ASSERT_ANY_THROW(comp.apply(cv::gin(in_mat), cv::gout(out_mat),
cv::compile_args(cv::gapi::core::fluid::kernels())));
}
} // namespace opencv_test

View File

@ -0,0 +1,44 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2018 Intel Corporation
#include "../test_precomp.hpp"
#include <opencv2/gapi/own/scalar.hpp>
namespace opencv_test
{
TEST(Scalar, CreateEmpty)
{
cv::gapi::own::Scalar s;
for (int i = 0; i < 4; ++i)
{
EXPECT_EQ(s[i], 0.0);
}
}
TEST(Scalar, CreateFromVal)
{
cv::gapi::own::Scalar s(5.0);
EXPECT_EQ(s[0], 5.0);
EXPECT_EQ(s[1], 0.0);
EXPECT_EQ(s[2], 0.0);
EXPECT_EQ(s[3], 0.0);
}
TEST(Scalar, CreateFromVals)
{
cv::gapi::own::Scalar s(5.3, 3.3, 4.1, -2.0);
EXPECT_EQ(s[0], 5.3);
EXPECT_EQ(s[1], 3.3);
EXPECT_EQ(s[2], 4.1);
EXPECT_EQ(s[3], -2.0);
}
} // namespace opencv_test