Commit fb61f88b authored by Alexander Alekhin's avatar Alexander Alekhin

Merge remote-tracking branch 'upstream/3.4' into merge-3.4

parents 1e32781b 38e6d668
......@@ -68,6 +68,9 @@ if(POLICY CMP0075)
cmake_policy(SET CMP0075 NEW) # CMake 3.12+: Include file check macros honor `CMAKE_REQUIRED_LIBRARIES`
endif()
if(POLICY CMP0077)
cmake_policy(SET CMP0077 NEW) # CMake 3.13+: option() honors normal variables.
endif()
#
# Configure OpenCV CMake hooks
......
......@@ -57,6 +57,14 @@ low light, low light values are discarded using **cv.inRange()** function.
@include samples/python/tutorial_code/video/meanshift/meanshift.py
@end_toggle
@add_toggle_java
- **Downloadable code**: Click
[here](https://github.com/opencv/opencv/tree/master/samples/java/tutorial_code/video/meanshift/MeanshiftDemo.java)
- **Code at glance:**
@include samples/java/tutorial_code/video/meanshift/MeanshiftDemo.java
@end_toggle
Three frames in a video I used is given below:
![image](images/meanshift_result.jpg)
......@@ -98,6 +106,14 @@ parameters (used to be passed as search window in next iteration). See the code
@include samples/python/tutorial_code/video/meanshift/camshift.py
@end_toggle
@add_toggle_java
- **Downloadable code**: Click
[here](https://github.com/opencv/opencv/tree/master/samples/java/tutorial_code/video/meanshift/CamshiftDemo.java)
- **Code at glance:**
@include samples/java/tutorial_code/video/meanshift/CamshiftDemo.java
@end_toggle
Three frames of the result is shown below:
![image](images/camshift_result.jpg)
......
......@@ -109,6 +109,15 @@ below:
@include samples/python/tutorial_code/video/optical_flow/optical_flow.py
@end_toggle
@add_toggle_java
- **Downloadable code**: Click
[here](https://github.com/opencv/opencv/tree/master/samples/java/tutorial_code/video/optical_flow/OpticalFlowDemo.java)
- **Code at glance:**
@include samples/java/tutorial_code/video/optical_flow/OpticalFlowDemo.java
@end_toggle
(This code doesn't check how correct are the next keypoints. So even if any feature point disappears
in image, there is a chance that optical flow finds the next point which may look close to it. So
actually for a robust tracking, corner points should be detected in particular intervals. OpenCV
......@@ -151,6 +160,15 @@ corresponds to Value plane. See the code below:
@end_toggle
@add_toggle_java
- **Downloadable code**: Click
[here](https://github.com/opencv/opencv/tree/master/samples/java/tutorial_code/video/optical_flow/OpticalFlowDenseDemo.java)
- **Code at glance:**
@include samples/java/tutorial_code/video/optical_flow/OpticalFlowDenseDemo.java
@end_toggle
See the result below:
![image](images/opticalfb.jpg)
......@@ -17,12 +17,12 @@ tracking and foreground extractions.
- @subpage tutorial_meanshift
*Languages:* C++, Python
*Languages:* C++, Java, Python
Learn how to use the Meanshift and Camshift algorithms to track objects in videos.
- @subpage tutorial_optical_flow
*Languages:* C++, Python
*Languages:* C++, Java, Python
We will learn how to use optical flow methods to track sparse features or to create a dense representation.
......@@ -99,6 +99,7 @@ enum StoreMode
}
// TODO FIXIT: Don't use "God" traits. Split on separate cases.
template<typename _Tp> struct V_TypeTraits
{
};
......@@ -130,21 +131,51 @@ template<typename _Tp> struct V_TypeTraits
} \
}
#define CV_INTRIN_DEF_TYPE_TRAITS_NO_Q_TYPE(type, int_type_, uint_type_, abs_type_, w_type_, sum_type_, nlanes128_) \
template<> struct V_TypeTraits<type> \
{ \
typedef type value_type; \
typedef int_type_ int_type; \
typedef abs_type_ abs_type; \
typedef uint_type_ uint_type; \
typedef w_type_ w_type; \
typedef sum_type_ sum_type; \
enum { nlanes128 = nlanes128_ }; \
\
static inline int_type reinterpret_int(type x) \
{ \
union { type l; int_type i; } v; \
v.l = x; \
return v.i; \
} \
\
static inline type reinterpret_from_int(int_type x) \
{ \
union { type l; int_type i; } v; \
v.i = x; \
return v.l; \
} \
}
CV_INTRIN_DEF_TYPE_TRAITS(uchar, schar, uchar, uchar, ushort, unsigned, unsigned, 16);
CV_INTRIN_DEF_TYPE_TRAITS(schar, schar, uchar, uchar, short, int, int, 16);
CV_INTRIN_DEF_TYPE_TRAITS(ushort, short, ushort, ushort, unsigned, uint64, unsigned, 8);
CV_INTRIN_DEF_TYPE_TRAITS(short, short, ushort, ushort, int, int64, int, 8);
CV_INTRIN_DEF_TYPE_TRAITS(unsigned, int, unsigned, unsigned, uint64, void, unsigned, 4);
CV_INTRIN_DEF_TYPE_TRAITS(int, int, unsigned, unsigned, int64, void, int, 4);
CV_INTRIN_DEF_TYPE_TRAITS(float, int, unsigned, float, double, void, float, 4);
CV_INTRIN_DEF_TYPE_TRAITS(uint64, int64, uint64, uint64, void, void, uint64, 2);
CV_INTRIN_DEF_TYPE_TRAITS(int64, int64, uint64, uint64, void, void, int64, 2);
CV_INTRIN_DEF_TYPE_TRAITS(double, int64, uint64, double, void, void, double, 2);
CV_INTRIN_DEF_TYPE_TRAITS_NO_Q_TYPE(unsigned, int, unsigned, unsigned, uint64, unsigned, 4);
CV_INTRIN_DEF_TYPE_TRAITS_NO_Q_TYPE(int, int, unsigned, unsigned, int64, int, 4);
CV_INTRIN_DEF_TYPE_TRAITS_NO_Q_TYPE(float, int, unsigned, float, double, float, 4);
CV_INTRIN_DEF_TYPE_TRAITS_NO_Q_TYPE(uint64, int64, uint64, uint64, void, uint64, 2);
CV_INTRIN_DEF_TYPE_TRAITS_NO_Q_TYPE(int64, int64, uint64, uint64, void, int64, 2);
CV_INTRIN_DEF_TYPE_TRAITS_NO_Q_TYPE(double, int64, uint64, double, void, double, 2);
#ifndef CV_DOXYGEN
#ifndef CV_CPU_OPTIMIZATION_HAL_NAMESPACE
#ifdef CV_CPU_DISPATCH_MODE
#ifdef CV_FORCE_SIMD128_CPP
#define CV_CPU_OPTIMIZATION_HAL_NAMESPACE hal_EMULATOR_CPP
#define CV_CPU_OPTIMIZATION_HAL_NAMESPACE_BEGIN namespace hal_EMULATOR_CPP {
#define CV_CPU_OPTIMIZATION_HAL_NAMESPACE_END }
#elif defined(CV_CPU_DISPATCH_MODE)
#define CV_CPU_OPTIMIZATION_HAL_NAMESPACE __CV_CAT(hal_, CV_CPU_DISPATCH_MODE)
#define CV_CPU_OPTIMIZATION_HAL_NAMESPACE_BEGIN namespace __CV_CAT(hal_, CV_CPU_DISPATCH_MODE) {
#define CV_CPU_OPTIMIZATION_HAL_NAMESPACE_END }
......@@ -197,7 +228,6 @@ using namespace CV_CPU_OPTIMIZATION_HAL_NAMESPACE;
#else
#define CV_SIMD128_CPP 1
#include "opencv2/core/hal/intrin_cpp.hpp"
#endif
......@@ -242,6 +272,10 @@ CV_CPU_OPTIMIZATION_HAL_NAMESPACE_BEGIN
#define CV_SIMD128 0
#endif
#ifndef CV_SIMD128_CPP
#define CV_SIMD128_CPP 0
#endif
#ifndef CV_SIMD128_64F
#define CV_SIMD128_64F 0
#endif
......@@ -346,7 +380,7 @@ template<typename _Tp> struct V_RegTraits
CV_DEF_REG_TRAITS(v, v_int16x8, short, s16, v_uint16x8, v_int32x4, v_int64x2, v_int16x8, void);
CV_DEF_REG_TRAITS(v, v_uint32x4, unsigned, u32, v_uint32x4, v_uint64x2, void, v_int32x4, void);
CV_DEF_REG_TRAITS(v, v_int32x4, int, s32, v_uint32x4, v_int64x2, void, v_int32x4, void);
#if CV_SIMD128_64F
#if CV_SIMD128_64F || CV_SIMD128_CPP
CV_DEF_REG_TRAITS(v, v_float32x4, float, f32, v_float32x4, v_float64x2, void, v_int32x4, v_int32x4);
#else
CV_DEF_REG_TRAITS(v, v_float32x4, float, f32, v_float32x4, void, void, v_int32x4, v_int32x4);
......@@ -433,7 +467,11 @@ namespace CV__SIMD_NAMESPACE {
} // namespace
using namespace CV__SIMD_NAMESPACE;
#elif (CV_SIMD128 || CV_SIMD128_CPP) && (!defined(CV__SIMD_FORCE_WIDTH) || CV__SIMD_FORCE_WIDTH == 128)
#if defined CV_SIMD128_CPP
#define CV__SIMD_NAMESPACE simd128_cpp
#else
#define CV__SIMD_NAMESPACE simd128
#endif
namespace CV__SIMD_NAMESPACE {
#define CV_SIMD CV_SIMD128
#define CV_SIMD_64F CV_SIMD128_64F
......
......@@ -1492,7 +1492,8 @@ struct InRange_SIMD<float>
v_float32 low2 = vx_load(src2 + x + v_float32::nlanes);
v_float32 high2 = vx_load(src3 + x + v_float32::nlanes);
v_pack_store(dst + x, v_pack(v_reinterpret_as_u32((values1 >= low1) & (high1 >= values1)), v_reinterpret_as_u32((values2 >= low2) & (high2 >= values2))));
v_pack_store(dst + x, v_pack(v_reinterpret_as_u32(values1 >= low1) & v_reinterpret_as_u32(high1 >= values1),
v_reinterpret_as_u32(values2 >= low2) & v_reinterpret_as_u32(high2 >= values2)));
}
vx_cleanup();
return x;
......
......@@ -1576,7 +1576,7 @@ struct op_div_scale
}
static inline Tvec pre(const Tvec& denom, const Tvec& res)
{
const Tvec v_zero = Tvec();
const Tvec v_zero = vx_setall<typename Tvec::lane_type>(0);
return v_select(denom == v_zero, v_zero, res);
}
static inline T1 r(T1 a, T1 denom, const T2* scalar)
......@@ -1826,7 +1826,7 @@ struct op_recip
}
static inline Tvec pre(const Tvec& denom, const Tvec& res)
{
const Tvec v_zero = Tvec();
const Tvec v_zero = vx_setall<typename Tvec::lane_type>(0);
return v_select(denom == v_zero, v_zero, res);
}
static inline T1 r(T1 denom, const T2* scalar)
......
......@@ -916,8 +916,9 @@ double cv::invert( InputArray _src, OutputArray _dst, int method )
result = true;
d = 1./d;
#if CV_SIMD128
static const float CV_DECL_ALIGNED(16) inv[4] = { 0.f,-0.f,-0.f,0.f };
v_float32x4 s0 = (v_load_halves((const float*)srcdata, (const float*)(srcdata + srcstep)) * v_setall_f32((float)d)) ^ v_load((const float *)inv);//0123//3120
const float d_32f = (float)d;
const v_float32x4 d_vec(d_32f, -d_32f, -d_32f, d_32f);
v_float32x4 s0 = v_load_halves((const float*)srcdata, (const float*)(srcdata + srcstep)) * d_vec;//0123//3120
s0 = v_extract<3>(s0, v_combine_low(v_rotate_right<1>(s0), s0));
v_store_low((float*)dstdata, s0);
v_store_high((float*)(dstdata + dststep), s0);
......@@ -946,7 +947,7 @@ double cv::invert( InputArray _src, OutputArray _dst, int method )
v_float64x2 s0 = v_load((const double*)srcdata) * det;
v_float64x2 s1 = v_load((const double*)(srcdata+srcstep)) * det;
v_float64x2 sm = v_extract<1>(s1, s0);//30
v_float64x2 ss = v_extract<1>(s0, s1) ^ v_setall_f64(-0.);//12
v_float64x2 ss = v_setall<double>(0) - v_extract<1>(s0, s1);//12
v_store((double*)dstdata, v_combine_low(sm, ss));//31
v_store((double*)(dstdata + dststep), v_combine_high(ss, sm));//20
#else
......
......@@ -725,7 +725,7 @@ void log32f( const float *_x, float *y, int n )
yf0 = v_fma(v_cvt_f32(yi0), vln2, yf0);
v_float32 delta = v_reinterpret_as_f32(h0 == vx_setall_s32(510)) & vshift;
v_float32 delta = v_select(v_reinterpret_as_f32(h0 == vx_setall_s32(510)), vshift, vx_setall<float>(0));
xf0 = v_fma((v_reinterpret_as_f32(xi0) - v1), xf0, delta);
v_float32 zf0 = v_fma(xf0, vA0, vA1);
......
......@@ -8,6 +8,7 @@
// OpenVX related functions
#include "precomp.hpp"
#include "opencv2/core/utils/tls.hpp"
#include "opencv2/core/ovx.hpp"
#include "opencv2/core/openvx/ovx_defs.hpp"
......
......@@ -3,22 +3,14 @@
// of this distribution and at http://opencv.org/license.html.
#include "test_precomp.hpp"
// see "opencv2/core/hal/intrin.hpp"
#define CV_CPU_OPTIMIZATION_HAL_NAMESPACE hal_EMULATOR_CPP
#define CV_CPU_OPTIMIZATION_HAL_NAMESPACE_BEGIN namespace hal_EMULATOR_CPP {
#define CV_CPU_OPTIMIZATION_HAL_NAMESPACE_END }
// see "opencv2/core/private/cv_cpu_include_simd_declarations.hpp"
//#define CV_CPU_OPTIMIZATION_DECLARATIONS_ONLY
#define CV_FORCE_SIMD128_CPP
#undef CV_FORCE_SIMD128_CPP
#define CV_FORCE_SIMD128_CPP 1
#undef CV_CPU_OPTIMIZATION_NAMESPACE_BEGIN
#undef CV_CPU_OPTIMIZATION_NAMESPACE_END
#define CV_CPU_OPTIMIZATION_NAMESPACE_BEGIN namespace opt_EMULATOR_CPP {
#define CV_CPU_OPTIMIZATION_NAMESPACE_END }
#include "test_intrin128.simd.hpp"
#undef CV_CPU_OPTIMIZATION_NAMESPACE_BEGIN
#undef CV_CPU_OPTIMIZATION_NAMESPACE_END
#undef CV_CPU_DISPATCH_MODE
#undef CV_FORCE_SIMD128_CPP
// tests implementation is in test_intrin_utils.hpp
......@@ -222,7 +222,10 @@ template <typename R> std::ostream & operator<<(std::ostream & out, const Data<R
return out;
}
template<typename T> static inline void EXPECT_COMPARE_EQ_(const T a, const T b);
template<typename T> static inline void EXPECT_COMPARE_EQ_(const T a, const T b)
{
EXPECT_EQ(a, b);
}
template<> inline void EXPECT_COMPARE_EQ_<float>(const float a, const float b)
{
EXPECT_FLOAT_EQ( a, b );
......@@ -742,12 +745,12 @@ template<typename R> struct TheTest
for (int i = 0; i < n; ++i)
{
SCOPED_TRACE(cv::format("i=%d", i));
EXPECT_EQ((double)dataA[i*2] * (double)dataA[i*2] +
(double)dataA[i*2 + 1] * (double)dataA[i*2 + 1], resA[i]);
EXPECT_EQ((double)dataB[i*2] * (double)dataB[i*2] +
(double)dataB[i*2 + 1] * (double)dataB[i*2 + 1], resB[i]);
EXPECT_EQ((double)dataA[i*2] * (double)dataB[i*2] +
(double)dataA[i*2 + 1] * (double)dataB[i*2 + 1] + dataC[i], resC[i]);
EXPECT_COMPARE_EQ((double)dataA[i*2] * (double)dataA[i*2] +
(double)dataA[i*2 + 1] * (double)dataA[i*2 + 1], resA[i]);
EXPECT_COMPARE_EQ((double)dataB[i*2] * (double)dataB[i*2] +
(double)dataB[i*2 + 1] * (double)dataB[i*2 + 1], resB[i]);
EXPECT_COMPARE_EQ((double)dataA[i*2] * (double)dataB[i*2] +
(double)dataA[i*2 + 1] * (double)dataB[i*2 + 1] + dataC[i], resC[i]);
}
#endif
return *this;
......
......@@ -950,6 +950,7 @@ void sortByExecutionOrder(tensorflow::GraphDef& net)
for (int i = 0; i < net.node_size(); ++i)
{
const tensorflow::NodeDef& node = net.node(i);
int numInputsInGraph = 0;
for (int j = 0; j < node.input_size(); ++j)
{
std::string inpName = node.input(j);
......@@ -957,22 +958,25 @@ void sortByExecutionOrder(tensorflow::GraphDef& net)
inpName = inpName.substr(inpName.find('^') + 1);
nodesMapIt = nodesMap.find(inpName);
CV_Assert(nodesMapIt != nodesMap.end());
edges[nodesMapIt->second].push_back(i);
if (nodesMapIt != nodesMap.end())
{
edges[nodesMapIt->second].push_back(i);
numInputsInGraph += 1;
}
}
if (node.input_size() == 0)
if (numInputsInGraph == 0)
nodesToAdd.push_back(i);
else
{
if (node.op() == "Merge" || node.op() == "RefMerge")
{
int numControlEdges = 0;
for (int j = 0; j < node.input_size(); ++j)
for (int j = 0; j < numInputsInGraph; ++j)
numControlEdges += node.input(j)[0] == '^';
numRefsToAdd[i] = numControlEdges + 1;
}
else
numRefsToAdd[i] = node.input_size();
numRefsToAdd[i] = numInputsInGraph;
}
}
......
......@@ -715,6 +715,10 @@ void TFImporter::populateNet(Net dstNet)
simplifySubgraphs(netBin);
sortByExecutionOrder(netBin);
}
else
{
sortByExecutionOrder(netTxt);
}
std::set<String> layers_to_ignore;
......
......@@ -303,7 +303,8 @@ int cornerScore<8>(const uchar* ptr, const int pixel[], int threshold)
for (k = 0; k < N; k++)
d[k] = (short)(v - ptr[pixel[k]]);
#if CV_SIMD128
#if CV_SIMD128 \
&& (!defined(CV_SIMD128_CPP) || (!defined(__GNUC__) || __GNUC__ != 5)) // "movdqa" bug on "v_load(d + 1)" line (Ubuntu 16.04 + GCC 5.4)
if (true)
{
v_int16x8 v0 = v_load(d + 1);
......
......@@ -56,65 +56,65 @@ int validateToInt(size_t sz)
#define cG (int)(0.587*(1 << SCALE) + 0.5)
#define cB ((1 << SCALE) - cR - cG)
void icvCvt_BGR2Gray_8u_C3C1R( const uchar* rgb, int rgb_step,
void icvCvt_BGR2Gray_8u_C3C1R( const uchar* bgr, int bgr_step,
uchar* gray, int gray_step,
Size size, int _swap_rb )
{
int i;
for( ; size.height--; gray += gray_step )
{
short cRGB0 = cR;
short cRGB2 = cB;
if (_swap_rb) std::swap(cRGB0, cRGB2);
for( i = 0; i < size.width; i++, rgb += 3 )
short cBGR0 = cB;
short cBGR2 = cR;
if (_swap_rb) std::swap(cBGR0, cBGR2);
for( i = 0; i < size.width; i++, bgr += 3 )
{
int t = descale( rgb[0]*cRGB0 + rgb[1]*cG + rgb[2]*cRGB2, SCALE );
int t = descale( bgr[0]*cBGR0 + bgr[1]*cG + bgr[2]*cBGR2, SCALE );
gray[i] = (uchar)t;
}
rgb += rgb_step - size.width*3;
bgr += bgr_step - size.width*3;
}
}
void icvCvt_BGRA2Gray_16u_CnC1R( const ushort* rgb, int rgb_step,
void icvCvt_BGRA2Gray_16u_CnC1R( const ushort* bgr, int bgr_step,
ushort* gray, int gray_step,
Size size, int ncn, int _swap_rb )
{
int i;
for( ; size.height--; gray += gray_step )
{
short cRGB0 = cR;
short cRGB2 = cB;
if (_swap_rb) std::swap(cRGB0, cRGB2);
for( i = 0; i < size.width; i++, rgb += ncn )
short cBGR0 = cB;
short cBGR2 = cR;
if (_swap_rb) std::swap(cBGR0, cBGR2);
for( i = 0; i < size.width; i++, bgr += ncn )
{
int t = descale( rgb[0]*cRGB0 + rgb[1]*cG + rgb[2]*cRGB2, SCALE );
int t = descale( bgr[0]*cBGR0 + bgr[1]*cG + bgr[2]*cBGR2, SCALE );
gray[i] = (ushort)t;
}
rgb += rgb_step - size.width*ncn;
bgr += bgr_step - size.width*ncn;
}
}
void icvCvt_BGRA2Gray_8u_C4C1R( const uchar* rgba, int rgba_step,
void icvCvt_BGRA2Gray_8u_C4C1R( const uchar* bgra, int rgba_step,
uchar* gray, int gray_step,
Size size, int _swap_rb )
{
int i;
for( ; size.height--; gray += gray_step )
{
short cRGB0 = cR;
short cRGB2 = cB;
if (_swap_rb) std::swap(cRGB0, cRGB2);
for( i = 0; i < size.width; i++, rgba += 4 )
short cBGR0 = cB;
short cBGR2 = cR;
if (_swap_rb) std::swap(cBGR0, cBGR2);
for( i = 0; i < size.width; i++, bgra += 4 )
{
int t = descale( rgba[0]*cRGB0 + rgba[1]*cG + rgba[2]*cRGB2, SCALE );
int t = descale( bgra[0]*cBGR0 + bgra[1]*cG + bgra[2]*cBGR2, SCALE );
gray[i] = (uchar)t;
}
rgba += rgba_step - size.width*4;
bgra += rgba_step - size.width*4;
}
}
......
......@@ -42,6 +42,7 @@
//M*/
#include "precomp.hpp"
#undef CV_FORCE_SIMD128_CPP // expected AVX implementation only
#include "opencv2/core/hal/intrin.hpp"
#include "corner.hpp"
......
This diff is collapsed.
......@@ -67,7 +67,7 @@ namespace opt_SSE4_1
void resizeNN2_SSE4_1(const Range&, const Mat&, Mat&, int*, int, double);
void resizeNN4_SSE4_1(const Range&, const Mat&, Mat&, int*, int, double);
int VResizeLanczos4Vec_32f16u_SSE41(const uchar** _src, uchar* _dst, const uchar* _beta, int width);
int VResizeLanczos4Vec_32f16u_SSE41(const float** src, ushort* dst, const float* beta, int width);
#endif
}
}
......
......@@ -186,13 +186,10 @@ void resizeNN4_SSE4_1(const Range& range, const Mat& src, Mat &dst, int *x_ofs,
parallel_for_(range, invoker, dst.total() / (double)(1 << 16));
}
int VResizeLanczos4Vec_32f16u_SSE41(const uchar** _src, uchar* _dst, const uchar* _beta, int width)
int VResizeLanczos4Vec_32f16u_SSE41(const float** src, ushort* dst, const float* beta, int width)
{
const float** src = (const float**)_src;
const float* beta = (const float*)_beta;
const float *S0 = src[0], *S1 = src[1], *S2 = src[2], *S3 = src[3],
*S4 = src[4], *S5 = src[5], *S6 = src[6], *S7 = src[7];
short * dst = (short*)_dst;
int x = 0;
__m128 v_b0 = _mm_set1_ps(beta[0]), v_b1 = _mm_set1_ps(beta[1]),
v_b2 = _mm_set1_ps(beta[2]), v_b3 = _mm_set1_ps(beta[3]),
......
......@@ -4,7 +4,7 @@
QR code detect and decode pipeline.
===============================================================================
'''
import os
import numpy as np
import cv2 as cv
......@@ -12,7 +12,7 @@ from tests_common import NewOpenCVTests
class qrcode_detector_test(NewOpenCVTests):
def test_detect_and_decode(self):
img = cv.imread(self.extraTestDataPath + '/cv/qrcode/link_ocv.jpg')
img = cv.imread(os.path.join(self.extraTestDataPath, 'cv/qrcode/link_ocv.jpg'))
detector = cv.QRCodeDetector()
retval, points, straight_qrcode = detector.detectAndDecode(img)
self.assertEqual(retval, "https://opencv.org/");
......@@ -25,6 +25,7 @@ public:
{}
explicit MyData(int) : A(97), X(CV_PI), id("mydata1234") // explicit to avoid implicit conversion
{}
//! [inside]
void write(FileStorage& fs) const //Write serialization for this class
{
fs << "{" << "A" << A << "X" << X << "id" << id << "}";
......@@ -35,6 +36,7 @@ public:
X = (double)node["X"];
id = (string)node["id"];
}
//! [inside]
public: // Data Members
int A;
double X;
......@@ -42,6 +44,7 @@ public: // Data Members
};
//These write and read functions must be defined for the serialization in FileStorage to work
//! [outside]
static void write(FileStorage& fs, const std::string&, const MyData& x)
{
x.write(fs);
......@@ -52,6 +55,7 @@ static void read(const FileNode& node, MyData& x, const MyData& default_value =
else
x.read(node);
}
//! [outside]
// This function will print our custom class to the console
static ostream& operator<<(ostream& out, const MyData& m)
......@@ -72,27 +76,48 @@ int main(int ac, char** av)
string filename = av[1];
{ //write
//! [iomati]
Mat R = Mat_<uchar>::eye(3, 3),
T = Mat_<double>::zeros(3, 1);
//! [iomati]
//! [customIOi]
MyData m(1);
//! [customIOi]
//! [open]
FileStorage fs(filename, FileStorage::WRITE);
// or:
// FileStorage fs;
// fs.open(filename, FileStorage::WRITE);
//! [open]
//! [writeNum]
fs << "iterationNr" << 100;
//! [writeNum]
//! [writeStr]
fs << "strings" << "["; // text - string sequence
fs << "image1.jpg" << "Awesomeness" << "../data/baboon.jpg";
fs << "]"; // close sequence
//! [writeStr]
//! [writeMap]
fs << "Mapping"; // text - mapping
fs << "{" << "One" << 1;
fs << "Two" << 2 << "}";
//! [writeMap]
//! [iomatw]
fs << "R" << R; // cv::Mat
fs << "T" << T;
//! [iomatw]
//! [customIOw]
fs << "MyData" << m; // your own data structures
//! [customIOw]
//! [close]
fs.release(); // explicit close
//! [close]
cout << "Write Done." << endl;
}
......@@ -101,9 +126,11 @@ int main(int ac, char** av)
FileStorage fs;
fs.open(filename, FileStorage::READ);
//! [readNum]
int itNr;
//fs["iterationNr"] >> itNr;
itNr = (int) fs["iterationNr"];
//! [readNum]
cout << itNr;
if (!fs.isOpened())
{
......@@ -112,6 +139,7 @@ int main(int ac, char** av)
return 1;
}
//! [readStr]
FileNode n = fs["strings"]; // Read string sequence - Get node
if (n.type() != FileNode::SEQ)
{
......@@ -122,19 +150,26 @@ int main(int ac, char** av)
FileNodeIterator it = n.begin(), it_end = n.end(); // Go through the node
for (; it != it_end; ++it)
cout << (string)*it << endl;
//! [readStr]
//! [readMap]
n = fs["Mapping"]; // Read mappings from a sequence
cout << "Two " << (int)(n["Two"]) << "; ";
cout << "One " << (int)(n["One"]) << endl << endl;
//! [readMap]
MyData m;
Mat R, T;
//! [iomat]
fs["R"] >> R; // Read cv::Mat
fs["T"] >> T;
//! [iomat]
//! [customIO]
fs["MyData"] >> m; // Read your own structure_
//! [customIO]
cout << endl
<< "R = " << R << endl;
......@@ -142,9 +177,11 @@ int main(int ac, char** av)
cout << "MyData = " << endl << m << endl << endl;
//Show default behavior for non existing nodes
//! [nonexist]
cout << "Attempt to read NonExisting (should initialize the data structure with its default).";
fs["NonExisting"] >> m;
cout << endl << "NonExisting = " << endl << m << endl;
//! [nonexist]
}
cout << endl
......
import java.util.Arrays;
import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.imgproc.Imgproc;
import org.opencv.video.Video;
import org.opencv.videoio.VideoCapture;
class Camshift {
public void run(String[] args) {
String filename = args[0];
VideoCapture capture = new VideoCapture(filename);
if (!capture.isOpened()) {
System.out.println("Unable to open file!");
System.exit(-1);
}
Mat frame = new Mat(), hsv_roi = new Mat(), mask = new Mat(), roi;
// take the first frame of the video
capture.read(frame);
//setup initial location of window
Rect track_window = new Rect(300, 200, 100, 50);
// set up the ROI for tracking
roi = new Mat(frame, track_window);
Imgproc.cvtColor(roi, hsv_roi, Imgproc.COLOR_BGR2HSV);
Core.inRange(hsv_roi, new Scalar(0, 60, 32), new Scalar(180, 255, 255), mask);
MatOfFloat range = new MatOfFloat(0, 256);
Mat roi_hist = new Mat();
MatOfInt histSize = new MatOfInt(180);
MatOfInt channels = new MatOfInt(0);
Imgproc.calcHist(Arrays.asList(hsv_roi), channels, mask, roi_hist, histSize, range);
Core.normalize(roi_hist, roi_hist, 0, 255, Core.NORM_MINMAX);
// Setup the termination criteria, either 10 iteration or move by atleast 1 pt
TermCriteria term_crit = new TermCriteria(TermCriteria.EPS | TermCriteria.COUNT, 10, 1);
while (true) {
Mat hsv = new Mat() , dst = new Mat();
capture.read(frame);
if (frame.empty()) {
break;
}
Imgproc.cvtColor(frame, hsv, Imgproc.COLOR_BGR2HSV);
Imgproc.calcBackProject(Arrays.asList(hsv), channels, roi_hist, dst, range, 1);
// apply camshift to get the new location
RotatedRect rot_rect = Video.CamShift(dst, track_window, term_crit);
// Draw it on image
Point[] points = new Point[4];
rot_rect.points(points);
for (int i = 0; i < 4 ;i++) {
Imgproc.line(frame, points[i], points[(i+1)%4], new Scalar(255, 0, 0),2);
}
HighGui.imshow("img2", frame);
int keyboard = HighGui.waitKey(30);
if (keyboard == 'q'|| keyboard == 27) {
break;
}
}
System.exit(0);
}
}
public class CamshiftDemo {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
new Camshift().run(args);
}
}
import java.util.Arrays;
import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.imgproc.Imgproc;
import org.opencv.video.Video;
import org.opencv.videoio.VideoCapture;
class Meanshift {
public void run(String[] args) {
String filename = args[0];
VideoCapture capture = new VideoCapture(filename);
if (!capture.isOpened()) {
System.out.println("Unable to open file!");
System.exit(-1);
}
Mat frame = new Mat(), hsv_roi = new Mat(), mask = new Mat(), roi;
// take the first frame of the video
capture.read(frame);
//setup initial location of window
Rect track_window = new Rect(300, 200, 100, 50);
// setup initial location of window
roi = new Mat(frame, track_window);
Imgproc.cvtColor(roi, hsv_roi, Imgproc.COLOR_BGR2HSV);
Core.inRange(hsv_roi, new Scalar(0, 60, 32), new Scalar(180, 255, 255), mask);
MatOfFloat range = new MatOfFloat(0, 256);
Mat roi_hist = new Mat();
MatOfInt histSize = new MatOfInt(180);
MatOfInt channels = new MatOfInt(0);
Imgproc.calcHist(Arrays.asList(hsv_roi), channels, mask, roi_hist, histSize, range);
Core.normalize(roi_hist, roi_hist, 0, 255, Core.NORM_MINMAX);
// Setup the termination criteria, either 10 iteration or move by atleast 1 pt
TermCriteria term_crit = new TermCriteria(TermCriteria.EPS | TermCriteria.COUNT, 10, 1);
while (true) {
Mat hsv = new Mat() , dst = new Mat();
capture.read(frame);
if (frame.empty()) {
break;
}
Imgproc.cvtColor(frame, hsv, Imgproc.COLOR_BGR2HSV);
Imgproc.calcBackProject(Arrays.asList(hsv), channels, roi_hist, dst, range, 1);
// apply meanshift to get the new location
Video.meanShift(dst, track_window, term_crit);
// Draw it on image
Imgproc.rectangle(frame, track_window, new Scalar(255, 0, 0), 2);
HighGui.imshow("img2", frame);
int keyboard = HighGui.waitKey(30);
if (keyboard == 'q' || keyboard == 27) {
break;
}
}
System.exit(0);
}
}
public class MeanshiftDemo {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
new Meanshift().run(args);
}
}
import java.util.ArrayList;
import java.util.Random;
import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.imgproc.Imgproc;
import org.opencv.video.Video;
import org.opencv.videoio.VideoCapture;
class OptFlow {
public void run(String[] args) {
String filename = args[0];
VideoCapture capture = new VideoCapture(filename);
if (!capture.isOpened()) {
System.out.println("Unable to open this file");
System.exit(-1);
}
// Create some random colors
Scalar[] colors = new Scalar[100];
Random rng = new Random();
for (int i = 0 ; i < 100 ; i++) {
int r = rng.nextInt(256);
int g = rng.nextInt(256);
int b = rng.nextInt(256);
colors[i] = new Scalar(r, g, b);
}
Mat old_frame = new Mat() , old_gray = new Mat();
// Since the function Imgproc.goodFeaturesToTrack requires MatofPoint
// therefore first p0MatofPoint is passed to the function and then converted to MatOfPoint2f
MatOfPoint p0MatofPoint = new MatOfPoint();
capture.read(old_frame);
Imgproc.cvtColor(old_frame, old_gray, Imgproc.COLOR_BGR2GRAY);
Imgproc.goodFeaturesToTrack(old_gray, p0MatofPoint,100,0.3,7, new Mat(),7,false,0.04);
MatOfPoint2f p0 = new MatOfPoint2f(p0MatofPoint.toArray()) , p1 = new MatOfPoint2f();
// Create a mask image for drawing purposes
Mat mask = Mat.zeros(old_frame.size(), old_frame.type());
while (true) {
Mat frame = new Mat(), frame_gray = new Mat();
capture.read(frame);
if (frame.empty()) {
break;
}
Imgproc.cvtColor(frame, frame_gray, Imgproc.COLOR_BGR2GRAY);
// calculate optical flow
MatOfByte status = new MatOfByte();
MatOfFloat err = new MatOfFloat();
TermCriteria criteria = new TermCriteria(TermCriteria.COUNT + TermCriteria.EPS,10,0.03);
Video.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, p1, status, err, new Size(15,15),2, criteria);
byte StatusArr[] = status.toArray();
Point p0Arr[] = p0.toArray();
Point p1Arr[] = p1.toArray();
ArrayList<Point> good_new = new ArrayList<>();
for (int i = 0; i<StatusArr.length ; i++ ) {
if (StatusArr[i] == 1) {
good_new.add(p1Arr[i]);
Imgproc.line(mask, p1Arr[i], p0Arr[i], colors[i],2);
Imgproc.circle(frame, p1Arr[i],5, colors[i],-1);
}
}
Mat img = new Mat();
Core.add(frame, mask, img);
HighGui.imshow("Frame", img);
int keyboard = HighGui.waitKey(30);
if (keyboard == 'q' || keyboard == 27) {
break;
}
// Now update the previous frame and previous points
old_gray = frame_gray.clone();
Point[] good_new_arr = new Point[good_new.size()];
good_new_arr = good_new.toArray(good_new_arr);
p0 = new MatOfPoint2f(good_new_arr);
}
System.exit(0);
}
}
public class OpticalFlowDemo {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
new OptFlow().run(args);
}
}
import java.util.ArrayList;
import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.imgproc.Imgproc;
import org.opencv.video.Video;
import org.opencv.videoio.VideoCapture;
class OptFlowDense {
public void run(String[] args) {
String filename = args[0];
VideoCapture capture = new VideoCapture(filename);
if (!capture.isOpened()) {
//error in opening the video input
System.out.println("Unable to open file!");
System.exit(-1);
}
Mat frame1 = new Mat() , prvs = new Mat();
capture.read(frame1);
Imgproc.cvtColor(frame1, prvs, Imgproc.COLOR_BGR2GRAY);
while (true) {
Mat frame2 = new Mat(), next = new Mat();
capture.read(frame2);
if (frame2.empty()) {
break;
}
Imgproc.cvtColor(frame2, next, Imgproc.COLOR_BGR2GRAY);
Mat flow = new Mat(prvs.size(), CvType.CV_32FC2);
Video.calcOpticalFlowFarneback(prvs, next, flow,0.5,3,15,3,5,1.2,0);
// visualization
ArrayList<Mat> flow_parts = new ArrayList<>(2);
Core.split(flow, flow_parts);
Mat magnitude = new Mat(), angle = new Mat(), magn_norm = new Mat();
Core.cartToPolar(flow_parts.get(0), flow_parts.get(1), magnitude, angle,true);
Core.normalize(magnitude, magn_norm,0.0,1.0, Core.NORM_MINMAX);
float factor = (float) ((1.0/360.0)*(180.0/255.0));
Mat new_angle = new Mat();
Core.multiply(angle, new Scalar(factor), new_angle);
//build hsv image
ArrayList<Mat> _hsv = new ArrayList<>() ;
Mat hsv = new Mat(), hsv8 = new Mat(), bgr = new Mat();
_hsv.add(new_angle);
_hsv.add(Mat.ones(angle.size(), CvType.CV_32F));
_hsv.add(magn_norm);
Core.merge(_hsv, hsv);
hsv.convertTo(hsv8, CvType.CV_8U, 255.0);
Imgproc.cvtColor(hsv8, bgr, Imgproc.COLOR_HSV2BGR);
HighGui.imshow("frame2", bgr);
int keyboard = HighGui.waitKey(30);
if (keyboard == 'q' || keyboard == 27) {
break;
}
prvs = next;
}
System.exit(0);
}
}
public class OpticalFlowDenseDemo {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
new OptFlowDense().run(args);
}
}
from __future__ import print_function
import numpy as np
import cv2 as cv
import sys
def help(filename):
print (
'''
{0} shows the usage of the OpenCV serialization functionality. \n\n
usage:\n
python3 {0} outputfile.yml.gz\n\n
The output file may be either in XML, YAML or JSON. You can even compress it\n
by specifying this in its extension like xml.gz yaml.gz etc... With\n
FileStorage you can serialize objects in OpenCV.\n\n
For example: - create a class and have it serialized\n
- use it to read and write matrices.\n
'''.format(filename)
)
class MyData:
A = 97
X = np.pi
name = 'mydata1234'
def __repr__(self):
s = '{ name = ' + self.name + ', X = ' + str(self.X)
s = s + ', A = ' + str(self.A) + '}'
return s
## [inside]
def write(self, fs):
fs.write('MyData','{')
fs.write('A', self.A)
fs.write('X', self.X)
fs.write('name', self.name)
fs.write('MyData','}')
def read(self, node):
if (not node.empty()):
self.A = int(node.getNode('A').real())
self.X = node.getNode('X').real()
self.name = node.getNode('name').string()
else:
self.A = self.X = 0
self.name = ''
## [inside]
def main(argv):
if len(argv) != 2:
help(argv[0])
exit(1)
# write
## [iomati]
R = np.eye(3,3)
T = np.zeros((3,1))
## [iomati]
## [customIOi]
m = MyData()
## [customIOi]
filename = argv[1]
## [open]
s = cv.FileStorage(filename, cv.FileStorage_WRITE)
# or:
# s = cv.FileStorage()
# s.open(filename, cv.FileStorage_WRITE)
## [open]
## [writeNum]
s.write('iterationNr', 100)
## [writeNum]
## [writeStr]
s.write('strings', '[')
s.write('image1.jpg','Awesomeness')
s.write('../data/baboon.jpg',']')
## [writeStr]
## [writeMap]
s.write ('Mapping', '{')
s.write ('One', 1)
s.write ('Two', 2)
s.write ('Mapping', '}')
## [writeMap]
## [iomatw]
s.write ('R_MAT', R)
s.write ('T_MAT', T)
## [iomatw]
## [customIOw]
m.write(s)
## [customIOw]
## [close]
s.release()
## [close]
print ('Write Done.')
# read
print ('\nReading: ')
s = cv.FileStorage()
s.open(filename, cv.FileStorage_READ)
## [readNum]
n = s.getNode('iterationNr')
itNr = int(n.real())
## [readNum]
print (itNr)
if (not s.isOpened()):
print ('Failed to open ', filename, file=sys.stderr)
help(argv[0])
exit(1)
## [readStr]
n = s.getNode('strings')
if (not n.isSeq()):
print ('strings is not a sequence! FAIL', file=sys.stderr)
exit(1)
for i in range(n.size()):
print (n.at(i).string())
## [readStr]
## [readMap]
n = s.getNode('Mapping')
print ('Two',int(n.getNode('Two').real()),'; ')
print ('One',int(n.getNode('One').real()),'\n')
## [readMap]
## [iomat]
R = s.getNode('R_MAT').mat()
T = s.getNode('T_MAT').mat()
## [iomat]
## [customIO]
m.read(s.getNode('MyData'))
## [customIO]
print ('\nR =',R)
print ('T =',T,'\n')
print ('MyData =','\n',m,'\n')
## [nonexist]
print ('Attempt to read NonExisting (should initialize the data structure',
'with its default).')
m.read(s.getNode('NonExisting'))
print ('\nNonExisting =','\n',m)
## [nonexist]
print ('\nTip: Open up',filename,'with a text editor to see the serialized data.')
if __name__ == '__main__':
main(sys.argv)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment