Commit 1996ae4a authored by Alexander Alekhin's avatar Alexander Alekhin

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

parents dec25e85 bb930665
......@@ -401,7 +401,7 @@ macro(ocv_clear_vars)
endmacro()
set(OCV_COMPILER_FAIL_REGEX
"argument '.*' is not valid" # GCC 9+
"argument .* is not valid" # GCC 9+ (including support of unicode quotes)
"command line option .* is valid for .* but not for C\\+\\+" # GNU
"command line option .* is valid for .* but not for C" # GNU
"unrecognized .*option" # GNU
......
......@@ -7,25 +7,25 @@ Goal
In this chapter,
- We will understand the concepts behind Harris Corner Detection.
- We will see the functions: **cv.cornerHarris()**, **cv.cornerSubPix()**
- We will see the following functions: **cv.cornerHarris()**, **cv.cornerSubPix()**
Theory
------
In last chapter, we saw that corners are regions in the image with large variation in intensity in
In the last chapter, we saw that corners are regions in the image with large variation in intensity in
all the directions. One early attempt to find these corners was done by **Chris Harris & Mike
Stephens** in their paper **A Combined Corner and Edge Detector** in 1988, so now it is called
Harris Corner Detector. He took this simple idea to a mathematical form. It basically finds the
the Harris Corner Detector. He took this simple idea to a mathematical form. It basically finds the
difference in intensity for a displacement of \f$(u,v)\f$ in all directions. This is expressed as below:
\f[E(u,v) = \sum_{x,y} \underbrace{w(x,y)}_\text{window function} \, [\underbrace{I(x+u,y+v)}_\text{shifted intensity}-\underbrace{I(x,y)}_\text{intensity}]^2\f]
Window function is either a rectangular window or gaussian window which gives weights to pixels
The window function is either a rectangular window or a Gaussian window which gives weights to pixels
underneath.
We have to maximize this function \f$E(u,v)\f$ for corner detection. That means, we have to maximize the
second term. Applying Taylor Expansion to above equation and using some mathematical steps (please
refer any standard text books you like for full derivation), we get the final equation as:
We have to maximize this function \f$E(u,v)\f$ for corner detection. That means we have to maximize the
second term. Applying Taylor Expansion to the above equation and using some mathematical steps (please
refer to any standard text books you like for full derivation), we get the final equation as:
\f[E(u,v) \approx \begin{bmatrix} u & v \end{bmatrix} M \begin{bmatrix} u \\ v \end{bmatrix}\f]
......@@ -34,20 +34,20 @@ where
\f[M = \sum_{x,y} w(x,y) \begin{bmatrix}I_x I_x & I_x I_y \\
I_x I_y & I_y I_y \end{bmatrix}\f]
Here, \f$I_x\f$ and \f$I_y\f$ are image derivatives in x and y directions respectively. (Can be easily found
out using **cv.Sobel()**).
Here, \f$I_x\f$ and \f$I_y\f$ are image derivatives in x and y directions respectively. (These can be easily found
using **cv.Sobel()**).
Then comes the main part. After this, they created a score, basically an equation, which will
determine if a window can contain a corner or not.
Then comes the main part. After this, they created a score, basically an equation, which
determines if a window can contain a corner or not.
\f[R = det(M) - k(trace(M))^2\f]
where
- \f$det(M) = \lambda_1 \lambda_2\f$
- \f$trace(M) = \lambda_1 + \lambda_2\f$
- \f$\lambda_1\f$ and \f$\lambda_2\f$ are the eigen values of M
- \f$\lambda_1\f$ and \f$\lambda_2\f$ are the eigenvalues of M
So the values of these eigen values decide whether a region is corner, edge or flat.
So the magnitudes of these eigenvalues decide whether a region is a corner, an edge, or flat.
- When \f$|R|\f$ is small, which happens when \f$\lambda_1\f$ and \f$\lambda_2\f$ are small, the region is
flat.
......@@ -60,16 +60,16 @@ It can be represented in a nice picture as follows:
![image](images/harris_region.jpg)
So the result of Harris Corner Detection is a grayscale image with these scores. Thresholding for a
suitable give you the corners in the image. We will do it with a simple image.
suitable score gives you the corners in the image. We will do it with a simple image.
Harris Corner Detector in OpenCV
--------------------------------
OpenCV has the function **cv.cornerHarris()** for this purpose. Its arguments are :
OpenCV has the function **cv.cornerHarris()** for this purpose. Its arguments are:
- **img** - Input image, it should be grayscale and float32 type.
- **img** - Input image. It should be grayscale and float32 type.
- **blockSize** - It is the size of neighbourhood considered for corner detection
- **ksize** - Aperture parameter of Sobel derivative used.
- **ksize** - Aperture parameter of the Sobel derivative used.
- **k** - Harris detector free parameter in the equation.
See the example below:
......@@ -103,12 +103,12 @@ Corner with SubPixel Accuracy
Sometimes, you may need to find the corners with maximum accuracy. OpenCV comes with a function
**cv.cornerSubPix()** which further refines the corners detected with sub-pixel accuracy. Below is
an example. As usual, we need to find the harris corners first. Then we pass the centroids of these
an example. As usual, we need to find the Harris corners first. Then we pass the centroids of these
corners (There may be a bunch of pixels at a corner, we take their centroid) to refine them. Harris
corners are marked in red pixels and refined corners are marked in green pixels. For this function,
we have to define the criteria when to stop the iteration. We stop it after a specified number of
iteration or a certain accuracy is achieved, whichever occurs first. We also need to define the size
of neighbourhood it would search for corners.
iterations or a certain accuracy is achieved, whichever occurs first. We also need to define the size
of the neighbourhood it searches for corners.
@code{.py}
import numpy as np
import cv2 as cv
......@@ -139,7 +139,7 @@ img[res[:,3],res[:,2]] = [0,255,0]
cv.imwrite('subpixel5.png',img)
@endcode
Below is the result, where some important locations are shown in zoomed window to visualize:
Below is the result, where some important locations are shown in the zoomed window to visualize:
![image](images/subpixel3.png)
......
......@@ -250,8 +250,6 @@ CV_IMPL void cvComposeRT( const CvMat* _rvec1, const CvMat* _tvec1,
CV_IMPL int cvRodrigues2( const CvMat* src, CvMat* dst, CvMat* jacobian )
{
int depth, elem_size;
int i, k;
double J[27] = {0};
CvMat matJ = cvMat( 3, 9, CV_64F, J );
......@@ -262,8 +260,8 @@ CV_IMPL int cvRodrigues2( const CvMat* src, CvMat* dst, CvMat* jacobian )
CV_Error( !dst ? CV_StsNullPtr : CV_StsBadArg,
"The first output argument is not a valid matrix" );
depth = CV_MAT_DEPTH(src->type);
elem_size = CV_ELEM_SIZE(depth);
int depth = CV_MAT_DEPTH(src->type);
int elem_size = CV_ELEM_SIZE(depth);
if( depth != CV_32F && depth != CV_64F )
CV_Error( CV_StsUnsupportedFormat, "The matrices must have 32f or 64f data type" );
......@@ -349,12 +347,12 @@ CV_IMPL int cvRodrigues2( const CvMat* src, CvMat* dst, CvMat* jacobian )
double d_r_x_[] = { 0, 0, 0, 0, 0, -1, 0, 1, 0,
0, 0, 1, 0, 0, 0, -1, 0, 0,
0, -1, 0, 1, 0, 0, 0, 0, 0 };
for( i = 0; i < 3; i++ )
for( int i = 0; i < 3; i++ )
{
double ri = i == 0 ? r.x : i == 1 ? r.y : r.z;
double a0 = -s*ri, a1 = (s - 2*c1*itheta)*ri, a2 = c1*itheta;
double a3 = (c - s*itheta)*ri, a4 = s*itheta;
for( k = 0; k < 9; k++ )
for( int k = 0; k < 9; k++ )
J[i*9+k] = a0*I[k] + a1*rrt.val[k] + a2*drrt[i*9+k] +
a3*r_x.val[k] + a4*d_r_x_[i*9+k];
}
......@@ -490,6 +488,10 @@ CV_IMPL int cvRodrigues2( const CvMat* src, CvMat* dst, CvMat* jacobian )
dst->data.db[step*2] = r.z;
}
}
else
{
CV_Error(CV_StsBadSize, "Input matrix must be 1x3 or 3x1 for a rotation vector, or 3x3 for a rotation matrix");
}
if( jacobian )
{
......@@ -3465,6 +3467,12 @@ void cv::Rodrigues(InputArray _src, OutputArray _dst, OutputArray _jacobian)
CV_INSTRUMENT_REGION();
Mat src = _src.getMat();
const Size srcSz = src.size();
CV_Check(srcSz, srcSz == Size(3, 1) || srcSz == Size(1, 3) ||
(srcSz == Size(1, 1) && src.channels() == 3) ||
srcSz == Size(3, 3),
"Input matrix must be 1x3 or 3x1 for a rotation vector, or 3x3 for a rotation matrix");
bool v2m = src.cols == 1 || src.rows == 1;
_dst.create(3, v2m ? 3 : 1, src.depth());
Mat dst = _dst.getMat();
......
......@@ -95,6 +95,7 @@ void CV_ChessboardDetectorBadArgTest::run( int /*start_from */)
initArgs();
pattern_size = Size(2,2);
errors += run_test_case( Error::StsOutOfRange, "Invalid pattern size" );
pattern_size = cbg.cornersSize();
cb.convertTo(img, CV_32F);
......
......@@ -671,7 +671,8 @@ TEST(Core_Check, testMatType_fail_2)
EXPECT_STREQ(e.err.c_str(),
"> Unsupported src:\n"
"> 'src_type == CV_32FC1 || src_type == CV_32FC3'\n"
"> where\n> 'src_type' is 0 (CV_8UC1)\n"
"> where\n"
"> 'src_type' is 0 (CV_8UC1)\n"
);
}
catch (const std::exception& e)
......@@ -737,7 +738,39 @@ TEST(Core_Check, testMatDepth_fail_2)
EXPECT_STREQ(e.err.c_str(),
"> Unsupported src:\n"
"> 'src_depth == CV_32F || src_depth == CV_64F'\n"
"> where\n> 'src_depth' is 0 (CV_8U)\n"
"> where\n"
"> 'src_depth' is 0 (CV_8U)\n"
);
}
catch (const std::exception& e)
{
FAIL() << "Unexpected C++ exception: " << e.what();
}
catch (...)
{
FAIL() << "Unexpected unknown exception";
}
}
void test_check_Size_1(const Size& srcSz)
{
CV_Check(srcSz, srcSz == Size(4, 3), "Unsupported src size");
}
TEST(Core_Check, testSize_1)
{
try
{
test_check_Size_1(Size(2, 1));
FAIL() << "Unreachable code called";
}
catch (const cv::Exception& e)
{
EXPECT_STREQ(e.err.c_str(),
"> Unsupported src size:\n"
"> 'srcSz == Size(4, 3)'\n"
"> where\n"
"> 'srcSz' is [2 x 1]\n"
);
}
catch (const std::exception& e)
......
......@@ -996,7 +996,7 @@ void TFImporter::populateNet(Net dstNet)
if (getDataLayout(name, data_layouts) == DATA_LAYOUT_UNKNOWN)
data_layouts[name] = DATA_LAYOUT_NHWC;
}
else if (type == "BiasAdd" || type == "Add" || type == "Sub" || type=="AddN")
else if (type == "BiasAdd" || type == "Add" || type == "AddV2" || type == "Sub" || type=="AddN")
{
bool haveConst = false;
for(int ii = 0; !haveConst && ii < layer.input_size(); ++ii)
......
......@@ -90,12 +90,12 @@ public:
CV_TRACE_FUNCTION();
DTreesImpl::clear();
oobError = 0.;
rng = RNG((uint64)-1);
}
const vector<int>& getActiveVars() CV_OVERRIDE
{
CV_TRACE_FUNCTION();
RNG &rng = theRNG();
int i, nvars = (int)allVars.size(), m = (int)activeVars.size();
for( i = 0; i < nvars; i++ )
{
......@@ -134,6 +134,7 @@ public:
bool train( const Ptr<TrainData>& trainData, int flags ) CV_OVERRIDE
{
CV_TRACE_FUNCTION();
RNG &rng = theRNG();
CV_Assert(!trainData.empty());
startTraining(trainData, flags);
int treeidx, ntrees = (rparams.termCrit.type & TermCriteria::COUNT) != 0 ?
......@@ -424,7 +425,6 @@ public:
double oobError;
vector<float> varImportance;
vector<int> allVars, activeVars;
RNG rng;
};
......
......@@ -62,7 +62,7 @@ class MultiscaleAnchorGenerator:
def createSSDGraph(modelPath, configPath, outputPath):
# Nodes that should be kept.
keepOps = ['Conv2D', 'BiasAdd', 'Add', 'Relu', 'Relu6', 'Placeholder', 'FusedBatchNorm',
keepOps = ['Conv2D', 'BiasAdd', 'Add', 'AddV2', 'Relu', 'Relu6', 'Placeholder', 'FusedBatchNorm',
'DepthwiseConv2dNative', 'ConcatV2', 'Mul', 'MaxPool', 'AvgPool', 'Identity',
'Sub', 'ResizeNearestNeighbor', 'Pad', 'FusedBatchNormV3']
......@@ -151,6 +151,9 @@ def createSSDGraph(modelPath, configPath, outputPath):
subgraphBatchNorm = ['Add',
['Mul', 'input', ['Mul', ['Rsqrt', ['Add', 'moving_variance', 'add_y']], 'gamma']],
['Sub', 'beta', ['Mul', 'moving_mean', 'Mul_0']]]
subgraphBatchNormV2 = ['AddV2',
['Mul', 'input', ['Mul', ['Rsqrt', ['AddV2', 'moving_variance', 'add_y']], 'gamma']],
['Sub', 'beta', ['Mul', 'moving_mean', 'Mul_0']]]
# Detect unfused nearest neighbor resize.
subgraphResizeNN = ['Reshape',
['Mul', ['Reshape', 'input', ['Pack', 'shape_1', 'shape_2', 'shape_3', 'shape_4', 'shape_5']],
......@@ -177,7 +180,8 @@ def createSSDGraph(modelPath, configPath, outputPath):
for node in graph_def.node:
inputs = {}
fusedNodes = []
if checkSubgraph(node, subgraphBatchNorm, inputs, fusedNodes):
if checkSubgraph(node, subgraphBatchNorm, inputs, fusedNodes) or \
checkSubgraph(node, subgraphBatchNormV2, inputs, fusedNodes):
name = node.name
node.Clear()
node.name = name
......
......@@ -17,6 +17,7 @@
#define CL_USE_DEPRECATED_OPENCL_1_1_APIS
#define CL_USE_DEPRECATED_OPENCL_1_2_APIS
#define CL_USE_DEPRECATED_OPENCL_2_0_APIS // eliminate build warning
#define CL_TARGET_OPENCL_VERSION 200 // 2.0
#ifdef __APPLE__
#define CL_SILENCE_DEPRECATION
......@@ -677,7 +678,7 @@ int App::initVideoSource()
throw std::runtime_error(std::string("specify video source"));
}
catch (const std::exception e)
catch (const std::exception& e)
{
cerr << "ERROR: " << e.what() << std::endl;
return -1;
......
......@@ -26,7 +26,6 @@ import cv2 as cv
import video
from common import anorm2, draw_str
from time import clock
lk_params = dict( winSize = (15, 15),
maxLevel = 2,
......
......@@ -39,9 +39,6 @@ import re
from numpy import pi, sin, cos
# built-in modules
from time import clock
# local modules
from tst_scene_render import TestSceneRender
import common
......
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