基于Qt 稠密光流farnaeback cuda接口 

tech2023-10-30  99

将opencv 稠密光流farnaeback cuda  接口 简答记录一下

#include <iostream> #include <vector> #include <sstream> #include <cmath> #include "opencv2/core.hpp" #include "opencv2/core/utility.hpp" #include "opencv2/highgui.hpp" #include "opencv2/video.hpp" #include "opencv2/cudaoptflow.hpp" #include "opencv2/cudaarithm.hpp" using namespace std; using namespace cv; using namespace cv::cuda; template <typename T> inline T mapVal(T x, T a, T b, T c, T d) { x = ::max(::min(x, b), a); return c + (d-c) * (x-a) / (b-a); } static void colorizeFlow(const Mat &u, const Mat &v, Mat &dst) { double uMin, uMax; cv::minMaxLoc(u, &uMin, &uMax, 0, 0); double vMin, vMax; cv::minMaxLoc(v, &vMin, &vMax, 0, 0); uMin = ::abs(uMin); uMax = ::abs(uMax); vMin = ::abs(vMin); vMax = ::abs(vMax); float dMax = static_cast<float>(std::max(std::max(uMin, uMax), std::max(vMin, vMax))); dst.create(u.size(), CV_8UC3); for (int y = 0; y < u.rows; ++y) { for (int x = 0; x < u.cols; ++x) { dst.at<uchar>(y,3*x) = 0; dst.at<uchar>(y,3*x+1) = (uchar)mapVal(-v.at<float>(y,x), -dMax, dMax, 0.f, 255.f); dst.at<uchar>(y,3*x+2) = (uchar)mapVal(u.at<float>(y,x), -dMax, dMax, 0.f, 255.f); } } } int main(int argc, char **argv) { const std::string rtsp_or_video_addr="/home/wang/images/drums10.mp4"; cv::VideoCapture cap(rtsp_or_video_addr); if(!cap.isOpened()){ std::cout<<"frame capture failed!\n"; return -1; } Mat frame; Mat curGray,prevGray; namedWindow("flow",WINDOW_NORMAL); Ptr<cuda::FarnebackOpticalFlow> d_calc = cuda::FarnebackOpticalFlow::create(); cv::Mat flowx, flowy, image; bool running=true; while (running) { cap>>frame; cvtColor(frame,curGray,COLOR_BGR2GRAY); if(!prevGray.empty()){ GpuMat gpuPrevFrame(prevGray); GpuMat gpuCurFrame(curGray); GpuMat flowMat; d_calc->calc(gpuPrevFrame, gpuCurFrame, flowMat); GpuMat planes[2]; cuda::split(flowMat, planes); planes[0].download(flowx); planes[1].download(flowy); colorizeFlow(flowx, flowy, image); //motionToColor(image,image); cv::imshow("flow", image); cv::waitKey(3); } std::swap(prevGray,curGray); } return 0; }

配置文件:

QT -= gui CONFIG += c++11 console CONFIG -= app_bundle # The following define makes your compiler emit warnings if you use # any Qt feature that has been marked deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ main.cpp INCLUDEPATH += /usr/local/include\ /usr/local/include/opencv\ /usr/local/include/opencv2\ LIBS +=/usr/local/lib/libopencv_*.so\ /usr/local/cuda-9.0/lib64/libcudart.so.9.0\ /usr/local/cuda-9.0/lib64/libcudnn.so.7\ /usr/local/cuda-9.0/lib64/libcurand.so.9.0\ /usr/local/cuda-9.0/lib64/libcublas.so.9.0

 

最新回复(0)