Sunday, April 28, 2013

Surf Detector

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
using namespace std;
using namespace cv;
//#include "Functions.h"
char* source_window = "Source image";
char* corners_window = "Corners detected";
int main()
{
Mat src;
src = imread("building.jpg",CV_LOAD_IMAGE_GRAYSCALE);
if(src.empty())
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
// -- step 1: Detect the keypoints using SURF Detector
int minHessian = 200;
SurfFeatureDetector detector(minHessian);
vector<KeyPoint> keypoints;
detector.detect(src,keypoints);
Mat img_keypoints;
drawKeypoints(src,keypoints,img_keypoints,Scalar::all(-1), DrawMatchesFlags::DEFAULT);
imshow("Keypoins", img_keypoints);
waitKey(0);
return 0;
}
view raw gistfile1.cpp hosted with ❤ by GitHub

OpenCV Sobel edge detector

Orignal

Sobel X Gradient

Sobel Y Gradient

Sobel Gradient
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
using namespace std;
using namespace cv;
#include "Functions.h"
int main()
{
Mat image;
//image = imread("lena.jpg",1);
image = imread("building.jpg",1);
if(image.empty())
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
/// Convert it to gray
cvtColor( image, image, CV_RGB2GRAY );
resize(image,image,Size(0,0),0.5,0.5,INTER_LINEAR);
namedWindow("Image", CV_WINDOW_AUTOSIZE );
imshow("Image", image);
/// Generate grad_x and grad_y
Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y;
int scale = 1;
int delta = 0;
int ddepth = CV_16S;
Mat grad;
/// Gradient X
//Scharr( image, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT );
Sobel( image, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT );
convertScaleAbs( grad_x, abs_grad_x );
/// Gradient Y
// Scharr( image, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT );
Sobel( image, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT );
convertScaleAbs( grad_y, abs_grad_y );
/// Total Gradient (approximate)
addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );
namedWindow("ImageSobel", CV_WINDOW_AUTOSIZE );
imshow( "ImageSobel", grad );
namedWindow("ImageSobelGx", CV_WINDOW_AUTOSIZE );
imshow( "ImageSobelGx", abs_grad_x );
namedWindow("ImageSobelGy", CV_WINDOW_AUTOSIZE );
imshow( "ImageSobelGy", abs_grad_y );
waitKey(0);
return 0;
}
view raw gistfile1.cpp hosted with ❤ by GitHub

OpenCV Tutorial 1: Mat - The basic Image Container


Color Space In OpenCV
There are, however, many other color systems each with their own advantages:
  • RGB is the most common as our eyes use something similar, our display systems also compose colors using these.
  • The HSV and HLS decompose colors into their hue, saturation and value/luminance components, which is a more natural way for us to describe colors. You might, for example, dismiss the last component, making your algorithm less sensible to the light conditions of the input image.
  • YCrCb is used by the popular JPEG image format.
  • CIE L*a*b* is a perceptually uniform color space, which comes handy if you need to measure the distance of a given color to another color.
