Showing posts with label OpenCV300. Show all posts
Showing posts with label OpenCV300. Show all posts

6/12/2015

MSER text detection example (opencv 300)

MSER text detection example



...
#include < iostream>  
#include < opencv2\opencv.hpp>  
#include < opencv2\highgui.hpp>  
#include < opencv2\imgcodecs.hpp>  
#include < opencv2\core.hpp>
#include < opencv2\imgproc.hpp>
#include < opencv2\text.hpp>
#include < opencv2/features2d.hpp>


#ifdef _DEBUG             
#pragma comment(lib, "opencv_core300d.lib")     
#pragma comment(lib, "opencv_highgui300d.lib")  
#pragma comment(lib, "opencv_imgcodecs300d.lib")
#pragma comment(lib, "opencv_text300d.lib")
#pragma comment(lib, "opencv_features2d300d.lib")
#pragma comment(lib, "opencv_imgproc300d.lib")

#else     
#pragma comment(lib, "opencv_core300.lib")     
#pragma comment(lib, "opencv_highgui300.lib")  
#pragma comment(lib, "opencv_imgcodecs300.lib")  
#pragma comment(lib, "opencv_text300.lib")
#pragma comment(lib, "opencv_features2d300.lib")
#pragma comment(lib, "opencv_imgproc300d.lib")
#endif      

using namespace std;
using namespace cv;

void groups_draw(Mat &src, vector< Rect> &groups);

void main()
{

 Mat inImg = imread(".\\scenetext01.jpg");
 
 vector< Mat> channels;
 text::computeNMChannels(inImg, channels);

 int cn = (int)channels.size();
 // Append negative channels to detect ER- (bright regions over dark background)
 for (int c = 0; c < cn - 1; c++)
  channels.push_back(255 - channels[c]);

 


 Ptr< text::ERFilter> er_filter1 = text::createERFilterNM1(text::loadClassifierNM1(".\\trained_classifierNM1.xml"), 
  16, 0.00015f, 0.13f, 0.2f, true, 0.1f);
 Ptr< text::ERFilter> er_filter2 = text::createERFilterNM2(text::loadClassifierNM2(".\\trained_classifierNM2.xml"), 0.5);

 vector< vector< text::ERStat> > regions(channels.size());

 for (int c = 0; c< (int)channels.size(); c++)
 {
  er_filter1->run(channels[c], regions[c]);
  er_filter2->run(channels[c], regions[c]);
 }


 // Detect character groups
 cout << "Grouping extracted ERs ... ";
 vector< vector< Vec2i> > region_groups;
 vector< Rect> groups_boxes;
 text::erGrouping(inImg, channels, regions, region_groups, groups_boxes, text::ERGROUPING_ORIENTATION_HORIZ);

 groups_draw(inImg, groups_boxes);
 imshow("grouping", inImg);

 waitKey(0);

}


void groups_draw(Mat &src, vector< Rect> &groups)
{
 for (int i = (int)groups.size() - 1; i >= 0; i--)
 {
  if (src.type() == CV_8UC3)
   rectangle(src, groups.at(i).tl(), groups.at(i).br(), Scalar(0, 255, 255), 3, 8);
  else
   rectangle(src, groups.at(i).tl(), groups.at(i).br(), Scalar(255), 3, 8);
 }
}

///

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/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);

}