1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-25 21:33:04 +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
@@ -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();