5/28/2015

OpenCV 3.0 rc1, FarnebackOpticalFlow(dense optical flow) example source code.

This article introduces FarnebackOpticalFlow example in opencv 3.0 rc1.
FarnebackOpticalFlow class also was changed dll name to reference.

This example is similar with here(http://study.marearts.com/2014/04/opencv-study-example-source-code-for.html).


This example will help what dll and header file need to use FarnebackOpticalFlow  class.

Required Dlls are follows :
(tbb.dll is my option)

And refer to this example.

#include < iostream>  
#include < opencv2\opencv.hpp>  
#include < opencv2\highgui.hpp>  
//#include < opencv2\imgcodecs.hpp>  
#include < opencv2\videoio.hpp> 
#include < opencv2\core\cuda.hpp>
#include < opencv2\imgproc.hpp>
#include < opencv2\cudawarping.hpp>
#include < opencv2\cudaimgproc.hpp>
//#include < opencv2\cudaarithm.hpp>
#include < opencv2\cudaoptflow.hpp>


#ifdef _DEBUG             
#pragma comment(lib, "opencv_core300d.lib")     
#pragma comment(lib, "opencv_highgui300d.lib")  
//#pragma comment(lib, "opencv_imgcodecs300d.lib")  //imread
#pragma comment(lib, "opencv_videoio300d.lib") //video capture
#pragma comment(lib, "opencv_imgproc300d.lib") //line, circle
#pragma comment(lib, "opencv_cudawarping300d.lib") //cuda::resize
#pragma comment(lib, "opencv_cudaimgproc300.lib") //cuda::cvtcolor
#pragma comment(lib, "opencv_cudaarithm300d.lib") //cuda::farnebackOpticalFlow
#pragma comment(lib, "opencv_cudaoptflow300d.lib") 
#else     
#pragma comment(lib, "opencv_core300.lib")     
#pragma comment(lib, "opencv_highgui300.lib")  
//#pragma comment(lib, "opencv_imgcodecs300.lib")  //imread
#pragma comment(lib, "opencv_videoio300.lib") //video capture
#pragma comment(lib, "opencv_imgproc300.lib") // //line, circle
#pragma comment(lib, "opencv_cudawarping300.lib") //cuda::resize
#pragma comment(lib, "opencv_cudaimgproc300.lib") //cuda::cvtcolor
#pragma comment(lib, "opencv_cudaarithm300.lib") //cuda::farnebackOpticalFlow
#pragma comment(lib, "opencv_cudaoptflow300.lib") 

#endif      

using namespace std;
using namespace cv;

void drawOptFlowMap_gpu(const Mat& flow_xy, Mat& cflowmap, int step, const Scalar& color);

int main()
{

 int s = 1;

 unsigned long AAtime = 0, BBtime = 0;

 //variables  
 Mat GetImg, flow_x, flow_y, next, prvs, flow_xy;

 //gpu variable  
 cuda::GpuMat prvs_gpu, next_gpu, flow_x_gpu, flow_y_gpu, flow_xy_gpu;
 cuda::GpuMat prvs_gpu_o, next_gpu_o;
 cuda::GpuMat prvs_gpu_c, next_gpu_c;

 //file name  
 char fileName[100] = "M:\\____videoSample____\\Rendering\\Wildlife.avi";
 //video file open  
 VideoCapture stream1(fileName);   //0 is the id of video device.0 if you have only one camera
 if (!(stream1.read(GetImg))) //get one frame form video
  return 0;

 //gpu upload, resize, color convert
 prvs_gpu_o.upload(GetImg);

 
 cuda::resize(prvs_gpu_o, prvs_gpu_c, Size(GetImg.size().width / s, GetImg.size().height / s));
 cuda::cvtColor(prvs_gpu_c, prvs_gpu, CV_BGR2GRAY);

 //dense optical flow
 Ptr< cuda::FarnebackOpticalFlow > fbOF = cuda::FarnebackOpticalFlow::create();

 

 //unconditional loop
 while (true) {

  if (!(stream1.read(GetImg))) //get one frame form video     
   break;

   ///////////////////////////////////////////////////////////////////  
  //gpu upload, resize, color convert  
  next_gpu_o.upload(GetImg);
  cuda::resize(next_gpu_o, next_gpu_c, Size(GetImg.size().width / s, GetImg.size().height / s));
  cuda::cvtColor(next_gpu_c, next_gpu, CV_BGR2GRAY);
  ///////////////////////////////////////////////////////////////////  

  AAtime = getTickCount();
  //dense optical flow  
  fbOF->calc(prvs_gpu, next_gpu, flow_xy_gpu);

  BBtime = getTickCount();
  float pt = (BBtime - AAtime) / getTickFrequency();
  float fpt = 1 / pt;
  printf("%.2lf / %.2lf \n", pt, fpt);

  //copy for vector flow drawing  
  Mat cflow;
  resize(GetImg, cflow, Size(GetImg.size().width / s, GetImg.size().height / s));  
  flow_xy_gpu.download(flow_xy);
  drawOptFlowMap_gpu(flow_xy, cflow, 10, CV_RGB(0, 255, 0));
  imshow("OpticalFlowFarneback", cflow);

  ///////////////////////////////////////////////////////////////////  
  //Display gpumat  
  next_gpu.download(next);
  prvs_gpu.download(prvs);
  imshow("next", next);
  imshow("prvs", prvs);

  //prvs mat update  
  prvs_gpu = next_gpu.clone();

  if (waitKey(5) >= 0)
   break;
 }


 return 0;
}

void drawOptFlowMap_gpu(const Mat& flow_xy, Mat& cflowmap, int step, const Scalar& color)
{

 for (int y = 0; y < cflowmap.rows; y += step)
  for (int x = 0; x < cflowmap.cols; x += step)
  {
   Point2f fxy;
   fxy.x = cvRound(flow_xy.at< Vec2f >(y, x)[0] + x);
   fxy.y = cvRound(flow_xy.at< Vec2f >(y, x)[1] + y);


   cv::line(cflowmap, Point(x, y), Point(fxy.x, fxy.y), color);
   cv::circle(cflowmap, Point(fxy.x, fxy.y), 1, color, -1);

  }

}




5/26/2015

I calculated hmm forward algorithm in manually.

Mr. soum asks me how to operate forward algorithm in this page code.
http://study.marearts.com/2013/02/hmm-forward-algorithm-source-code.html?spref=fb

I calculated this case in manually.
I hope to help to you.






5/06/2015

OpenCV300 rc1 - VideoCapture, imread example

This is Video capture example in ver. opencv 3.0 rc1.

In 3.0, location of functions are little changed.
In case of VideoCapture, we should include opencv_videoio300.lib.

So note to this source code for easy coding access.


#include < iostream>
#include < opencv2\opencv.hpp>
#include < opencv2\videoio.hpp>
#include < opencv2\highgui.hpp>

#ifdef _DEBUG           
#pragma comment(lib, "opencv_core300d.lib")   
#pragma comment(lib, "opencv_highgui300d.lib")
#pragma comment(lib, "opencv_videoio300d.lib")
#else   
#pragma comment(lib, "opencv_core300.lib")   
#pragma comment(lib, "opencv_highgui300.lib")
#pragma comment(lib, "opencv_videoio300.lib")
#endif    

using namespace std;
using namespace cv;
void main()
{
 
 Mat test;
 cv::VideoCapture cap("M:\\____videoSample____\\posco\\cartype1.avi");//0); 
 namedWindow("t");
 while(1)
 {
  cap >> test;
  if( test.empty() )
   break;

  imshow("t", test);

  if( waitKey(10) > 0)
   break;
 }
}



----

This is source code of using "imread" function. (need opencv_imgcodecs300.lib )

#include < iostream>
#include < opencv2\opencv.hpp>
#include < opencv2\highgui.hpp>
#include < opencv2\imgcodecs.hpp>

#ifdef _DEBUG           
#pragma comment(lib, "opencv_core300d.lib")   
#pragma comment(lib, "opencv_highgui300d.lib")
#pragma comment(lib, "opencv_imgcodecs300d.lib")
#else   
#pragma comment(lib, "opencv_core300.lib")   
#pragma comment(lib, "opencv_highgui300.lib")
#pragma comment(lib, "opencv_imgcodecs300.lib")
#endif    

using namespace std;
using namespace cv;
void main()
{
 
 Mat test;
 test = imread("M:\\____videoSample____\\torso\\Positive1.png");

 namedWindow("t");
 imshow("t", test);
 waitKey(0);

}





QT + OpenCV error : C1083 opencv2/opencv.hpp no such file or directory

In Qt, setting of "include and lib path" is configured in the .pro file.

For example :

INCLUDEPATH += C:\opencv300rc1\opencv\build\include
LIBS += C:\opencv300rc1\MyBuild\lib\Debug

But in my case, I can not find the reason of error "C1083 opencv.hpp no such file or directory..."

I tried various attempts, but I don't know why error occur.
But I found the way of to avoid the error.
That is using build setting tab (ctrl + 5).
In build environment, add opencv path INCLUDE and LIB.



I solved the problem in this way.
Please note.











Creating dynamic link library including OpenCV(on MS Visual studio)


The method to make dynamic link library is introduced in here.
https://msdn.microsoft.com/en-us//library/ms235636.aspx

This introduction is similar with this.
But if you refer both, you will be more helpful.

And see the this youtube video.
This video is also very useful.
https://www.youtube.com/watch?v=yEqRyQhhto8


firstly, make dll project in VS tool. like this->



And add new class.

And set opencv include and lib path setting.


Now coding your opencv application class.
see the sample code
In here, please note OPENCVDLL_API keyword.
OPENCVDLL_API is replaced dllexport or dllimport.
and It surely have to declare in front of exporting class name.


OpenCVDLL.h
...

#pragma once
// OpenCVDLL.h

#ifdef OPENCVDLL_EXPORTS
#define OPENCVDLL_API __declspec(dllexport) 
#else
#define OPENCVDLL_API __declspec(dllimport) 
#endif

#include < opencv2\opencv.hpp>

#ifdef _DEBUG            
#pragma comment(lib, "opencv_core249d.lib")    
#pragma comment(lib, "opencv_imgproc249d.lib")   //MAT processing    
#else    
#pragma comment(lib, "opencv_core249.lib")    
#pragma comment(lib, "opencv_imgproc249.lib")   
#endif  

namespace opencvdll_mare
{

 class OPENCVDLL_API OpenCVDLL
 {
 public:
  OpenCVDLL(void);
  ~OpenCVDLL(void);
  cv::Mat convert(cv::Mat& inMat);

 };
}


...

OpenCVDLL.cpp
...
#include "OpenCVDLL.h"

namespace opencvdll_mare
{

 OpenCVDLL::OpenCVDLL(void)
 {
 }


 OpenCVDLL::~OpenCVDLL(void)
 {
 }

 cv::Mat OpenCVDLL::convert(cv::Mat& inMat)
 {
  cv::Mat cMat;
  cv::Sobel(inMat, cMat, inMat.depth(), 1, 0);
  return cMat;
 }


}
...

You will get .dll, .lib and .h file after compile the OpenCVDLL project.

".dll, .lib" is build in Release or Debug folder. ".h" is just OpenCVDLL.h .


Now, let's use this dll in normal project.
Firstly, make console project and copy files of .dll, .lib, .h into new project folder.

We have to do lib setting. OpenCV setting is same with normal method.
Our dll setting is same with OpenCV.
But we don't need to set include folder path, because we copy header file in local project.



And coding your application.
Sample is like this.
loadOpenCVDLL/Source.cpp
...
#include < iostream>
using namespace std;

#include < opencv2\opencv.hpp>
#include "OpenCVDLL.h"

#ifdef _DEBUG            

#pragma comment(lib, "opencv_highgui249d.lib")    
#else
#pragma comment(lib, "opencv_highgui249.lib")    
#endif  


using namespace cv;

void main()
{
 

 Mat test = imread("AA.jpg");
 imshow("test",test);

 opencvdll_mare::OpenCVDLL cOpenCVDLL_test;
 Mat outmat = cOpenCVDLL_test.convert(test);
 imshow("dll test", outmat);

 waitKey(0);
}
...


After all, build and check the result.
My result is here.



5/04/2015

Java.lang.NullPointerException error, matlab install on the MAC

I met the this error message, after installing matlab on MAC.


But don't worry about that, this youtube video will solve.
Watch this.



Thank you.

Basic MOG2 example souce using opencv

Basic MOG2 source code.




...
#include < time.h>  
#include < opencv2\opencv.hpp>  
#include < opencv2\gpu\gpu.hpp>  
#include < string>  
#include < stdio.h>  


#ifdef _DEBUG          
#pragma comment(lib, "opencv_core249d.lib")  
#pragma comment(lib, "opencv_imgproc249d.lib")   //MAT processing  
#pragma comment(lib, "opencv_gpu249d.lib")  
#pragma comment(lib, "opencv_highgui249d.lib")  
#else  
#pragma comment(lib, "opencv_core249.lib")  
#pragma comment(lib, "opencv_imgproc249.lib")  
#pragma comment(lib, "opencv_gpu249.lib")  
#pragma comment(lib, "opencv_highgui249.lib")  
#endif     


#define RWIDTH 800  
#define RHEIGHT 600  

using namespace std;  
using namespace cv;  

int main()  
{  
 /////////////////////////////////////////////////////////////////////////  
 gpu::MOG2_GPU pMOG2_g(30);  
 pMOG2_g.history = 3000; //300;  
 pMOG2_g.varThreshold =64; //128; //64; //32;//;   
 pMOG2_g.bShadowDetection = true;  
 Mat Mog_Mask;  
 gpu::GpuMat Mog_Mask_g;  
 /////////////////////////////////////////////////////////////////////////  


 VideoCapture cap("M:\\____videoSample____\\blog\\video20.wmv");//0);  
 /////////////////////////////////////////////////////////////////////////  
 Mat o_frame;  
 gpu::GpuMat o_frame_gpu;  
 /////////////////////////////////////////////////////////////////////////  

 cap >> o_frame;  
 if( o_frame.empty() )  
  return 0;   
 
 /////////////////////////////////////////////////////////////////////////  


 unsigned long AAtime=0, BBtime=0;  

 //Mat rFrame;  
 Mat showMat_r_blur;  
 Mat showMat_r;  

 namedWindow("origin");
 namedWindow("mog_mask");

 while(1)  
 {  
  /////////////////////////////////////////////////////////////////////////  
  cap >> o_frame;  
  if( o_frame.empty() )  
   return 0;  


  o_frame_gpu.upload(o_frame);
  AAtime = getTickCount();
  
  //  
  pMOG2_g.operator()(o_frame_gpu, Mog_Mask_g,-1);  
  //
  Mog_Mask_g.download(Mog_Mask);

  BBtime = getTickCount();
  float pt = (BBtime - AAtime)/getTickFrequency();
  float fpt = 1/pt;
  printf("gpu %.4lf / %.4lf \n",  pt, fpt );


  
  o_frame_gpu.download(showMat_r);
  imshow("origin", showMat_r); 
  imshow("mog_mask", Mog_Mask);


  /////////////////////////////////////////////////////////////////////////  

  if( waitKey(10) > 0)  
   break;  
 }  

 return 0;  
}  

build opencv 3.0 + cuda 6.5 + QT 5.4 + tbb

These are cmake configuration for OpenCV rc1 + CUDA 6.5 + QT 5.4 + TBB 4.3








I thought it will help opencv building beginner.
I will help how to set path and what I should check.

And configuration result is here.



Now I am building on VS 2012... I hope to build with no error.. ^^

Refer to this page, these are my result of building opencv in waste of time.