mirror of
https://github.com/opencv/opencv.git
synced 2026-07-31 08:13:04 +04:00
Merge remote-tracking branch 'upstream/3.4' into merge-3.4
This commit is contained in:
@@ -39,4 +39,4 @@ message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
|
||||
add_executable(${EXAMPLE_NAME} "${EXAMPLE_FILE}")
|
||||
|
||||
# Link your application with OpenCV libraries
|
||||
target_link_libraries(${EXAMPLE_NAME} ${OpenCV_LIBS})
|
||||
target_link_libraries(${EXAMPLE_NAME} LINK_PRIVATE ${OpenCV_LIBS})
|
||||
|
||||
+19
-29
@@ -1,32 +1,3 @@
|
||||
# Utility function: adds sample executable target with name "example_<group>_<file_name>"
|
||||
# Usage:
|
||||
# ocv_define_sample(<output target> <relative filename> <group>)
|
||||
function(ocv_define_sample out_target source sub)
|
||||
get_filename_component(name "${source}" NAME_WE)
|
||||
set(the_target "example_${sub}_${name}")
|
||||
add_executable(${the_target} "${source}")
|
||||
set_target_properties(${the_target} PROPERTIES PROJECT_LABEL "(sample) ${name}")
|
||||
if(ENABLE_SOLUTION_FOLDERS)
|
||||
set_target_properties(${the_target} PROPERTIES FOLDER "samples/${sub}")
|
||||
endif()
|
||||
if(WIN32 AND MSVC AND NOT BUILD_SHARED_LIBS)
|
||||
set_target_properties(${the_target} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:atlthunk.lib /NODEFAULTLIB:atlsd.lib /DEBUG")
|
||||
endif()
|
||||
if(WIN32)
|
||||
install(TARGETS ${the_target} RUNTIME DESTINATION "samples/${sub}" COMPONENT samples)
|
||||
endif()
|
||||
# Add single target to build all samples in the group: 'make opencv_samples_cpp'
|
||||
set(parent_target opencv_samples_${sub})
|
||||
if(NOT TARGET ${parent_target})
|
||||
add_custom_target(${parent_target})
|
||||
if(TARGET opencv_samples)
|
||||
add_dependencies(opencv_samples ${parent_target})
|
||||
endif()
|
||||
endif()
|
||||
add_dependencies(${parent_target} ${the_target})
|
||||
set(${out_target} ${the_target} PARENT_SCOPE)
|
||||
endfunction()
|
||||
|
||||
if(NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_LIST_DIR)
|
||||
#===================================================================================================
|
||||
#
|
||||
@@ -34,6 +5,8 @@ if(NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_LIST_DIR)
|
||||
#
|
||||
#===================================================================================================
|
||||
|
||||
include("${CMAKE_CURRENT_LIST_DIR}/samples_utils.cmake")
|
||||
|
||||
function(ocv_install_example_src relpath)
|
||||
if(INSTALL_C_EXAMPLES)
|
||||
file(GLOB files ${ARGN})
|
||||
@@ -43,6 +16,10 @@ function(ocv_install_example_src relpath)
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
if((TARGET Threads::Threads OR HAVE_PTHREAD OR MSVC OR APPLE) AND NOT OPENCV_EXAMPLES_DISABLE_THREADS)
|
||||
add_definitions(-DHAVE_THREADS=1)
|
||||
endif()
|
||||
|
||||
add_subdirectory(cpp)
|
||||
add_subdirectory(java/tutorial_code)
|
||||
add_subdirectory(dnn)
|
||||
@@ -98,6 +75,8 @@ option(BUILD_EXAMPLES "Build samples" ON)
|
||||
# │ ├── cpp/
|
||||
find_package(OpenCV REQUIRED PATHS "..")
|
||||
|
||||
include("${CMAKE_CURRENT_LIST_DIR}/samples_utils.cmake")
|
||||
|
||||
function(ocv_install_example_src)
|
||||
# not used in this branch
|
||||
endfunction()
|
||||
@@ -129,6 +108,17 @@ endif()
|
||||
|
||||
add_definitions(-DDISABLE_OPENCV_24_COMPATIBILITY=1) # Avoid C-like legacy API
|
||||
|
||||
if(OPENCV_EXAMPLES_DISABLE_THREADS)
|
||||
# nothing
|
||||
elseif(MSVC OR APPLE)
|
||||
set(HAVE_THREADS 1)
|
||||
else()
|
||||
find_package(Threads)
|
||||
endif()
|
||||
if((TARGET Threads::Threads OR HAVE_THREADS) AND NOT OPENCV_EXAMPLES_DISABLE_THREADS)
|
||||
add_definitions(-DHAVE_THREADS=1)
|
||||
endif()
|
||||
|
||||
add_subdirectory(cpp)
|
||||
if(WIN32)
|
||||
add_subdirectory(directx)
|
||||
|
||||
@@ -35,12 +35,12 @@ foreach(sample_filename ${cpp_samples})
|
||||
set(package "tutorial")
|
||||
endif()
|
||||
ocv_define_sample(tgt ${sample_filename} ${package})
|
||||
ocv_target_link_libraries(${tgt} ${OPENCV_LINKER_LIBS} ${OPENCV_CPP_SAMPLES_REQUIRED_DEPS})
|
||||
ocv_target_link_libraries(${tgt} LINK_PRIVATE ${OPENCV_LINKER_LIBS} ${OPENCV_CPP_SAMPLES_REQUIRED_DEPS})
|
||||
if(sample_filename MATCHES "/gpu/" AND HAVE_opencv_cudaarithm AND HAVE_opencv_cuda_filters)
|
||||
ocv_target_link_libraries(${tgt} opencv_cudaarithm opencv_cudafilters)
|
||||
ocv_target_link_libraries(${tgt} LINK_PRIVATE opencv_cudaarithm opencv_cudafilters)
|
||||
endif()
|
||||
if(sample_filename MATCHES "/viz/")
|
||||
ocv_target_link_libraries(${tgt} ${VTK_LIBRARIES})
|
||||
ocv_target_link_libraries(${tgt} LINK_PRIVATE ${VTK_LIBRARIES})
|
||||
target_compile_definitions(${tgt} PRIVATE -DUSE_VTK)
|
||||
endif()
|
||||
if(HAVE_OPENGL AND sample_filename MATCHES "detect_mser")
|
||||
|
||||
@@ -27,4 +27,4 @@ message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
|
||||
add_executable(opencv_example example.cpp)
|
||||
|
||||
# Link your application with OpenCV libraries
|
||||
target_link_libraries(opencv_example ${OpenCV_LIBS})
|
||||
target_link_libraries(opencv_example LINK_PRIVATE ${OpenCV_LIBS})
|
||||
|
||||
@@ -17,5 +17,5 @@ ocv_include_modules_recurse(${OPENCV_CPP_SAMPLES_REQUIRED_DEPS})
|
||||
add_executable( ${target}pnp_registration ${sample_dir}main_registration.cpp ${sample_pnplib} )
|
||||
add_executable( ${target}pnp_detection ${sample_dir}main_detection.cpp ${sample_pnplib} )
|
||||
|
||||
ocv_target_link_libraries( ${target}pnp_registration ${OPENCV_LINKER_LIBS} ${OPENCV_CPP_SAMPLES_REQUIRED_DEPS} )
|
||||
ocv_target_link_libraries( ${target}pnp_detection ${OPENCV_LINKER_LIBS} ${OPENCV_CPP_SAMPLES_REQUIRED_DEPS} )
|
||||
ocv_target_link_libraries(${target}pnp_registration LINK_PRIVATE ${OPENCV_LINKER_LIBS} ${OPENCV_CPP_SAMPLES_REQUIRED_DEPS})
|
||||
ocv_target_link_libraries(${target}pnp_detection LINK_PRIVATE ${OPENCV_LINKER_LIBS} ${OPENCV_CPP_SAMPLES_REQUIRED_DEPS})
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
#include <iostream>
|
||||
#include <opencv2/imgcodecs.hpp>
|
||||
#include <opencv2/imgproc.hpp>
|
||||
#include <opencv2/videoio.hpp>
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <opencv2/video.hpp>
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
const string about =
|
||||
"This sample demonstrates the camshift algorithm.\n"
|
||||
"The example file can be downloaded from:\n"
|
||||
" https://www.bogotobogo.com/python/OpenCV_Python/images/mean_shift_tracking/slow_traffic_small.mp4";
|
||||
const string keys =
|
||||
"{ h help | | print this help message }"
|
||||
"{ @image |<none>| path to image file }";
|
||||
CommandLineParser parser(argc, argv, keys);
|
||||
parser.about(about);
|
||||
if (parser.has("help"))
|
||||
{
|
||||
parser.printMessage();
|
||||
return 0;
|
||||
}
|
||||
string filename = parser.get<string>("@image");
|
||||
if (!parser.check())
|
||||
{
|
||||
parser.printErrors();
|
||||
return 0;
|
||||
}
|
||||
|
||||
VideoCapture capture(filename);
|
||||
if (!capture.isOpened()){
|
||||
//error in opening the video input
|
||||
cerr << "Unable to open file!" << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Mat frame, roi, hsv_roi, mask;
|
||||
// take first frame of the video
|
||||
capture >> frame;
|
||||
|
||||
// setup initial location of window
|
||||
Rect track_window(300, 200, 100, 50); // simply hardcoded the values
|
||||
|
||||
// set up the ROI for tracking
|
||||
roi = frame(track_window);
|
||||
cvtColor(roi, hsv_roi, COLOR_BGR2HSV);
|
||||
inRange(hsv_roi, Scalar(0, 60, 32), Scalar(180, 255, 255), mask);
|
||||
|
||||
float range_[] = {0, 180};
|
||||
const float* range[] = {range_};
|
||||
Mat roi_hist;
|
||||
int histSize[] = {180};
|
||||
int channels[] = {0};
|
||||
calcHist(&hsv_roi, 1, channels, mask, roi_hist, 1, histSize, range);
|
||||
normalize(roi_hist, roi_hist, 0, 255, NORM_MINMAX);
|
||||
|
||||
// Setup the termination criteria, either 10 iteration or move by atleast 1 pt
|
||||
TermCriteria term_crit(TermCriteria::EPS | TermCriteria::COUNT, 10, 1);
|
||||
|
||||
while(true){
|
||||
Mat hsv, dst;
|
||||
capture >> frame;
|
||||
if (frame.empty())
|
||||
break;
|
||||
cvtColor(frame, hsv, COLOR_BGR2HSV);
|
||||
calcBackProject(&hsv, 1, channels, roi_hist, dst, range);
|
||||
|
||||
// apply camshift to get the new location
|
||||
RotatedRect rot_rect = CamShift(dst, track_window, term_crit);
|
||||
|
||||
// Draw it on image
|
||||
Point2f points[4];
|
||||
rot_rect.points(points);
|
||||
for (int i = 0; i < 4; i++)
|
||||
line(frame, points[i], points[(i+1)%4], 255, 2);
|
||||
imshow("img2", frame);
|
||||
|
||||
int keyboard = waitKey(30);
|
||||
if (keyboard == 'q' || keyboard == 27)
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
#include <iostream>
|
||||
#include <opencv2/imgcodecs.hpp>
|
||||
#include <opencv2/imgproc.hpp>
|
||||
#include <opencv2/videoio.hpp>
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <opencv2/video.hpp>
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
const string about =
|
||||
"This sample demonstrates the meanshift algorithm.\n"
|
||||
"The example file can be downloaded from:\n"
|
||||
" https://www.bogotobogo.com/python/OpenCV_Python/images/mean_shift_tracking/slow_traffic_small.mp4";
|
||||
const string keys =
|
||||
"{ h help | | print this help message }"
|
||||
"{ @image |<none>| path to image file }";
|
||||
CommandLineParser parser(argc, argv, keys);
|
||||
parser.about(about);
|
||||
if (parser.has("help"))
|
||||
{
|
||||
parser.printMessage();
|
||||
return 0;
|
||||
}
|
||||
string filename = parser.get<string>("@image");
|
||||
if (!parser.check())
|
||||
{
|
||||
parser.printErrors();
|
||||
return 0;
|
||||
}
|
||||
|
||||
VideoCapture capture(filename);
|
||||
if (!capture.isOpened()){
|
||||
//error in opening the video input
|
||||
cerr << "Unable to open file!" << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Mat frame, roi, hsv_roi, mask;
|
||||
// take first frame of the video
|
||||
capture >> frame;
|
||||
|
||||
// setup initial location of window
|
||||
Rect track_window(300, 200, 100, 50); // simply hardcoded the values
|
||||
|
||||
// set up the ROI for tracking
|
||||
roi = frame(track_window);
|
||||
cvtColor(roi, hsv_roi, COLOR_BGR2HSV);
|
||||
inRange(hsv_roi, Scalar(0, 60, 32), Scalar(180, 255, 255), mask);
|
||||
|
||||
float range_[] = {0, 180};
|
||||
const float* range[] = {range_};
|
||||
Mat roi_hist;
|
||||
int histSize[] = {180};
|
||||
int channels[] = {0};
|
||||
calcHist(&hsv_roi, 1, channels, mask, roi_hist, 1, histSize, range);
|
||||
normalize(roi_hist, roi_hist, 0, 255, NORM_MINMAX);
|
||||
|
||||
// Setup the termination criteria, either 10 iteration or move by atleast 1 pt
|
||||
TermCriteria term_crit(TermCriteria::EPS | TermCriteria::COUNT, 10, 1);
|
||||
|
||||
while(true){
|
||||
Mat hsv, dst;
|
||||
capture >> frame;
|
||||
if (frame.empty())
|
||||
break;
|
||||
cvtColor(frame, hsv, COLOR_BGR2HSV);
|
||||
calcBackProject(&hsv, 1, channels, roi_hist, dst, range);
|
||||
|
||||
// apply meanshift to get the new location
|
||||
meanShift(dst, track_window, term_crit);
|
||||
|
||||
// Draw it on image
|
||||
rectangle(frame, track_window, 255, 2);
|
||||
imshow("img2", frame);
|
||||
|
||||
int keyboard = waitKey(30);
|
||||
if (keyboard == 'q' || keyboard == 27)
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
#include <iostream>
|
||||
#include <opencv2/core.hpp>
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <opencv2/imgproc.hpp>
|
||||
#include <opencv2/videoio.hpp>
|
||||
#include <opencv2/video.hpp>
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
const string about =
|
||||
"This sample demonstrates Lucas-Kanade Optical Flow calculation.\n"
|
||||
"The example file can be downloaded from:\n"
|
||||
" https://www.bogotobogo.com/python/OpenCV_Python/images/mean_shift_tracking/slow_traffic_small.mp4";
|
||||
const string keys =
|
||||
"{ h help | | print this help message }"
|
||||
"{ @image |<none>| path to image file }";
|
||||
CommandLineParser parser(argc, argv, keys);
|
||||
parser.about(about);
|
||||
if (parser.has("help"))
|
||||
{
|
||||
parser.printMessage();
|
||||
return 0;
|
||||
}
|
||||
string filename = parser.get<string>("@image");
|
||||
if (!parser.check())
|
||||
{
|
||||
parser.printErrors();
|
||||
return 0;
|
||||
}
|
||||
|
||||
VideoCapture capture(filename);
|
||||
if (!capture.isOpened()){
|
||||
//error in opening the video input
|
||||
cerr << "Unable to open file!" << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Create some random colors
|
||||
vector<Scalar> colors;
|
||||
RNG rng;
|
||||
for(int i = 0; i < 100; i++)
|
||||
{
|
||||
int r = rng.uniform(0, 256);
|
||||
int g = rng.uniform(0, 256);
|
||||
int b = rng.uniform(0, 256);
|
||||
colors.push_back(Scalar(r,g,b));
|
||||
}
|
||||
|
||||
Mat old_frame, old_gray;
|
||||
vector<Point2f> p0, p1;
|
||||
|
||||
// Take first frame and find corners in it
|
||||
capture >> old_frame;
|
||||
cvtColor(old_frame, old_gray, COLOR_BGR2GRAY);
|
||||
goodFeaturesToTrack(old_gray, p0, 100, 0.3, 7, Mat(), 7, false, 0.04);
|
||||
|
||||
// Create a mask image for drawing purposes
|
||||
Mat mask = Mat::zeros(old_frame.size(), old_frame.type());
|
||||
|
||||
while(true){
|
||||
Mat frame, frame_gray;
|
||||
|
||||
capture >> frame;
|
||||
if (frame.empty())
|
||||
break;
|
||||
cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
|
||||
|
||||
// calculate optical flow
|
||||
vector<uchar> status;
|
||||
vector<float> err;
|
||||
TermCriteria criteria = TermCriteria((TermCriteria::COUNT) + (TermCriteria::EPS), 10, 0.03);
|
||||
calcOpticalFlowPyrLK(old_gray, frame_gray, p0, p1, status, err, Size(15,15), 2, criteria);
|
||||
|
||||
vector<Point2f> good_new;
|
||||
for(uint i = 0; i < p0.size(); i++)
|
||||
{
|
||||
// Select good points
|
||||
if(status[i] == 1) {
|
||||
good_new.push_back(p1[i]);
|
||||
// draw the tracks
|
||||
line(mask,p1[i], p0[i], colors[i], 2);
|
||||
circle(frame, p1[i], 5, colors[i], -1);
|
||||
}
|
||||
}
|
||||
Mat img;
|
||||
add(frame, mask, img);
|
||||
|
||||
imshow("Frame", img);
|
||||
|
||||
int keyboard = waitKey(30);
|
||||
if (keyboard == 'q' || keyboard == 27)
|
||||
break;
|
||||
|
||||
// Now update the previous frame and previous points
|
||||
old_gray = frame_gray.clone();
|
||||
p0 = good_new;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
#include <iostream>
|
||||
#include <opencv2/core.hpp>
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <opencv2/imgproc.hpp>
|
||||
#include <opencv2/videoio.hpp>
|
||||
#include <opencv2/video.hpp>
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
VideoCapture capture(samples::findFile("vtest.avi"));
|
||||
if (!capture.isOpened()){
|
||||
//error in opening the video input
|
||||
cerr << "Unable to open file!" << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Mat frame1, prvs;
|
||||
capture >> frame1;
|
||||
cvtColor(frame1, prvs, COLOR_BGR2GRAY);
|
||||
|
||||
while(true){
|
||||
Mat frame2, next;
|
||||
capture >> frame2;
|
||||
if (frame2.empty())
|
||||
break;
|
||||
cvtColor(frame2, next, COLOR_BGR2GRAY);
|
||||
|
||||
Mat flow(prvs.size(), CV_32FC2);
|
||||
calcOpticalFlowFarneback(prvs, next, flow, 0.5, 3, 15, 3, 5, 1.2, 0);
|
||||
|
||||
// visualization
|
||||
Mat flow_parts[2];
|
||||
split(flow, flow_parts);
|
||||
Mat magnitude, angle, magn_norm;
|
||||
cartToPolar(flow_parts[0], flow_parts[1], magnitude, angle, true);
|
||||
normalize(magnitude, magn_norm, 0.0f, 1.0f, NORM_MINMAX);
|
||||
angle *= ((1.f / 360.f) * (180.f / 255.f));
|
||||
|
||||
//build hsv image
|
||||
Mat _hsv[3], hsv, hsv8, bgr;
|
||||
_hsv[0] = angle;
|
||||
_hsv[1] = Mat::ones(angle.size(), CV_32F);
|
||||
_hsv[2] = magn_norm;
|
||||
merge(_hsv, 3, hsv);
|
||||
hsv.convertTo(hsv8, CV_8U, 255.0);
|
||||
cvtColor(hsv8, bgr, COLOR_HSV2BGR);
|
||||
|
||||
imshow("frame2", bgr);
|
||||
|
||||
int keyboard = waitKey(30);
|
||||
if (keyboard == 'q' || keyboard == 27)
|
||||
break;
|
||||
|
||||
prvs = next;
|
||||
}
|
||||
}
|
||||
@@ -17,5 +17,5 @@ ocv_include_modules_recurse(${tgt} ${OPENCV_DIRECTX_SAMPLES_REQUIRED_DEPS})
|
||||
file(GLOB all_samples RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)
|
||||
foreach(sample_filename ${all_samples})
|
||||
ocv_define_sample(tgt ${sample_filename} directx)
|
||||
ocv_target_link_libraries(${tgt} ${OPENCV_LINKER_LIBS} ${OPENCV_DIRECTX_SAMPLES_REQUIRED_DEPS})
|
||||
ocv_target_link_libraries(${tgt} LINK_PRIVATE ${OPENCV_LINKER_LIBS} ${OPENCV_DIRECTX_SAMPLES_REQUIRED_DEPS})
|
||||
endforeach()
|
||||
|
||||
@@ -18,5 +18,5 @@ ocv_include_modules_recurse(${OPENCV_DNN_SAMPLES_REQUIRED_DEPS})
|
||||
file(GLOB_RECURSE dnn_samples RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)
|
||||
foreach(sample_filename ${dnn_samples})
|
||||
ocv_define_sample(tgt ${sample_filename} dnn)
|
||||
ocv_target_link_libraries(${tgt} ${OPENCV_LINKER_LIBS} ${OPENCV_DNN_SAMPLES_REQUIRED_DEPS})
|
||||
ocv_target_link_libraries(${tgt} LINK_PRIVATE ${OPENCV_LINKER_LIBS} ${OPENCV_DNN_SAMPLES_REQUIRED_DEPS})
|
||||
endforeach()
|
||||
|
||||
@@ -5,6 +5,11 @@
|
||||
#include <opencv2/imgproc.hpp>
|
||||
#include <opencv2/highgui.hpp>
|
||||
|
||||
#ifdef CV_CXX11
|
||||
#include <thread>
|
||||
#include <queue>
|
||||
#endif
|
||||
|
||||
#include "common.hpp"
|
||||
|
||||
std::string keys =
|
||||
@@ -26,8 +31,9 @@ std::string keys =
|
||||
"0: CPU target (by default), "
|
||||
"1: OpenCL, "
|
||||
"2: OpenCL fp16 (half-float precision), "
|
||||
"3: VPU }";
|
||||
|
||||
"3: VPU }"
|
||||
"{ async | 0 | Number of asynchronous forwards at the same time. "
|
||||
"Choose 0 for synchronous mode }";
|
||||
|
||||
using namespace cv;
|
||||
using namespace dnn;
|
||||
@@ -35,13 +41,66 @@ using namespace dnn;
|
||||
float confThreshold, nmsThreshold;
|
||||
std::vector<std::string> classes;
|
||||
|
||||
inline void preprocess(const Mat& frame, Net& net, Size inpSize, float scale,
|
||||
const Scalar& mean, bool swapRB);
|
||||
|
||||
void postprocess(Mat& frame, const std::vector<Mat>& out, Net& net);
|
||||
|
||||
void drawPred(int classId, float conf, int left, int top, int right, int bottom, Mat& frame);
|
||||
|
||||
void callback(int pos, void* userdata);
|
||||
|
||||
std::vector<String> getOutputsNames(const Net& net);
|
||||
#ifdef CV_CXX11
|
||||
template <typename T>
|
||||
class QueueFPS : public std::queue<T>
|
||||
{
|
||||
public:
|
||||
QueueFPS() : counter(0) {}
|
||||
|
||||
void push(const T& entry)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
|
||||
std::queue<T>::push(entry);
|
||||
counter += 1;
|
||||
if (counter == 1)
|
||||
{
|
||||
// Start counting from a second frame (warmup).
|
||||
tm.reset();
|
||||
tm.start();
|
||||
}
|
||||
}
|
||||
|
||||
T get()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
T entry = this->front();
|
||||
this->pop();
|
||||
return entry;
|
||||
}
|
||||
|
||||
float getFPS()
|
||||
{
|
||||
tm.stop();
|
||||
double fps = counter / tm.getTimeSec();
|
||||
tm.start();
|
||||
return static_cast<float>(fps);
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
while (!this->empty())
|
||||
this->pop();
|
||||
}
|
||||
|
||||
unsigned int counter;
|
||||
|
||||
private:
|
||||
TickMeter tm;
|
||||
std::mutex mutex;
|
||||
};
|
||||
#endif // CV_CXX11
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
@@ -67,6 +126,7 @@ int main(int argc, char** argv)
|
||||
bool swapRB = parser.get<bool>("rgb");
|
||||
int inpWidth = parser.get<int>("width");
|
||||
int inpHeight = parser.get<int>("height");
|
||||
size_t async = parser.get<int>("async");
|
||||
CV_Assert(parser.has("model"));
|
||||
std::string modelPath = findFile(parser.get<String>("model"));
|
||||
std::string configPath = findFile(parser.get<String>("config"));
|
||||
@@ -104,6 +164,108 @@ int main(int argc, char** argv)
|
||||
else
|
||||
cap.open(parser.get<int>("device"));
|
||||
|
||||
#ifdef CV_CXX11
|
||||
bool process = true;
|
||||
|
||||
// Frames capturing thread
|
||||
QueueFPS<Mat> framesQueue;
|
||||
std::thread framesThread([&](){
|
||||
Mat frame;
|
||||
while (process)
|
||||
{
|
||||
cap >> frame;
|
||||
if (!frame.empty())
|
||||
framesQueue.push(frame.clone());
|
||||
else
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
// Frames processing thread
|
||||
QueueFPS<Mat> processedFramesQueue;
|
||||
QueueFPS<std::vector<Mat> > predictionsQueue;
|
||||
std::thread processingThread([&](){
|
||||
std::queue<std::future<Mat> > futureOutputs;
|
||||
Mat blob;
|
||||
while (process)
|
||||
{
|
||||
// Get a next frame
|
||||
Mat frame;
|
||||
{
|
||||
if (!framesQueue.empty())
|
||||
{
|
||||
frame = framesQueue.get();
|
||||
if (async)
|
||||
{
|
||||
if (futureOutputs.size() == async)
|
||||
frame = Mat();
|
||||
}
|
||||
else
|
||||
framesQueue.clear(); // Skip the rest of frames
|
||||
}
|
||||
}
|
||||
|
||||
// Process the frame
|
||||
if (!frame.empty())
|
||||
{
|
||||
preprocess(frame, net, Size(inpWidth, inpHeight), scale, mean, swapRB);
|
||||
processedFramesQueue.push(frame);
|
||||
|
||||
if (async)
|
||||
{
|
||||
futureOutputs.push(net.forwardAsync());
|
||||
}
|
||||
else
|
||||
{
|
||||
std::vector<Mat> outs;
|
||||
net.forward(outs, outNames);
|
||||
predictionsQueue.push(outs);
|
||||
}
|
||||
}
|
||||
|
||||
while (!futureOutputs.empty() &&
|
||||
futureOutputs.front().wait_for(std::chrono::seconds(0)) == std::future_status::ready)
|
||||
{
|
||||
Mat out = futureOutputs.front().get();
|
||||
predictionsQueue.push({out});
|
||||
futureOutputs.pop();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Postprocessing and rendering loop
|
||||
while (waitKey(1) < 0)
|
||||
{
|
||||
if (predictionsQueue.empty())
|
||||
continue;
|
||||
|
||||
std::vector<Mat> outs = predictionsQueue.get();
|
||||
Mat frame = processedFramesQueue.get();
|
||||
|
||||
postprocess(frame, outs, net);
|
||||
|
||||
if (predictionsQueue.counter > 1)
|
||||
{
|
||||
std::string label = format("Camera: %.2f FPS", framesQueue.getFPS());
|
||||
putText(frame, label, Point(0, 15), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 255, 0));
|
||||
|
||||
label = format("Network: %.2f FPS", predictionsQueue.getFPS());
|
||||
putText(frame, label, Point(0, 30), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 255, 0));
|
||||
|
||||
label = format("Skipped frames: %d", framesQueue.counter - predictionsQueue.counter);
|
||||
putText(frame, label, Point(0, 45), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 255, 0));
|
||||
}
|
||||
imshow(kWinName, frame);
|
||||
}
|
||||
|
||||
process = false;
|
||||
framesThread.join();
|
||||
processingThread.join();
|
||||
|
||||
#else // CV_CXX11
|
||||
if (async)
|
||||
CV_Error(Error::StsNotImplemented, "Asynchronous forward is supported only with Inference Engine backend.");
|
||||
|
||||
// Process frames.
|
||||
Mat frame, blob;
|
||||
while (waitKey(1) < 0)
|
||||
@@ -115,19 +277,8 @@ int main(int argc, char** argv)
|
||||
break;
|
||||
}
|
||||
|
||||
// Create a 4D blob from a frame.
|
||||
Size inpSize(inpWidth > 0 ? inpWidth : frame.cols,
|
||||
inpHeight > 0 ? inpHeight : frame.rows);
|
||||
blobFromImage(frame, blob, scale, inpSize, mean, swapRB, false);
|
||||
preprocess(frame, net, Size(inpWidth, inpHeight), scale, mean, swapRB);
|
||||
|
||||
// Run a model.
|
||||
net.setInput(blob);
|
||||
if (net.getLayer(0)->outputNameToIndex("im_info") != -1) // Faster-RCNN or R-FCN
|
||||
{
|
||||
resize(frame, frame, inpSize);
|
||||
Mat imInfo = (Mat_<float>(1, 3) << inpSize.height, inpSize.width, 1.6f);
|
||||
net.setInput(imInfo, "im_info");
|
||||
}
|
||||
std::vector<Mat> outs;
|
||||
net.forward(outs, outNames);
|
||||
|
||||
@@ -142,9 +293,29 @@ int main(int argc, char** argv)
|
||||
|
||||
imshow(kWinName, frame);
|
||||
}
|
||||
#endif // CV_CXX11
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline void preprocess(const Mat& frame, Net& net, Size inpSize, float scale,
|
||||
const Scalar& mean, bool swapRB)
|
||||
{
|
||||
static Mat blob;
|
||||
// Create a 4D blob from a frame.
|
||||
if (inpSize.width <= 0) inpSize.width = frame.cols;
|
||||
if (inpSize.height <= 0) inpSize.height = frame.rows;
|
||||
blobFromImage(frame, blob, 1.0, inpSize, Scalar(), swapRB, false, CV_8U);
|
||||
|
||||
// Run a model.
|
||||
net.setInput(blob, "", scale, mean);
|
||||
if (net.getLayer(0)->outputNameToIndex("im_info") != -1) // Faster-RCNN or R-FCN
|
||||
{
|
||||
resize(frame, frame, inpSize);
|
||||
Mat imInfo = (Mat_<float>(1, 3) << inpSize.height, inpSize.width, 1.6f);
|
||||
net.setInput(imInfo, "im_info");
|
||||
}
|
||||
}
|
||||
|
||||
void postprocess(Mat& frame, const std::vector<Mat>& outs, Net& net)
|
||||
{
|
||||
static std::vector<int> outLayers = net.getUnconnectedOutLayers();
|
||||
|
||||
+125
-22
@@ -1,6 +1,13 @@
|
||||
import cv2 as cv
|
||||
import argparse
|
||||
import numpy as np
|
||||
import sys
|
||||
import time
|
||||
from threading import Thread
|
||||
if sys.version_info[0] == '2':
|
||||
import Queue as queue
|
||||
else:
|
||||
import queue
|
||||
|
||||
from common import *
|
||||
from tf_text_graph_common import readTextMessage
|
||||
@@ -35,6 +42,9 @@ parser.add_argument('--target', choices=targets, default=cv.dnn.DNN_TARGET_CPU,
|
||||
'%d: OpenCL, '
|
||||
'%d: OpenCL fp16 (half-float precision), '
|
||||
'%d: VPU' % targets)
|
||||
parser.add_argument('--async', type=int, default=0,
|
||||
help='Number of asynchronous forwards at the same time. '
|
||||
'Choose 0 for synchronous mode')
|
||||
args, _ = parser.parse_known_args()
|
||||
add_preproc_args(args.zoo, parser, 'object_detection')
|
||||
parser = argparse.ArgumentParser(parents=[parser],
|
||||
@@ -173,32 +183,125 @@ def callback(pos):
|
||||
cv.createTrackbar('Confidence threshold, %', winName, int(confThreshold * 100), 99, callback)
|
||||
|
||||
cap = cv.VideoCapture(cv.samples.findFileOrKeep(args.input) if args.input else 0)
|
||||
|
||||
class QueueFPS(queue.Queue):
|
||||
def __init__(self):
|
||||
queue.Queue.__init__(self)
|
||||
self.startTime = 0
|
||||
self.counter = 0
|
||||
|
||||
def put(self, v):
|
||||
queue.Queue.put(self, v)
|
||||
self.counter += 1
|
||||
if self.counter == 1:
|
||||
self.startTime = time.time()
|
||||
|
||||
def getFPS(self):
|
||||
return self.counter / (time.time() - self.startTime)
|
||||
|
||||
|
||||
process = True
|
||||
|
||||
#
|
||||
# Frames capturing thread
|
||||
#
|
||||
framesQueue = QueueFPS()
|
||||
def framesThreadBody():
|
||||
global framesQueue, process
|
||||
|
||||
while process:
|
||||
hasFrame, frame = cap.read()
|
||||
if not hasFrame:
|
||||
break
|
||||
framesQueue.put(frame)
|
||||
|
||||
|
||||
#
|
||||
# Frames processing thread
|
||||
#
|
||||
processedFramesQueue = queue.Queue()
|
||||
predictionsQueue = QueueFPS()
|
||||
def processingThreadBody():
|
||||
global processedFramesQueue, predictionsQueue, args, process
|
||||
|
||||
futureOutputs = []
|
||||
while process:
|
||||
# Get a next frame
|
||||
frame = None
|
||||
try:
|
||||
frame = framesQueue.get_nowait()
|
||||
|
||||
if args.async:
|
||||
if len(futureOutputs) == args.async:
|
||||
frame = None # Skip the frame
|
||||
else:
|
||||
framesQueue.queue.clear() # Skip the rest of frames
|
||||
except queue.Empty:
|
||||
pass
|
||||
|
||||
|
||||
if not frame is None:
|
||||
frameHeight = frame.shape[0]
|
||||
frameWidth = frame.shape[1]
|
||||
|
||||
# Create a 4D blob from a frame.
|
||||
inpWidth = args.width if args.width else frameWidth
|
||||
inpHeight = args.height if args.height else frameHeight
|
||||
blob = cv.dnn.blobFromImage(frame, size=(inpWidth, inpHeight), swapRB=args.rgb, ddepth=cv.CV_8U)
|
||||
processedFramesQueue.put(frame)
|
||||
|
||||
# Run a model
|
||||
net.setInput(blob, scalefactor=args.scale, mean=args.mean)
|
||||
if net.getLayer(0).outputNameToIndex('im_info') != -1: # Faster-RCNN or R-FCN
|
||||
frame = cv.resize(frame, (inpWidth, inpHeight))
|
||||
net.setInput(np.array([[inpHeight, inpWidth, 1.6]], dtype=np.float32), 'im_info')
|
||||
|
||||
if args.async:
|
||||
futureOutputs.append(net.forwardAsync())
|
||||
else:
|
||||
outs = net.forward(outNames)
|
||||
predictionsQueue.put(np.copy(outs))
|
||||
|
||||
while futureOutputs and futureOutputs[0].wait_for(0) == 0:
|
||||
out = futureOutputs[0].get()
|
||||
predictionsQueue.put(np.copy([out]))
|
||||
|
||||
del futureOutputs[0]
|
||||
|
||||
|
||||
framesThread = Thread(target=framesThreadBody)
|
||||
framesThread.start()
|
||||
|
||||
processingThread = Thread(target=processingThreadBody)
|
||||
processingThread.start()
|
||||
|
||||
#
|
||||
# Postprocessing and rendering loop
|
||||
#
|
||||
while cv.waitKey(1) < 0:
|
||||
hasFrame, frame = cap.read()
|
||||
if not hasFrame:
|
||||
cv.waitKey()
|
||||
break
|
||||
try:
|
||||
# Request prediction first because they put after frames
|
||||
outs = predictionsQueue.get_nowait()
|
||||
frame = processedFramesQueue.get_nowait()
|
||||
|
||||
frameHeight = frame.shape[0]
|
||||
frameWidth = frame.shape[1]
|
||||
postprocess(frame, outs)
|
||||
|
||||
# Create a 4D blob from a frame.
|
||||
inpWidth = args.width if args.width else frameWidth
|
||||
inpHeight = args.height if args.height else frameHeight
|
||||
blob = cv.dnn.blobFromImage(frame, args.scale, (inpWidth, inpHeight), args.mean, args.rgb, crop=False)
|
||||
# Put efficiency information.
|
||||
if predictionsQueue.counter > 1:
|
||||
label = 'Camera: %.2f FPS' % (framesQueue.getFPS())
|
||||
cv.putText(frame, label, (0, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0))
|
||||
|
||||
# Run a model
|
||||
net.setInput(blob)
|
||||
if net.getLayer(0).outputNameToIndex('im_info') != -1: # Faster-RCNN or R-FCN
|
||||
frame = cv.resize(frame, (inpWidth, inpHeight))
|
||||
net.setInput(np.array([[inpHeight, inpWidth, 1.6]], dtype=np.float32), 'im_info')
|
||||
outs = net.forward(outNames)
|
||||
label = 'Network: %.2f FPS' % (predictionsQueue.getFPS())
|
||||
cv.putText(frame, label, (0, 30), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0))
|
||||
|
||||
postprocess(frame, outs)
|
||||
label = 'Skipped frames: %d' % (framesQueue.counter - predictionsQueue.counter)
|
||||
cv.putText(frame, label, (0, 45), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0))
|
||||
|
||||
# Put efficiency information.
|
||||
t, _ = net.getPerfProfile()
|
||||
label = 'Inference time: %.2f ms' % (t * 1000.0 / cv.getTickFrequency())
|
||||
cv.putText(frame, label, (0, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0))
|
||||
cv.imshow(winName, frame)
|
||||
except queue.Empty:
|
||||
pass
|
||||
|
||||
cv.imshow(winName, frame)
|
||||
|
||||
process = False
|
||||
framesThread.join()
|
||||
processingThread.join()
|
||||
|
||||
@@ -51,11 +51,11 @@ endif()
|
||||
file(GLOB all_samples RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)
|
||||
foreach(sample_filename ${all_samples})
|
||||
ocv_define_sample(tgt ${sample_filename} gpu)
|
||||
ocv_target_link_libraries(${tgt} ${OPENCV_LINKER_LIBS} ${OPENCV_CUDA_SAMPLES_REQUIRED_DEPS})
|
||||
ocv_target_link_libraries(${tgt} LINK_PRIVATE ${OPENCV_LINKER_LIBS} ${OPENCV_CUDA_SAMPLES_REQUIRED_DEPS})
|
||||
if(HAVE_opencv_xfeatures2d)
|
||||
ocv_target_link_libraries(${tgt} opencv_xfeatures2d)
|
||||
ocv_target_link_libraries(${tgt} LINK_PRIVATE opencv_xfeatures2d)
|
||||
endif()
|
||||
if(HAVE_opencv_cudacodec)
|
||||
ocv_target_link_libraries(${tgt} opencv_cudacodec)
|
||||
ocv_target_link_libraries(${tgt} LINK_PRIVATE opencv_cudacodec)
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
@@ -31,7 +31,7 @@ ocv_include_directories(${OpenCL_INCLUDE_DIR})
|
||||
file(GLOB all_samples RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)
|
||||
foreach(sample_filename ${all_samples})
|
||||
ocv_define_sample(tgt ${sample_filename} opencl)
|
||||
ocv_target_link_libraries(${tgt}
|
||||
ocv_target_link_libraries(${tgt} LINK_PRIVATE
|
||||
${OPENCV_LINKER_LIBS}
|
||||
${OPENCV_OPENCL_SAMPLES_REQUIRED_DEPS}
|
||||
${OpenCL_LIBRARY})
|
||||
|
||||
@@ -23,9 +23,9 @@ if(BUILD_EXAMPLES AND OCV_DEPENDENCIES_FOUND)
|
||||
endif()
|
||||
foreach(sample_filename ${all_samples})
|
||||
ocv_define_sample(tgt ${sample_filename} opengl)
|
||||
ocv_target_link_libraries(${tgt} ${OPENCV_LINKER_LIBS} ${OPENCV_OPENGL_SAMPLES_REQUIRED_DEPS})
|
||||
ocv_target_link_libraries(${tgt} LINK_PRIVATE ${OPENCV_LINKER_LIBS} ${OPENCV_OPENGL_SAMPLES_REQUIRED_DEPS})
|
||||
if(sample_filename STREQUAL "opengl_interop.cpp")
|
||||
ocv_target_link_libraries(${tgt} ${X11_LIBRARIES})
|
||||
ocv_target_link_libraries(${tgt} LINK_PRIVATE ${X11_LIBRARIES})
|
||||
ocv_target_include_directories(${tgt} ${X11_INCLUDE_DIR})
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
@@ -21,5 +21,5 @@ add_definitions(-DIVX_HIDE_INFO_WARNINGS)
|
||||
file(GLOB_RECURSE cpp_samples RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)
|
||||
foreach(sample_filename ${cpp_samples})
|
||||
ocv_define_sample(tgt ${sample_filename} openvx)
|
||||
ocv_target_link_libraries(${tgt} ${OPENCV_LINKER_LIBS} ${OPENCV_OPENVX_SAMPLE_REQUIRED_DEPS})
|
||||
ocv_target_link_libraries(${tgt} LINK_PRIVATE ${OPENCV_LINKER_LIBS} ${OPENCV_OPENVX_SAMPLE_REQUIRED_DEPS})
|
||||
endforeach()
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
import numpy as np
|
||||
import cv2 as cv
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser(description='This sample demonstrates the camshift algorithm. \
|
||||
The example file can be downloaded from: \
|
||||
https://www.bogotobogo.com/python/OpenCV_Python/images/mean_shift_tracking/slow_traffic_small.mp4')
|
||||
parser.add_argument('image', type=str, help='path to image file')
|
||||
args = parser.parse_args()
|
||||
|
||||
cap = cv.VideoCapture(args.image)
|
||||
|
||||
# take first frame of the video
|
||||
ret,frame = cap.read()
|
||||
|
||||
# setup initial location of window
|
||||
x, y, w, h = 300, 200, 100, 50 # simply hardcoded the values
|
||||
track_window = (x, y, w, h)
|
||||
|
||||
# set up the ROI for tracking
|
||||
roi = frame[y:y+h, x:x+w]
|
||||
hsv_roi = cv.cvtColor(roi, cv.COLOR_BGR2HSV)
|
||||
mask = cv.inRange(hsv_roi, np.array((0., 60.,32.)), np.array((180.,255.,255.)))
|
||||
roi_hist = cv.calcHist([hsv_roi],[0],mask,[180],[0,180])
|
||||
cv.normalize(roi_hist,roi_hist,0,255,cv.NORM_MINMAX)
|
||||
|
||||
# Setup the termination criteria, either 10 iteration or move by atleast 1 pt
|
||||
term_crit = ( cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10, 1 )
|
||||
|
||||
while(1):
|
||||
ret, frame = cap.read()
|
||||
|
||||
if ret == True:
|
||||
hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
|
||||
dst = cv.calcBackProject([hsv],[0],roi_hist,[0,180],1)
|
||||
|
||||
# apply camshift to get the new location
|
||||
ret, track_window = cv.CamShift(dst, track_window, term_crit)
|
||||
|
||||
# Draw it on image
|
||||
pts = cv.boxPoints(ret)
|
||||
pts = np.int0(pts)
|
||||
img2 = cv.polylines(frame,[pts],True, 255,2)
|
||||
cv.imshow('img2',img2)
|
||||
|
||||
k = cv.waitKey(30) & 0xff
|
||||
if k == 27:
|
||||
break
|
||||
else:
|
||||
break
|
||||
@@ -0,0 +1,49 @@
|
||||
import numpy as np
|
||||
import cv2 as cv
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser(description='This sample demonstrates the meanshift algorithm. \
|
||||
The example file can be downloaded from: \
|
||||
https://www.bogotobogo.com/python/OpenCV_Python/images/mean_shift_tracking/slow_traffic_small.mp4')
|
||||
parser.add_argument('image', type=str, help='path to image file')
|
||||
args = parser.parse_args()
|
||||
|
||||
cap = cv.VideoCapture(args.image)
|
||||
|
||||
# take first frame of the video
|
||||
ret,frame = cap.read()
|
||||
|
||||
# setup initial location of window
|
||||
x, y, w, h = 300, 200, 100, 50 # simply hardcoded the values
|
||||
track_window = (x, y, w, h)
|
||||
|
||||
# set up the ROI for tracking
|
||||
roi = frame[y:y+h, x:x+w]
|
||||
hsv_roi = cv.cvtColor(roi, cv.COLOR_BGR2HSV)
|
||||
mask = cv.inRange(hsv_roi, np.array((0., 60.,32.)), np.array((180.,255.,255.)))
|
||||
roi_hist = cv.calcHist([hsv_roi],[0],mask,[180],[0,180])
|
||||
cv.normalize(roi_hist,roi_hist,0,255,cv.NORM_MINMAX)
|
||||
|
||||
# Setup the termination criteria, either 10 iteration or move by atleast 1 pt
|
||||
term_crit = ( cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10, 1 )
|
||||
|
||||
while(1):
|
||||
ret, frame = cap.read()
|
||||
|
||||
if ret == True:
|
||||
hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
|
||||
dst = cv.calcBackProject([hsv],[0],roi_hist,[0,180],1)
|
||||
|
||||
# apply meanshift to get the new location
|
||||
ret, track_window = cv.meanShift(dst, track_window, term_crit)
|
||||
|
||||
# Draw it on image
|
||||
x,y,w,h = track_window
|
||||
img2 = cv.rectangle(frame, (x,y), (x+w,y+h), 255,2)
|
||||
cv.imshow('img2',img2)
|
||||
|
||||
k = cv.waitKey(30) & 0xff
|
||||
if k == 27:
|
||||
break
|
||||
else:
|
||||
break
|
||||
@@ -0,0 +1,61 @@
|
||||
import numpy as np
|
||||
import cv2 as cv
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser(description='This sample demonstrates Lucas-Kanade Optical Flow calculation. \
|
||||
The example file can be downloaded from: \
|
||||
https://www.bogotobogo.com/python/OpenCV_Python/images/mean_shift_tracking/slow_traffic_small.mp4')
|
||||
parser.add_argument('image', type=str, help='path to image file')
|
||||
args = parser.parse_args()
|
||||
|
||||
cap = cv.VideoCapture(args.image)
|
||||
|
||||
# params for ShiTomasi corner detection
|
||||
feature_params = dict( maxCorners = 100,
|
||||
qualityLevel = 0.3,
|
||||
minDistance = 7,
|
||||
blockSize = 7 )
|
||||
|
||||
# Parameters for lucas kanade optical flow
|
||||
lk_params = dict( winSize = (15,15),
|
||||
maxLevel = 2,
|
||||
criteria = (cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10, 0.03))
|
||||
|
||||
# Create some random colors
|
||||
color = np.random.randint(0,255,(100,3))
|
||||
|
||||
# Take first frame and find corners in it
|
||||
ret, old_frame = cap.read()
|
||||
old_gray = cv.cvtColor(old_frame, cv.COLOR_BGR2GRAY)
|
||||
p0 = cv.goodFeaturesToTrack(old_gray, mask = None, **feature_params)
|
||||
|
||||
# Create a mask image for drawing purposes
|
||||
mask = np.zeros_like(old_frame)
|
||||
|
||||
while(1):
|
||||
ret,frame = cap.read()
|
||||
frame_gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
|
||||
|
||||
# calculate optical flow
|
||||
p1, st, err = cv.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params)
|
||||
|
||||
# Select good points
|
||||
good_new = p1[st==1]
|
||||
good_old = p0[st==1]
|
||||
|
||||
# draw the tracks
|
||||
for i,(new,old) in enumerate(zip(good_new, good_old)):
|
||||
a,b = new.ravel()
|
||||
c,d = old.ravel()
|
||||
mask = cv.line(mask, (a,b),(c,d), color[i].tolist(), 2)
|
||||
frame = cv.circle(frame,(a,b),5,color[i].tolist(),-1)
|
||||
img = cv.add(frame,mask)
|
||||
|
||||
cv.imshow('frame',img)
|
||||
k = cv.waitKey(30) & 0xff
|
||||
if k == 27:
|
||||
break
|
||||
|
||||
# Now update the previous frame and previous points
|
||||
old_gray = frame_gray.copy()
|
||||
p0 = good_new.reshape(-1,1,2)
|
||||
@@ -0,0 +1,23 @@
|
||||
import numpy as np
|
||||
import cv2 as cv
|
||||
cap = cv.VideoCapture(cv.samples.findFile("vtest.avi"))
|
||||
ret, frame1 = cap.read()
|
||||
prvs = cv.cvtColor(frame1,cv.COLOR_BGR2GRAY)
|
||||
hsv = np.zeros_like(frame1)
|
||||
hsv[...,1] = 255
|
||||
while(1):
|
||||
ret, frame2 = cap.read()
|
||||
next = cv.cvtColor(frame2,cv.COLOR_BGR2GRAY)
|
||||
flow = cv.calcOpticalFlowFarneback(prvs,next, None, 0.5, 3, 15, 3, 5, 1.2, 0)
|
||||
mag, ang = cv.cartToPolar(flow[...,0], flow[...,1])
|
||||
hsv[...,0] = ang*180/np.pi/2
|
||||
hsv[...,2] = cv.normalize(mag,None,0,255,cv.NORM_MINMAX)
|
||||
bgr = cv.cvtColor(hsv,cv.COLOR_HSV2BGR)
|
||||
cv.imshow('frame2',bgr)
|
||||
k = cv.waitKey(30) & 0xff
|
||||
if k == 27:
|
||||
break
|
||||
elif k == ord('s'):
|
||||
cv.imwrite('opticalfb.png',frame2)
|
||||
cv.imwrite('opticalhsv.png',bgr)
|
||||
prvs = next
|
||||
@@ -0,0 +1,31 @@
|
||||
# Utility function: adds sample executable target with name "example_<group>_<file_name>"
|
||||
# Usage:
|
||||
# ocv_define_sample(<output target> <relative filename> <group>)
|
||||
function(ocv_define_sample out_target source sub)
|
||||
get_filename_component(name "${source}" NAME_WE)
|
||||
set(the_target "example_${sub}_${name}")
|
||||
add_executable(${the_target} "${source}")
|
||||
if(TARGET Threads::Threads AND NOT OPENCV_EXAMPLES_DISABLE_THREADS)
|
||||
target_link_libraries(${the_target} LINK_PRIVATE Threads::Threads)
|
||||
endif()
|
||||
set_target_properties(${the_target} PROPERTIES PROJECT_LABEL "(sample) ${name}")
|
||||
if(ENABLE_SOLUTION_FOLDERS)
|
||||
set_target_properties(${the_target} PROPERTIES FOLDER "samples/${sub}")
|
||||
endif()
|
||||
if(WIN32 AND MSVC AND NOT BUILD_SHARED_LIBS)
|
||||
set_target_properties(${the_target} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:atlthunk.lib /NODEFAULTLIB:atlsd.lib /DEBUG")
|
||||
endif()
|
||||
if(WIN32)
|
||||
install(TARGETS ${the_target} RUNTIME DESTINATION "samples/${sub}" COMPONENT samples)
|
||||
endif()
|
||||
# Add single target to build all samples in the group: 'make opencv_samples_cpp'
|
||||
set(parent_target opencv_samples_${sub})
|
||||
if(NOT TARGET ${parent_target})
|
||||
add_custom_target(${parent_target})
|
||||
if(TARGET opencv_samples)
|
||||
add_dependencies(opencv_samples ${parent_target})
|
||||
endif()
|
||||
endif()
|
||||
add_dependencies(${parent_target} ${the_target})
|
||||
set(${out_target} ${the_target} PARENT_SCOPE)
|
||||
endfunction()
|
||||
@@ -22,5 +22,5 @@ ocv_include_modules_recurse(${OPENCV_TAPI_SAMPLES_REQUIRED_DEPS})
|
||||
file(GLOB all_samples RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)
|
||||
foreach(sample_filename ${all_samples})
|
||||
ocv_define_sample(tgt ${sample_filename} tapi)
|
||||
ocv_target_link_libraries(${tgt} ${OPENCV_LINKER_LIBS} ${OPENCV_TAPI_SAMPLES_REQUIRED_DEPS})
|
||||
ocv_target_link_libraries(${tgt} LINK_PRIVATE ${OPENCV_LINKER_LIBS} ${OPENCV_TAPI_SAMPLES_REQUIRED_DEPS})
|
||||
endforeach()
|
||||
|
||||
@@ -17,5 +17,5 @@ ocv_include_modules_recurse(${OPENCV_VA_INTEL_SAMPLES_REQUIRED_DEPS})
|
||||
file(GLOB all_samples RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)
|
||||
foreach(sample_filename ${all_samples})
|
||||
ocv_define_sample(tgt ${sample_filename} va_intel)
|
||||
ocv_target_link_libraries(${tgt} ${OPENCV_LINKER_LIBS} ${OPENCV_VA_INTEL_SAMPLES_REQUIRED_DEPS} ${VA_LIBRARIES} ${VA_INTEL_LIBRARIES})
|
||||
ocv_target_link_libraries(${tgt} LINK_PRIVATE ${OPENCV_LINKER_LIBS} ${OPENCV_VA_INTEL_SAMPLES_REQUIRED_DEPS} ${VA_LIBRARIES} ${VA_INTEL_LIBRARIES})
|
||||
endforeach()
|
||||
|
||||
Reference in New Issue
Block a user