1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-29 07:13:02 +04:00

Merge branch 4.x

This commit is contained in:
Alexander Alekhin
2021-04-09 10:30:38 +00:00
1114 changed files with 64039 additions and 14611 deletions
+20 -2
View File
@@ -25,6 +25,9 @@ if(NOT BUILD_EXAMPLES OR NOT OCV_DEPENDENCIES_FOUND)
return()
endif()
set(DEPS_example_snippet_imgproc_segmentation opencv_core opencv_imgproc)
set(DEPS_example_cpp_intelligent_scissors opencv_core opencv_imgproc opencv_imgcodecs opencv_highgui)
project(cpp_samples)
ocv_include_modules_recurse(${OPENCV_CPP_SAMPLES_REQUIRED_DEPS})
file(GLOB_RECURSE cpp_samples RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)
@@ -32,13 +35,20 @@ if(NOT HAVE_opencv_cudaarithm OR NOT HAVE_opencv_cudafilters)
ocv_list_filterout(cpp_samples "/gpu/")
endif()
ocv_list_filterout(cpp_samples "real_time_pose_estimation/")
ocv_list_filterout(cpp_samples "parallel_backend/")
foreach(sample_filename ${cpp_samples})
set(package "cpp")
if(sample_filename MATCHES "tutorial_code")
if(sample_filename MATCHES "tutorial_code/snippet")
set(package "snippet")
elseif(sample_filename MATCHES "tutorial_code")
set(package "tutorial")
endif()
ocv_define_sample(tgt ${sample_filename} ${package})
ocv_target_link_libraries(${tgt} PRIVATE ${OPENCV_LINKER_LIBS} ${OPENCV_CPP_SAMPLES_REQUIRED_DEPS})
set(deps ${OPENCV_CPP_SAMPLES_REQUIRED_DEPS})
if(DEFINED DEPS_${tgt})
set(deps ${DEPS_${tgt}})
endif()
ocv_target_link_libraries(${tgt} PRIVATE ${OPENCV_LINKER_LIBS} ${deps})
if(sample_filename MATCHES "/gpu/" AND HAVE_opencv_cudaarithm AND HAVE_opencv_cuda_filters)
ocv_target_link_libraries(${tgt} PRIVATE opencv_cudaarithm opencv_cudafilters)
endif()
@@ -59,3 +69,11 @@ foreach(sample_filename ${cpp_samples})
endforeach()
include("tutorial_code/calib3d/real_time_pose_estimation/CMakeLists.txt" OPTIONAL)
# Standalone samples only
if(OpenCV_FOUND AND NOT CMAKE_VERSION VERSION_LESS "3.1")
add_subdirectory("example_cmake")
endif()
if(OpenCV_FOUND AND NOT CMAKE_VERSION VERSION_LESS "3.9")
add_subdirectory("tutorial_code/core/parallel_backend")
endif()
+2 -2
View File
@@ -81,7 +81,7 @@ static void getPlanes (InputArray points3d_, std::vector<int> &labels, std::vect
double xx = 0, yy = 0, zz = 0, xy = 0, xz = 0, yz = 0;
for (int s : sample) {
const double x_ = points[3*s] - c_x, y_ = points[3*s+1] - c_y, z_ = points[3*s+2] - c_z;
xx += x_*x_; yy += y_*y_; zz += z_*z_; xy = x_*y_; yz += y_*z_; xz += x_*z_;
xx += x_*x_; yy += y_*y_; zz += z_*z_; xy += x_*y_; yz += y_*z_; xz += x_*z_;
}
xx /= n; yy /= n; zz /= n; xy /= n; yz /= n; xz /= n;
Vec3d weighted_normal(0,0,0);
@@ -340,4 +340,4 @@ int main(int args, char** argv) {
imshow("image 1-2", image1);
imwrite("planes.png", image1);
waitKey(0);
}
}
+2 -57
View File
@@ -1,60 +1,5 @@
/*//////////////////////////////////////////////////////////////////////////////////////
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
// This is a implementation of the Logistic Regression algorithm in C++ in OpenCV.
// AUTHOR:
// Rahul Kavi rahulkavi[at]live[at]com
//
// contains a subset of data from the popular Iris Dataset (taken from
// "http://archive.ics.uci.edu/ml/datasets/Iris")
// # You are free to use, change, or redistribute the code in any way you wish for
// # non-commercial purposes, but please maintain the name of the original author.
// # This code comes with no warranty of any kind.
// #
// # You are free to use, change, or redistribute the code in any way you wish for
// # non-commercial purposes, but please maintain the name of the original author.
// # This code comes with no warranty of any kind.
// # Logistic Regression ALGORITHM
// License Agreement
// For Open Source Computer Vision Library
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2008-2011, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.*/
// Logistic Regression sample
// AUTHOR: Rahul Kavi rahulkavi[at]live[at]com
#include <iostream>
-1
View File
@@ -17,7 +17,6 @@
OPENCV WEBSITES:
Homepage: http://opencv.org
Online docs: http://docs.opencv.org
Q&A forum: http://answers.opencv.org
GitHub: https://github.com/opencv/opencv/
************************************************** */
@@ -24,23 +24,16 @@ int main(int argc, char *argv[])
return -1;
}
// Show source image
// Show the source image
imshow("Source Image", src);
//! [load_image]
//! [black_bg]
// Change the background from white to black, since that will help later to extract
// better results during the use of Distance Transform
for ( int i = 0; i < src.rows; i++ ) {
for ( int j = 0; j < src.cols; j++ ) {
if ( src.at<Vec3b>(i, j) == Vec3b(255,255,255) )
{
src.at<Vec3b>(i, j)[0] = 0;
src.at<Vec3b>(i, j)[1] = 0;
src.at<Vec3b>(i, j)[2] = 0;
}
}
}
Mat mask;
inRange(src, Scalar(255, 255, 255), Scalar(255, 255, 255), mask);
src.setTo(Scalar(0, 0, 0), mask);
// Show output image
imshow("Black Background Image", src);
@@ -124,7 +117,9 @@ int main(int argc, char *argv[])
// Draw the background marker
circle(markers, Point(5,5), 3, Scalar(255), -1);
imshow("Markers", markers*10000);
Mat markers8u;
markers.convertTo(markers8u, CV_8U, 10);
imshow("Markers", markers8u);
//! [seeds]
//! [watershed]
@@ -419,7 +419,12 @@ int main(int argc, char* argv[])
{
Mat temp = view.clone();
if (s.useFisheye)
cv::fisheye::undistortImage(temp, view, cameraMatrix, distCoeffs);
{
Mat newCamMat;
fisheye::estimateNewCameraMatrixForUndistortRectify(cameraMatrix, distCoeffs, imageSize,
Matx33d::eye(), newCamMat, 1);
cv::fisheye::undistortImage(temp, view, cameraMatrix, distCoeffs, newCamMat);
}
else
undistort(temp, view, cameraMatrix, distCoeffs);
}
@@ -445,7 +450,7 @@ int main(int argc, char* argv[])
// -----------------------Show the undistorted image for the image list ------------------------
//! [show_results]
if( s.inputType == Settings::IMAGE_LIST && s.showUndistorsed )
if( s.inputType == Settings::IMAGE_LIST && s.showUndistorsed && !cameraMatrix.empty())
{
Mat view, rview, map1, map2;
@@ -548,7 +553,7 @@ static bool runCalibration( Settings& s, Size& imageSize, Mat& cameraMatrix, Mat
{
//! [fixed_aspect]
cameraMatrix = Mat::eye(3, 3, CV_64F);
if( s.flag & CALIB_FIX_ASPECT_RATIO )
if( !s.useFisheye && s.flag & CALIB_FIX_ASPECT_RATIO )
cameraMatrix.at<double>(0,0) = s.aspectRatio;
//! [fixed_aspect]
if (s.useFisheye) {
@@ -631,7 +636,7 @@ static void saveCameraParams( Settings& s, Size& imageSize, Mat& cameraMatrix, M
fs << "board_height" << s.boardSize.height;
fs << "square_size" << s.squareSize;
if( s.flag & CALIB_FIX_ASPECT_RATIO )
if( !s.useFisheye && s.flag & CALIB_FIX_ASPECT_RATIO )
fs << "fix_aspect_ratio" << s.aspectRatio;
if (s.flag)
@@ -0,0 +1,26 @@
cmake_minimum_required(VERSION 3.9)
find_package(OpenCV REQUIRED COMPONENTS opencv_core)
find_package(OpenMP)
if(OpenMP_FOUND)
project(opencv_example_openmp_backend)
add_executable(opencv_example_openmp_backend example-openmp.cpp)
target_link_libraries(opencv_example_openmp_backend PRIVATE
opencv_core
OpenMP::OpenMP_CXX
)
endif()
# TODO: find_package(TBB)
find_path(TBB_INCLUDE_DIR NAMES "tbb/tbb.h")
find_library(TBB_LIBRARY NAMES "tbb")
if(TBB_INCLUDE_DIR AND TBB_LIBRARY AND NOT OPENCV_EXAMPLE_SKIP_TBB)
project(opencv_example_tbb_backend)
add_executable(opencv_example_tbb_backend example-tbb.cpp)
target_include_directories(opencv_example_tbb_backend SYSTEM PRIVATE ${TBB_INCLUDE_DIR})
target_link_libraries(opencv_example_tbb_backend PRIVATE
opencv_core
${TBB_LIBRARY}
)
endif()
@@ -0,0 +1,44 @@
#include "opencv2/core.hpp"
#include <iostream>
#include <chrono>
#include <thread>
//! [openmp_include]
#include "opencv2/core/parallel/backend/parallel_for.openmp.hpp"
//! [openmp_include]
namespace cv { // private.hpp
CV_EXPORTS const char* currentParallelFramework();
}
static
std::string currentParallelFrameworkSafe()
{
const char* framework = cv::currentParallelFramework();
if (framework)
return framework;
return std::string();
}
using namespace cv;
int main()
{
std::cout << "OpenCV builtin parallel framework: '" << currentParallelFrameworkSafe() << "' (nthreads=" << getNumThreads() << ")" << std::endl;
//! [openmp_backend]
//omp_set_dynamic(1);
cv::parallel::setParallelForBackend(std::make_shared<cv::parallel::openmp::ParallelForBackend>());
//! [openmp_backend]
std::cout << "New parallel backend: '" << currentParallelFrameworkSafe() << "'" << "' (nthreads=" << getNumThreads() << ")" << std::endl;
parallel_for_(Range(0, 20), [&](const Range range)
{
std::ostringstream out;
out << "Thread " << getThreadNum() << "(opencv=" << utils::getThreadID() << "): range " << range.start << "-" << range.end << std::endl;
std::cout << out.str() << std::flush;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
});
}
@@ -0,0 +1,43 @@
#include "opencv2/core.hpp"
#include <iostream>
#include <chrono>
#include <thread>
//! [tbb_include]
#include "opencv2/core/parallel/backend/parallel_for.tbb.hpp"
//! [tbb_include]
namespace cv { // private.hpp
CV_EXPORTS const char* currentParallelFramework();
}
static
std::string currentParallelFrameworkSafe()
{
const char* framework = cv::currentParallelFramework();
if (framework)
return framework;
return std::string();
}
using namespace cv;
int main()
{
std::cout << "OpenCV builtin parallel framework: '" << currentParallelFrameworkSafe() << "' (nthreads=" << getNumThreads() << ")" << std::endl;
//! [tbb_backend]
cv::parallel::setParallelForBackend(std::make_shared<cv::parallel::tbb::ParallelForBackend>());
//! [tbb_backend]
std::cout << "New parallel backend: '" << currentParallelFrameworkSafe() << "'" << "' (nthreads=" << getNumThreads() << ")" << std::endl;
parallel_for_(Range(0, 20), [&](const Range range)
{
std::ostringstream out;
out << "Thread " << getThreadNum() << "(opencv=" << utils::getThreadID() << "): range " << range.start << "-" << range.end << std::endl;
std::cout << out.str() << std::flush;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
});
}
@@ -0,0 +1,35 @@
#include "opencv2/imgproc.hpp"
#include "opencv2/imgproc/segmentation.hpp"
using namespace cv;
static
void usage_example_intelligent_scissors()
{
Mat image(Size(1920, 1080), CV_8UC3, Scalar::all(128));
//! [usage_example_intelligent_scissors]
segmentation::IntelligentScissorsMB tool;
tool.setEdgeFeatureCannyParameters(16, 100) // using Canny() as edge feature extractor
.setGradientMagnitudeMaxLimit(200);
// calculate image features
tool.applyImage(image);
// calculate map for specified source point
Point source_point(200, 100);
tool.buildMap(source_point);
// fast fetching of contours
// for specified target point and the pre-calculated map (stored internally)
Point target_point(400, 300);
std::vector<Point> pts;
tool.getContour(target_point, pts);
//! [usage_example_intelligent_scissors]
}
int main()
{
usage_example_intelligent_scissors();
return 0;
}
@@ -25,10 +25,10 @@ struct Frame
int main()
{
//! [Open streams]
// Open color stream
VideoCapture colorStream(CAP_V4L2);
// Open depth stream
VideoCapture depthStream(CAP_OPENNI2_ASTRA);
// Open color stream
VideoCapture colorStream(0, CAP_V4L2);
//! [Open streams]
// Check that stream has opened
@@ -69,7 +69,6 @@ int main()
//! [Read streams]
// Create two lists to store frames
std::list<Frame> depthFrames, colorFrames;
std::mutex depthFramesMtx, colorFramesMtx;
const std::size_t maxFrames = 64;
// Synchronization objects
@@ -90,8 +89,6 @@ int main()
Frame f;
f.timestamp = cv::getTickCount();
depthStream.retrieve(f.frame, CAP_OPENNI_DEPTH_MAP);
//depthStream.retrieve(f.frame, CAP_OPENNI_DISPARITY_MAP);
//depthStream.retrieve(f.frame, CAP_OPENNI_IR_IMAGE);
if (f.frame.empty())
{
cerr << "ERROR: Failed to decode frame from depth stream" << endl;
@@ -99,7 +96,7 @@ int main()
}
{
std::lock_guard<std::mutex> lk(depthFramesMtx);
std::lock_guard<std::mutex> lk(mtx);
if (depthFrames.size() >= maxFrames)
depthFrames.pop_front();
depthFrames.push_back(f);
@@ -127,7 +124,7 @@ int main()
}
{
std::lock_guard<std::mutex> lk(colorFramesMtx);
std::lock_guard<std::mutex> lk(mtx);
if (colorFrames.size() >= maxFrames)
colorFrames.pop_front();
colorFrames.push_back(f);
@@ -138,56 +135,66 @@ int main()
});
//! [Read streams]
while (true)
//! [Pair frames]
// Pair depth and color frames
while (!isFinish)
{
std::unique_lock<std::mutex> lk(mtx);
while (depthFrames.empty() && colorFrames.empty())
while (!isFinish && (depthFrames.empty() || colorFrames.empty()))
dataReady.wait(lk);
depthFramesMtx.lock();
if (depthFrames.empty())
while (!depthFrames.empty() && !colorFrames.empty())
{
depthFramesMtx.unlock();
}
else
{
// Get a frame from the list
Mat depthMap = depthFrames.front().frame;
depthFrames.pop_front();
depthFramesMtx.unlock();
if (!lk.owns_lock())
lk.lock();
// Get a frame from the list
Frame depthFrame = depthFrames.front();
int64 depthT = depthFrame.timestamp;
// Get a frame from the list
Frame colorFrame = colorFrames.front();
int64 colorT = colorFrame.timestamp;
// Half of frame period is a maximum time diff between frames
const int64 maxTdiff = int64(1000000000 / (2 * colorStream.get(CAP_PROP_FPS)));
if (depthT + maxTdiff < colorT)
{
depthFrames.pop_front();
continue;
}
else if (colorT + maxTdiff < depthT)
{
colorFrames.pop_front();
continue;
}
depthFrames.pop_front();
colorFrames.pop_front();
lk.unlock();
//! [Show frames]
// Show depth frame
Mat d8, dColor;
depthMap.convertTo(d8, CV_8U, 255.0 / 2500);
depthFrame.frame.convertTo(d8, CV_8U, 255.0 / 2500);
applyColorMap(d8, dColor, COLORMAP_OCEAN);
imshow("Depth (colored)", dColor);
}
//! [Show color frame]
colorFramesMtx.lock();
if (colorFrames.empty())
{
colorFramesMtx.unlock();
}
else
{
// Get a frame from the list
Mat colorFrame = colorFrames.front().frame;
colorFrames.pop_front();
colorFramesMtx.unlock();
// Show color frame
imshow("Color", colorFrame);
imshow("Color", colorFrame.frame);
//! [Show frames]
// Exit on Esc key press
int key = waitKey(1);
if (key == 27) // ESC
{
isFinish = true;
break;
}
}
//! [Show color frame]
// Exit on Esc key press
int key = waitKey(1);
if (key == 27) // ESC
break;
}
//! [Pair frames]
isFinish = true;
dataReady.notify_one();
depthReader.join();
colorReader.join();
@@ -223,7 +223,7 @@ inline Ptr<VideoWriter> createWriter(const string &backend, const string &file_n
}
else if (backend == "ffmpeg")
{
cout << "Created FFMpeg writer ( " << file_name << ", FPS=" << fps << ", Size=" << sz << " )" << endl;
cout << "Created FFmpeg writer ( " << file_name << ", FPS=" << fps << ", Size=" << sz << " )" << endl;
return makePtr<VideoWriter>(file_name, CAP_FFMPEG, getValue(fourccByCodec(), codec, "Invalid codec"), fps, sz, true);
}
return Ptr<VideoWriter>();