12/13/2013

(Arduino study) LED brightness control by switch connection

The source code is combine of Brightness changing(http://feelmare.blogspot.kr/2013/12/arduino-study-led-brightness-changing.html) and LED turn on/off(http://feelmare.blogspot.kr/2013/12/arduino-led-onoff-using-switch-aduino.html).


The LDE brightness is changed when switch is connected.

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

int val = 0;

int old_val = 0;
int state = 0;

int brightness = 128;
unsigned long startTime = 0;

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

void loop()
{
  val = digitalRead(BUTTON);
  
  if( (val == HIGH) && (old_val == LOW) ){
    state = 1-state;
    startTime = millis();
    delay(10);
  }
  
  
  if( (val == HIGH) && (old_val==HIGH) ){
    
    if(state == 1 && (millis() - startTime) > 500 ){
      
      brightness++;
      delay(10);
      
      if(brightness > 255){
        brightness=0;
      }
    }
  }
  
  
  old_val = val;
  if(state == 1)
  {
    analogWrite(LED, brightness);
  }
}
////





(Arduino Study) LED brightness changing, using analog pin-port, (analogWrite function)

'analogWrite' output value's range is 0~255.
In the source code, 'i' is changed 0 to 255 and 255 to 0.
So LED brightness would be changed.


//////
const int LED = 9;
int i=0;

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

void loop(){
  
  for(i=0; i< 255; i++){
    analogWrite(LED, i);
    delay(10);
  }
  
  for(i=255; i> 0; --i)
  {
    analogWrite(LED, i);
    delay(10);
  }
}

//////






How to use mask parameter for SURF feature detector (OpenCV)

The function of mask parameter is we can extract features only desired position(Rect).

This is mask.
 
This is output.
 
We can see features is extracted only white rect.
This is example source code.
 
 
 
 

/////
#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()  
{
 
 //SURF_GPU example source code
 Mat inImg,outImg;
 vector< cv::KeyPoint > src_keypoints;
 vector< float > src_descriptors;
 
 //image load
 inImg = imread("ship.png",0);

 //FeatureFinder 
 SurfFeatureDetector FeatureFinder(400);

 //make mask
 Mat mask = Mat::zeros(inImg.size(), CV_8U);  
 Mat roi(mask, Rect(400,400,400,400) );
 roi = Scalar(255, 255, 255);  

 /*
 //The mode to set multiple masks
 vector< Rect > mask_rect;
 mask_rect.push_back(Rect(0,0,200,200) );
 mask_rect.push_back(Rect(200,200,200,200) );
 mask_rect.push_back(Rect(400,400,200,200) );
 mask_rect.push_back(Rect(600,600,200,200) );

 Mat mask = Mat::zeros(inImg.size(), CV_8U);  
 for(int i=0; i< mask_rect.size(); ++i)
 {
  Mat roi(mask, mask_rect[i] ); 
  roi = Scalar(255, 255, 255);  
 }
 */

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

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

 imshow("Show", outImg); 
 imshow("mask", mask); 
 imshow("roi", roi); 
 waitKey(0);

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

}

/////


If you want to set one more masks, see the comment section.
This source code is advance on this page(http://feelmare.blogspot.kr/2013/12/surffeaturedetector-exmaple-source-code.html).

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.