Sample Code
// create by using the constructor
Mat M(2,2, CV_8UC3, Scalar(0,0,255));
cout << "M = " << endl << " " << M << endl << endl;
// create by using the create function()
M.create(4,4, CV_8UC(2));
cout << "M = "<< endl << " " << M << endl << endl;
// create multidimensional matrices
int sz[3] = {2,2,2};
Mat L(3,sz, CV_8UC(1), Scalar::all(0));
// Cannot print via operator <<
// Create using MATLAB style eye, ones or zero matrix
Mat E = Mat::eye(4, 4, CV_64F);
cout << "E = " << endl << " " << E << endl << endl;
Mat O = Mat::ones(2, 2, CV_32F);
cout << "O = " << endl << " " << O << endl << endl;
Mat Z = Mat::zeros(3,3, CV_8UC1);
cout << "Z = " << endl << " " << Z << endl << endl;
// create a 3x3 double-precision identity matrix
Mat C = (Mat_<double>(3,3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
cout << "C = " << endl << " " << C << endl << endl;
Mat RowClone = C.row(1).clone();
cout << "RowClone = " << endl << " " << RowClone << endl << endl;
// Fill a matrix with random values
Mat R = Mat(3, 2, CV_8UC3);
randu(R, Scalar::all(0), Scalar::all(255));
// Demonstrate the output formating options
cout << "R (default) = " << endl << R << endl << endl;
cout << "R (python) = " << endl << format(R,"python") << endl << endl;
cout << "R (numpy) = " << endl << format(R,"numpy" ) << endl << endl;
cout << "R (csv) = " << endl << format(R,"csv" ) << endl << endl;
cout << "R (c) = " << endl << format(R,"C" ) << endl << endl;
Point2f P(5, 1);
cout << "Point (2D) = " << P << endl << endl;
Point3f P3f(2, 6, 7);
cout << "Point (3D) = " << P3f << endl << endl;
vector<float> v;
v.push_back( (float)CV_PI); v.push_back(2); v.push_back(3.01f);
cout << "Vector of floats via Mat = " << Mat(v) << endl << endl;
vector<Point2f> vPoints(20);
for (size_t E = 0; E < vPoints.size(); ++E)
vPoints[E] = Point2f((float)(E * 5), (float)(E % 7));
cout << "A vector of 2D Points = " << vPoints << endl << endl;
view raw gistfile1.cpp hosted with ❤ by GitHub

Tuesday, April 16, 2013

Camera Calibration tool box from Caltech

http://www.vision.caltech.edu/bouguetj/calib_doc/
This is a release of a Camera Calibration Toolbox for Matlab® with a complete documentation. This document may also be used as a tutorial on camera calibration since it includes general information about calibration, references and related links. 

Friday, April 5, 2013

Read Camera Write Video

// ReadCameraWriteVideo.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
// Include standard OpenCV headers
#include "cv.h"
#include "highgui.h"
using namespace std;
// All the new API is put into "cv" namespace
using namespace cv;
int main (int argc, char *argv[])
{
// Open the default camera
VideoCapture capture(0);
// Check if the camera was opened
if(!capture.isOpened())
{
cerr << "Could not create capture";
return -1;
}
// Get the properties from the camera
double width = capture.get(CV_CAP_PROP_FRAME_WIDTH);
double height = capture.get(CV_CAP_PROP_FRAME_HEIGHT);
cout << "Camera properties\n";
cout << "width = " << width << endl <<"height = "<< height << endl;
// Create a matrix to keep the retrieved frame
Mat frame;
// Create a window to show the image
namedWindow ("Capture", CV_WINDOW_AUTOSIZE);
// Create the video writer
VideoWriter video("capture.avi",-1, 30, cvSize((int)width,(int)height) );
// Check if the video was opened
if(!video.isOpened())
{
cerr << "Could not create video.";
return -1;
}
cout << "Press Esc to stop recording." << endl;
// Get the next frame until the user presses the escape key
while(true)
{
// Get frame from capture
capture >> frame;
// Check if the frame was retrieved
if(!frame.data)
{
cerr << "Could not retrieve frame.";
return -1;
}
// Save frame to video
video << frame;
// Show image
imshow("Capture", frame);
// Exit with escape key
if(waitKey(1) == 27)
break;
}
// Exit
return 0;
}
view raw gistfile1.cpp hosted with ❤ by GitHub

Wednesday, April 3, 2013

overloading constructors

#include <iostream>
using namespace std;
class CRectangle {
int x, y;
public:
CRectangle();
CRectangle(int _x, int _y);
void set_values (int,int);
int area () {return (x*y);}
};
void CRectangle::set_values (int a, int b) {
x = a;
y = b;
}
CRectangle::CRectangle():x(0),y(0)
{}
CRectangle::CRectangle(int _x, int _y):x(_x),y(_y)
{}
int main () {
CRectangle rect, rectb, rectc(3,4);
rectb.set_values (5,6);
cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << endl;
cout << "rectc area: " << rectc.area() << endl;
return 0;
}
view raw gistfile1.cpp hosted with ❤ by GitHub
Output:
1
2
3
rect area: 0
rectb area: 30
rectc area: 12

Push structure into vector in C++

Push structure into vector in C++
#include "stdafx.h"
#include <stdio.h>
#include<iostream>
#include<vector>
using namespace std;
//define struct
struct test{
double r;
double theta;
};
int _tmain(int argc, _TCHAR* argv[])
{
test testStr[9];
for (int i =0;i<9;i++)
{
testStr[i].r = 0;
testStr[i].theta = 0;
}
vector<test> strVect; // define a vector of struct
strVect.push_back(testStr[0]);
strVect.push_back(testStr[1]);
printf("test structure %d\n", strVect[0].r);
getchar();
}
view raw gistfile1.cpp hosted with ❤ by GitHub

Monday, April 1, 2013

Latex array stretch

Stretch the row size of a table
\renewcommand\arraystretch{MyValue}% (MyValue=1.0 is for standard spacing)