Skip to content

Commit

Permalink
Merge branch 4.x
Browse files Browse the repository at this point in the history
  • Loading branch information
alalek committed Apr 9, 2021
2 parents 5af8624 + 9163f86 commit 68492c4
Show file tree
Hide file tree
Showing 278 changed files with 38,652 additions and 1,638 deletions.
2 changes: 1 addition & 1 deletion modules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ $ cmake -D OPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules -D BUILD_opencv_<r

- **julia**: Julia language wrappers with samples and tests.

- **line_descriptor**: Line Segment Extract and Match -- Methods of extracting, describing and latching line segments using binary descriptors.
- **line_descriptor**: Line Segment Extract and Match -- Methods of extracting, describing and matching line segments using binary descriptors.

- **matlab**: Matlab Interface -- OpenCV Matlab Mex wrapper code generator for certain opencv core modules.

Expand Down
2 changes: 1 addition & 1 deletion modules/alphamat/src/cm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void lle(my_vector_of_vectors_t& indm, my_vector_of_vectors_t& samples, float ep
Mat ptDotN(20, 1, DataType<float>::type), imd(20, 1, DataType<float>::type);
Mat Cones(20, 1, DataType<float>::type), Cinv(20, 1, DataType<float>::type);
float alpha, beta, lagrangeMult;
Cones += 1;
Cones = 1;

C = 0;
rhs = 1;
Expand Down
2 changes: 1 addition & 1 deletion modules/aruco/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
set(the_description "ArUco Marker Detection")
ocv_define_module(aruco opencv_core opencv_imgproc opencv_3d opencv_calib WRAP python java objc)
ocv_define_module(aruco opencv_core opencv_imgproc opencv_3d opencv_calib WRAP python java objc js)
45 changes: 24 additions & 21 deletions modules/aruco/src/aruco.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,34 +308,36 @@ static void _filterTooCloseCandidates(const vector< vector< Point2f > > &candida
vector< vector< Point > > smallerContours;

// save possible candidates
for( unsigned int i = 0; i < groupedCandidates.size(); i++ ) {
int smallerIdx = groupedCandidates[i][0];
int biggerIdx = -1;
for(unsigned int i = 0; i < groupedCandidates.size(); i++) {
unsigned int smallerIdx = groupedCandidates[i][0];
unsigned int biggerIdx = smallerIdx;
double smallerArea = contourArea(candidatesIn[smallerIdx]);
double biggerArea = smallerArea;

// evaluate group elements
for( unsigned int j = 1; j < groupedCandidates[i].size(); j++ ) {
size_t currPerim = contoursIn[ groupedCandidates[i][j] ].size();
for(unsigned int j = 1; j < groupedCandidates[i].size(); j++) {
unsigned int currIdx = groupedCandidates[i][j];
double currArea = contourArea(candidatesIn[currIdx]);

// check if current contour is bigger
if ( biggerIdx < 0 )
biggerIdx = groupedCandidates[i][j];
else if(currPerim >= contoursIn[ biggerIdx ].size())
biggerIdx = groupedCandidates[i][j];
if(currArea >= biggerArea) {
biggerIdx = currIdx;
biggerArea = currArea;
}

// check if current contour is smaller
if(currPerim < contoursIn[ smallerIdx ].size() && detectInvertedMarker)
smallerIdx = groupedCandidates[i][j];
if(currArea < smallerArea && detectInvertedMarker) {
smallerIdx = currIdx;
smallerArea = currArea;
}
}
// add contours und candidates
if(biggerIdx > -1){

biggerCandidates.push_back(candidatesIn[biggerIdx]);
biggerContours.push_back(contoursIn[biggerIdx]);

if( detectInvertedMarker ){
smallerCandidates.push_back(alignContourOrder(candidatesIn[biggerIdx][0], candidatesIn[smallerIdx]));
smallerContours.push_back(contoursIn[smallerIdx]);
}
// add contours and candidates
biggerCandidates.push_back(candidatesIn[biggerIdx]);
biggerContours.push_back(contoursIn[biggerIdx]);
if(detectInvertedMarker) {
smallerCandidates.push_back(alignContourOrder(candidatesIn[biggerIdx][0], candidatesIn[smallerIdx]));
smallerContours.push_back(contoursIn[smallerIdx]);
}
}
// to preserve the structure :: candidateSet< defaultCandidates, whiteCandidates >
Expand Down Expand Up @@ -564,7 +566,7 @@ static uint8_t _identifyOneCandidate(const Ptr<Dictionary>& dictionary, InputArr
Mat onlyBits =
candidateBits.rowRange(params->markerBorderBits,
candidateBits.rows - params->markerBorderBits)
.colRange(params->markerBorderBits, candidateBits.rows - params->markerBorderBits);
.colRange(params->markerBorderBits, candidateBits.cols - params->markerBorderBits);

// try to indentify the marker
if(!dictionary->identify(onlyBits, idx, rotation, params->errorCorrectionRate))
Expand Down Expand Up @@ -806,6 +808,7 @@ static void _refineCandidateLines(std::vector<Point>& nContours, std::vector<Poi

// saves extra group into corresponding
if( !cntPts[4].empty() ){
CV_CheckLT(group, 4, "FIXIT: avoiding infinite loop: implementation should be revised: https://github.com/opencv/opencv_contrib/issues/2738");
for( unsigned int i=0; i < cntPts[4].size() ; i++ )
cntPts[group].push_back(cntPts[4].at(i));
cntPts[4].clear();
Expand Down
4 changes: 2 additions & 2 deletions modules/aruco/src/charuco.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ static int _selectAndRefineChessboardCorners(InputArray _allCorners, InputArray
if(_image.type() == CV_8UC3)
cvtColor(_image, grey, COLOR_BGR2GRAY);
else
_image.copyTo(grey);
grey = _image.getMat();

const Ptr<DetectorParameters> params = DetectorParameters::create(); // use default params for corner refinement

Expand Down Expand Up @@ -725,7 +725,7 @@ void detectCharucoDiamond(InputArray _image, InputArrayOfArrays _markerCorners,
if(_image.type() == CV_8UC3)
cvtColor(_image, grey, COLOR_BGR2GRAY);
else
_image.copyTo(grey);
grey = _image.getMat();

// for each of the detected markers, try to find a diamond
for(unsigned int i = 0; i < _markerIds.total(); i++) {
Expand Down
4 changes: 2 additions & 2 deletions modules/ccalib/src/omnidir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ void cv::omnidir::undistortPoints( InputArray distorted, OutputArray undistorted
Vec3d Xs = Xw / cv::norm(Xw);

// reproject to camera plane
Vec3d ppu = Vec3d(Xs[0]/(Xs[2]+_xi), Xs[1]/(Xs[2]+_xi), 1.0);
Vec3d ppu = Vec3d(Xs[0]/Xs[2], Xs[1]/Xs[2], 1.0);
if (undistorted.depth() == CV_32F)
{
dstf[i] = Vec2f((float)ppu[0], (float)ppu[1]);
Expand Down Expand Up @@ -2223,7 +2223,7 @@ void cv::omnidir::stereoRectify(InputArray R, InputArray T, OutputArray R1, Outp
e1.copyTo(_R1.row(0));
e2.copyTo(_R1.row(1));
e3.copyTo(_R1.row(2));
_R2 = R21 * _R1;
_R2 = _R1 * R21;

}

Expand Down
25 changes: 25 additions & 0 deletions modules/cudaarithm/include/opencv2/cudaarithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,31 @@ threshold types are not supported.
*/
CV_EXPORTS_W double threshold(InputArray src, OutputArray dst, double thresh, double maxval, int type, Stream& stream = Stream::Null());

/** @brief Checks if array elements lie between two scalars.
The function checks the range as follows:
- For every element of a single-channel input array:
\f[\texttt{dst} (I)= \texttt{lowerb}_0 \leq \texttt{src} (I)_0 \leq \texttt{upperb}_0\f]
- For two-channel arrays:
\f[\texttt{dst} (I)= \texttt{lowerb}_0 \leq \texttt{src} (I)_0 \leq \texttt{upperb}_0 \land \texttt{lowerb}_1 \leq \texttt{src} (I)_1 \leq \texttt{upperb}_1\f]
- and so forth.
That is, dst (I) is set to 255 (all 1 -bits) if src (I) is within the
specified 1D, 2D, 3D, ... box and 0 otherwise.
Note that unlike the CPU inRange, this does NOT accept an array for lowerb or
upperb, only a cv::Scalar.
@param src first input array.
@param lowerb inclusive lower boundary cv::Scalar.
@param upperb inclusive upper boundary cv::Scalar.
@param dst output array of the same size as src and CV_8U type.
@param stream Stream for the asynchronous version.
@sa cv::inRange
*/
CV_EXPORTS_W void inRange(InputArray src, const Scalar& lowerb, const Scalar& upperb, OutputArray dst, Stream& stream = Stream::Null());

/** @brief Computes magnitudes of complex matrix elements.
@param xy Source complex matrix in the interleaved format ( CV_32FC2 ).
Expand Down
21 changes: 20 additions & 1 deletion modules/cudaarithm/misc/python/test/test_cudaarithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,24 @@ def test_convolution(self):
self.assertTrue(np.allclose(cuMatDst.download(),
cv.filter2D(npMat,-1,kernel,anchor=(-1,-1))[iS[0]:iE[0]+1,iS[1]:iE[1]+1]))

def test_inrange(self):
npMat = (np.random.random((128, 128, 3)) * 255).astype(np.float32)

bound1 = np.random.random((4,)) * 255
bound2 = np.random.random((4,)) * 255
lowerb = np.minimum(bound1, bound2).tolist()
upperb = np.maximum(bound1, bound2).tolist()

cuMat = cv.cuda_GpuMat()
cuMat.upload(npMat)

self.assertTrue((cv.cuda.inRange(cuMat, lowerb, upperb).download() ==
cv.inRange(npMat, np.array(lowerb), np.array(upperb))).all())

cuMatDst = cv.cuda_GpuMat(cuMat.size(), cv.CV_8UC1)
cv.cuda.inRange(cuMat, lowerb, upperb, cuMatDst)
self.assertTrue((cuMatDst.download() ==
cv.inRange(npMat, np.array(lowerb), np.array(upperb))).all())

if __name__ == '__main__':
NewOpenCVTests.bootstrap()
NewOpenCVTests.bootstrap()
37 changes: 37 additions & 0 deletions modules/cudaarithm/perf/perf_element_operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1501,4 +1501,41 @@ PERF_TEST_P(Sz_Depth_Op, Threshold,
}
}

//////////////////////////////////////////////////////////////////////
// InRange

PERF_TEST_P(Sz_Depth_Cn, InRange,
Combine(CUDA_TYPICAL_MAT_SIZES,
Values(CV_8U, CV_16U, CV_32F, CV_64F),
CUDA_CHANNELS_1_3_4))
{
const cv::Size size = GET_PARAM(0);
const int depth = GET_PARAM(1);
const int channels = GET_PARAM(2);

cv::Mat src(size, CV_MAKE_TYPE(depth, channels));
declare.in(src, WARMUP_RNG);

const cv::Scalar lowerb(10, 50, 100);
const cv::Scalar upperb(70, 85, 200);

if (PERF_RUN_CUDA())
{
const cv::cuda::GpuMat d_src(src);
cv::cuda::GpuMat dst;

TEST_CYCLE() cv::cuda::inRange(d_src, lowerb, upperb, dst);

CUDA_SANITY_CHECK(dst, 0);
}
else
{
cv::Mat dst;

TEST_CYCLE() cv::inRange(src, lowerb, upperb, dst);

CPU_SANITY_CHECK(dst);
}
}

}} // namespace
2 changes: 1 addition & 1 deletion modules/cudaarithm/src/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ void cv::cuda::flip(InputArray _src, OutputArray _dst, int flipCode, Stream& str

_dst.create(src.size(), src.type());
GpuMat dst = getOutputMat(_dst, src.size(), src.type(), stream);
bool isInplace = (src.data == dst.data) || (src.refcount == dst.refcount);
bool isInplace = (src.data == dst.data);
bool isSizeOdd = (src.cols & 1) == 1 || (src.rows & 1) == 1;
if (isInplace && isSizeOdd)
CV_Error(Error::BadROISize, "In-place version of flip only accepts even width/height");
Expand Down
99 changes: 99 additions & 0 deletions modules/cudaarithm/src/cuda/in_range.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// 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.

#include "opencv2/opencv_modules.hpp"

#ifndef HAVE_OPENCV_CUDEV

#error "opencv_cudev is required"

#else

#include "opencv2/core/private.cuda.hpp"
#include "opencv2/cudaarithm.hpp"
#include "opencv2/cudev.hpp"

using namespace cv;
using namespace cv::cuda;
using namespace cv::cudev;

namespace {

template <typename T, int cn>
void inRangeImpl(const GpuMat& src,
const Scalar& lowerb,
const Scalar& upperb,
GpuMat& dst,
Stream& stream) {
gridTransformUnary(globPtr<typename MakeVec<T, cn>::type>(src),
globPtr<uchar>(dst),
InRangeFunc<T, cn>(lowerb, upperb),
stream);
}

} // namespace

void cv::cuda::inRange(InputArray _src,
const Scalar& _lowerb,
const Scalar& _upperb,
OutputArray _dst,
Stream& stream) {
const GpuMat src = getInputMat(_src, stream);

typedef void (*func_t)(const GpuMat& src,
const Scalar& lowerb,
const Scalar& upperb,
GpuMat& dst,
Stream& stream);

// Note: We cannot support 16F with the current implementation because we
// use a CUDA vector (e.g. int3) to store the bounds, and there is no CUDA
// vector type for float16
static constexpr const int MAX_CHANNELS = 4;
static constexpr const int NUM_DEPTHS = CV_64F + 1;

static const std::array<std::array<func_t, NUM_DEPTHS>, MAX_CHANNELS>
funcs = {std::array<func_t, NUM_DEPTHS>{inRangeImpl<uchar, 1>,
inRangeImpl<schar, 1>,
inRangeImpl<ushort, 1>,
inRangeImpl<short, 1>,
inRangeImpl<int, 1>,
inRangeImpl<float, 1>,
inRangeImpl<double, 1>},
std::array<func_t, NUM_DEPTHS>{inRangeImpl<uchar, 2>,
inRangeImpl<schar, 2>,
inRangeImpl<ushort, 2>,
inRangeImpl<short, 2>,
inRangeImpl<int, 2>,
inRangeImpl<float, 2>,
inRangeImpl<double, 2>},
std::array<func_t, NUM_DEPTHS>{inRangeImpl<uchar, 3>,
inRangeImpl<schar, 3>,
inRangeImpl<ushort, 3>,
inRangeImpl<short, 3>,
inRangeImpl<int, 3>,
inRangeImpl<float, 3>,
inRangeImpl<double, 3>},
std::array<func_t, NUM_DEPTHS>{inRangeImpl<uchar, 4>,
inRangeImpl<schar, 4>,
inRangeImpl<ushort, 4>,
inRangeImpl<short, 4>,
inRangeImpl<int, 4>,
inRangeImpl<float, 4>,
inRangeImpl<double, 4>}};

CV_CheckLE(src.channels(), MAX_CHANNELS, "Src must have <= 4 channels");
CV_CheckLE(src.depth(),
CV_64F,
"Src must have depth 8U, 8S, 16U, 16S, 32S, 32F, or 64F");

GpuMat dst = getOutputMat(_dst, src.size(), CV_8UC1, stream);

const func_t func = funcs.at(src.channels() - 1).at(src.depth());
func(src, _lowerb, _upperb, dst, stream);

syncOutput(dst, _dst, stream);
}

#endif
2 changes: 2 additions & 0 deletions modules/cudaarithm/src/element_operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ void cv::cuda::addWeighted(InputArray, double, InputArray, double, double, Outpu

double cv::cuda::threshold(InputArray, OutputArray, double, double, int, Stream&) {throw_no_cuda(); return 0.0;}

void cv::cuda::inRange(InputArray, const Scalar&, const Scalar&, OutputArray, Stream&) { throw_no_cuda(); }

void cv::cuda::magnitude(InputArray, OutputArray, Stream&) { throw_no_cuda(); }
void cv::cuda::magnitude(InputArray, InputArray, OutputArray, Stream&) { throw_no_cuda(); }
void cv::cuda::magnitudeSqr(InputArray, OutputArray, Stream&) { throw_no_cuda(); }
Expand Down
Loading

0 comments on commit 68492c4

Please sign in to comment.