Skip to content

Instantly share code, notes, and snippets.

@jrussino
Created July 8, 2017 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrussino/c10aece599934797f4cf9e99b6f883aa to your computer and use it in GitHub Desktop.
Save jrussino/c10aece599934797f4cf9e99b6f883aa to your computer and use it in GitHub Desktop.
OpenCV multi-threaded CUDA ORB example (fails with "illegal memory access")
#include <iostream>
#include <thread>
#include <opencv2/cudafeatures2d.hpp>
#include <opencv2/cudaimgproc.hpp>
#include <opencv2/highgui.hpp>
void capture_thread(int device_num, int n_cycles)
{
std::cout << "Starting capture thread for device " + std::to_string(device_num) + "\n" << std::flush;
// open VideoCapture for the specified device
cv::VideoCapture cap(device_num);
if (!cap.isOpened())
{
std::cerr << "ERROR - Failed to open VideoCapture for device " + std::to_string(device_num) + "\n" << std::flush;
return;
}
// each thread gets its own CUDA stream
cv::cuda::Stream stream;
std::cout << "Initialization complete for capture thread for device " + std::to_string(device_num) + "\n" << std::flush;
for(int i = 1; i <= n_cycles; ++i)
{
// capture frame
cv::Mat frame;
if(!cap.read(frame))
{
continue;
}
// upload to GPU
cv::cuda::GpuMat frame_d;
frame_d.upload(frame, stream);
// convert to grayscale
cv::cuda::GpuMat frame_gray_d;
cv::cuda::cvtColor(frame_d, frame_gray_d, CV_BGR2GRAY, 0, stream);
// create CUDA ORB feature detector
cv::Ptr<cv::cuda::ORB> p_orb_d = cv::cuda::ORB::create();
p_orb_d->setBlurForDescriptor(true);
// detect and compute features
cv::cuda::GpuMat kp_d;
cv::cuda::GpuMat desc_d;
p_orb_d->detectAndComputeAsync(frame_gray_d, cv::cuda::GpuMat(), kp_d, desc_d, false, stream);
/*
* Do something with the features & descriptors...
*/
}
std::cout << "Capture thread for device " + std::to_string(device_num) + " finished with no errors\n" << std::flush;
}
int main(int argc, const char* argv[])
{
std::cout << "Starting main program" << std::endl;
// Start two capture threads
std::thread capture_thread_0(capture_thread, 0, 1000);
std::thread capture_thread_1(capture_thread, 1, 1000);
// Wait for both threads to return
capture_thread_0.join();
capture_thread_1.join();
std::cout << "Main program completed with no errors" << std::endl;
return 0;
}
@jiawenhao2015
Copy link

hello~~~~i met the similar error recentlly when i use opencv gpu download or opencv gpu clone, so how do you solve your issue above???
many many thanks~~~waiting for your reply sincerely

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment