12/13/2013

SurfFeatureDetector exmaple source code (OpenCV)

This is 'SurfFeatureDetector' example source code.
On this page(http://feelmare.blogspot.kr/2013/12/surfgpu-example-source-code-feature.html), I introduced SURF_GPU.
In my case, The processing time difference is small.
GPU-> 0.99 sec, CPU-> 1.04 sec.


 



 

////
#include < stdio.h >  
#include < opencv2\opencv.hpp >
#include < opencv2\features2d\features2d.hpp >
#include < opencv2\nonfree\features2d.hpp >

#ifdef _DEBUG  
#pragma comment(lib, "opencv_core246d.lib")   
//#pragma comment(lib, "opencv_imgproc246d.lib")   //MAT processing  
//#pragma comment(lib, "opencv_objdetect246d.lib")   
//#pragma comment(lib, "opencv_gpu246d.lib")  
#pragma comment(lib, "opencv_features2d246d.lib")  
#pragma comment(lib, "opencv_highgui246d.lib")  
//#pragma comment(lib, "opencv_ml246d.lib")
//#pragma comment(lib, "opencv_stitching246d.lib");
#pragma comment(lib, "opencv_nonfree246d.lib");

#else  
#pragma comment(lib, "opencv_core246.lib")  
//#pragma comment(lib, "opencv_imgproc246.lib")  
//#pragma comment(lib, "opencv_objdetect246.lib")  
//#pragma comment(lib, "opencv_gpu246.lib")  
#pragma comment(lib, "opencv_features2d246.lib")  
#pragma comment(lib, "opencv_highgui246.lib")  
//#pragma comment(lib, "opencv_ml246.lib")  
//#pragma comment(lib, "opencv_stitching246.lib");
#pragma comment(lib, "opencv_nonfree246.lib");
#endif  

using namespace cv;  
using namespace std;


void main()  
{
/////CPU mode


//processign tiem measurement
unsigned long AAtime=0, BBtime=0;

//SURF_GPU example source code
Mat inImg,outImg;
vector src_keypoints;
vector src_descriptors;


//image load
inImg = imread("ship.png",0);

//FeatureFinder 
SurfFeatureDetector FeatureFinder(400);

//processing time measure
AAtime = getTickCount();

//Feature Extraction
FeatureFinder.detect( inImg, src_keypoints );

//Processing time measurement
BBtime = getTickCount(); 

//Features Draw
drawKeypoints( inImg, src_keypoints, outImg, Scalar::all(-1), DrawMatchesFlags::DRAW_RICH_KEYPOINTS );

imshow("Show", outImg); 
printf("Processing time = %.2lf(sec) \n",  (BBtime - AAtime)/getTickFrequency() );
printf("Features %d\n", src_keypoints.size() );

waitKey(0);

//save to file
imwrite("output.jpg", outImg);

}
////





SURF_GPU example source code (feature finder using GPU )

This is example source code about SURF_GPU.
The time of processing is taken 0.99 sec on the 1787x1510 image size.
My environment is
Intel(r) core(TM) i5-3570 cpu@3.4ghz 3.80 Ghz
NVIDA Geforce GTX 650






Example source code



//////
#include < stdio.h >  
#include < opencv2\opencv.hpp >  
#include < opencv2\nonfree\gpu.hpp >


#ifdef _DEBUG  
#pragma comment(lib, "opencv_core246d.lib")   
//#pragma comment(lib, "opencv_imgproc246d.lib")   //MAT processing  
//#pragma comment(lib, "opencv_objdetect246d.lib")   
//#pragma comment(lib, "opencv_gpu246d.lib")  
#pragma comment(lib, "opencv_features2d246d.lib")  
#pragma comment(lib, "opencv_highgui246d.lib")  
//#pragma comment(lib, "opencv_ml246d.lib")
//#pragma comment(lib, "opencv_stitching246d.lib");
#pragma comment(lib, "opencv_nonfree246d.lib");

#else  
#pragma comment(lib, "opencv_core246.lib")  
//#pragma comment(lib, "opencv_imgproc246.lib")  
//#pragma comment(lib, "opencv_objdetect246.lib")  
//#pragma comment(lib, "opencv_gpu246.lib")  
#pragma comment(lib, "opencv_features2d246.lib")  
#pragma comment(lib, "opencv_highgui246.lib")  
//#pragma comment(lib, "opencv_ml246.lib")  
//#pragma comment(lib, "opencv_stitching246.lib");
#pragma comment(lib, "opencv_nonfree246.lib");
#endif  

using namespace cv;  
using namespace std;


void main()  
{
 //processign tiem measurement
 unsigned long AAtime=0, BBtime=0;

 //SURF_GPU example source code
 Mat inImg;
 vector src_keypoints;
 vector src_descriptors;

 gpu::GpuMat inImg_g;
 gpu::GpuMat src_keypoints_gpu, src_descriptors_gpu;

 //image load
 inImg = imread("ship.png",0);

 //FeatureFinder 
 gpu::SURF_GPU FeatureFinder_gpu(400);

 //processing time measure
 AAtime = getTickCount();
 inImg_g.upload(inImg);

 //Feature Extraction
 FeatureFinder_gpu(inImg_g, gpu::GpuMat(), src_keypoints_gpu, src_descriptors_gpu, false);

 //Processing time measurement
 BBtime = getTickCount(); 
 
 //descriptor down
 FeatureFinder_gpu.downloadKeypoints(src_keypoints_gpu, src_keypoints); 
 FeatureFinder_gpu.downloadDescriptors(src_descriptors_gpu, src_descriptors);
 
 //Features Draw
 //νŠΉμ§•μ  뿌리기 1
 drawKeypoints(inImg, src_keypoints, inImg, Scalar::all(-1), DrawMatchesFlags::DRAW_RICH_KEYPOINTS );
 
 imshow("Show", inImg); 

 printf("Processing time = %.2lf(sec) \n",  (BBtime - AAtime)/getTickFrequency() );
 printf("Features %d\n", src_keypoints.size() );

 waitKey(0);

 //save to file
 imwrite("output.jpg", inImg);
}

///

12/11/2013

Arduino, LED On/Off using switch, (Aduino study),

Today, I studied about input signal.
So, my arduino can turn on/off LED by switch.
In the picture, black wire works the role of switch.






///
const int LED = 13;
const int BUTTON = 7;

int val = 0;
int old_val = 0;
int sw_OnOff=0;

void setup(){
  pinMode(LED, OUTPUT);
  pinMode(BUTTON, INPUT);
}


void loop(){
  val = digitalRead(BUTTON);
  
  if((old_val==LOW) && (val==HIGH)){
    sw_OnOff=1-sw_OnOff;
    delay(100);
  }
  
  old_val = val;
  
  
  digitalWrite(LED, sw_OnOff);
  
}
////


But I still don't know why it need register and why the wire connect with there??
But, it is very fun~

12/10/2013

OpenCV, What is the InputArray?

The 'InputArray' is used usually in OpenCV function parameter.
I think that is made to transfer vector< >, Matx< >, Vec< > and scalar easily.

This is the document on the opencv.org.

InputArray and OutputArray

Many OpenCV functions process dense 2-dimensional or multi-dimensional numerical arrays. Usually, such functions take cpp:class:Mat as parameters, but in some cases it’s more convenient to use std::vector<> (for a point set, for example) or Matx<> (for 3x3 homography matrix and such). To avoid many duplicates in the API, special “proxy” classes have been introduced. The base “proxy” class is InputArray. It is used for passing read-only arrays on a function input. The derived from InputArray class OutputArray is used to specify an output array for a function. Normally, you should not care of those intermediate types (and you should not declare variables of those types explicitly) - it will all just work automatically. You can assume that instead of InputArray/OutputArray you can always use Mat, std::vector<>, Matx<>, Vec<> or Scalar. When a function has an optional input or output array, and you do not have or do not want one, pass cv::noArray().


So I tested the 'InputArray' by programming.
Reference this source code.

/////
using namespace std;

void main()
{

 ///////////////////////////////////////////
 ///Test #1
 //Read
 std::vector< cv::Mat > inImgs;
 inImgs.push_back( cv::imread("S1.jpg") );
 inImgs.push_back( cv::imread("S2.jpg") );
 inImgs.push_back( cv::imread("S3.jpg") );

 //input
 cv::InputArray imgs = inImgs; 

 //property
 printf("Total = %d, Size=(%d,%d)\n", imgs.total(), imgs.size().width, imgs.size().height );

 //copy;
 std::vector< cv::Mat > inFunction;
 imgs.getMatVector( inFunction ); 

 //Test inFunction
 cv::imshow("a",inFunction[0]);
  
 cv::waitKey(0);

}
/////
output
 
 
 

Visual studio, the method do not shut the console window (λΉ„μ£Όμ–Ό μŠ€νŠœλ””μ˜€, μ½˜μ†”μ°½ μ•ˆλ‹«νžˆκ²Œ ν•˜λŠ” 방법)

This tip is easy to forget.


Select option to Cosole(..) at the sub system.

[Project] menu, select [Properties] ->
[Configuration Properties] ->
[Linker] ->
[System] ->
[Sub System] Console (/ SUBSYSTEM: CONSOLE)]

Thank you.

avrdude stk500_recv() programmer is not responding Error ( simple solution in my case.. ), Arduino beginner

I meet the error message when I upload source code to the board.


In my case, I set correct board type, at the tools->board menu.
On the Mac, you can see your board type at the system information application.


Change you correct board type and try again.

In my case, communication port is like this picture.




12/05/2013

OpenCV Cuda Example source code

#include < stdio.h >  
#include < opencv2\opencv.hpp >
#include < opencv2\gpu\gpu.hpp >
 
#ifdef _DEBUG  
#pragma comment(lib, "opencv_core246d.lib")   
#pragma comment(lib, "opencv_imgproc246d.lib")   //MAT processing  
//#pragma comment(lib, "opencv_objdetect246d.lib")   
#pragma comment(lib, "opencv_gpu246d.lib")  
//#pragma comment(lib, "opencv_features2d246d.lib")  
#pragma comment(lib, "opencv_highgui246d.lib")   
//#pragma comment(lib, "opencv_ml246d.lib")  
#else  
#pragma comment(lib, "opencv_core246.lib")  
#pragma comment(lib, "opencv_imgproc246.lib")  
//#pragma comment(lib, "opencv_objdetect246.lib")  
#pragma comment(lib, "opencv_gpu246.lib")  
//#pragma comment(lib, "opencv_features2d246.lib")  
#pragma comment(lib, "opencv_highgui246.lib")  
//#pragma comment(lib, "opencv_ml246.lib")  
#endif  

using namespace cv;  


void main()  
{  
 unsigned long AAtime=0, BBtime=0;
 
 Mat img;
 Mat outimg, outimg2;
 img = imread("Tulips.jpg",0); 
 gpu::GpuMat d_src, d_dst;
 
 d_src.upload(img);
 AAtime = getTickCount(); 
 gpu::Canny(d_src, d_dst, 35, 200, 3); 
 BBtime = getTickCount();

 printf("cuda %.5lf \n",  (BBtime - AAtime)/getTickFrequency() );
 d_dst.download(outimg);


 AAtime = getTickCount();
 Canny(img, outimg2, 35, 200, 3);
 BBtime = getTickCount();
 printf("cpu %.5lf \n",  (BBtime - AAtime)/getTickFrequency() );


 

 namedWindow("t");  
 imshow("t",outimg2); 
 namedWindow("t2");  
 imshow("t2",outimg);  
 
 cvWaitKey(0);  
 

} 


////

cuda processing takes 0.00578 sec.
and non-cuda processing takes 0.01707 sec.

In this case, cuda is faster 2times than non-cuda, but cuda will be seen higher speed.