OpenCV(C++)
- OpenCV is a great library of computer vision. Many useful filters, image processing algorithms, and tools are implemented.
Useful Links †
- http://opencv.org/
- Documentation for ver. 2.4: http://docs.opencv.org/2.4/
- Documentations of all versions: http://docs.opencv.org/
- Q and A Forum of OpenCV: http://answers.opencv.org/questions/
Simple Examples †
A minimum code would be capturing an image from your USB camera (webcam), and just showing it on a window. Such a code is like this (ignore if(...){...} which is just checking errors):
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <cstdio>
int main(int argc, char **argv)
{
cv::VideoCapture cap(0); // open the default camera
if(argc==2)
{
cap.release();
cap.open(atoi(argv[1]));
}
if(!cap.isOpened()) // check if we succeeded
{
std::cerr<<"no camera!"<<std::endl;
return -1;
}
std::cerr<<"camera opened"<<std::endl;
cv::namedWindow("camera",1);
cv::Mat frame;
for(;;)
{
cap >> frame; // get a new frame from camera
cv::imshow("camera", frame);
int c(cv::waitKey(10));
if(c=='\x1b'||c=='q') break;
// usleep(10000);
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
In OpenCV, an image is stored in a matrix (note that there are three channels of red, green, and blue in color images; OpenCV's matrix stores all these information). There are many build-in matrix operations. The details are: Basic Structures. Simple examples are below.
#include <opencv2/core/core.hpp>
#include <iostream>
using namespace std;
#define print(var) std::cout<<#var"= "<<endl<<(var)<<std::endl
int main(int argc, char**argv)
{
cv::Mat m1(3,3,CV_8UC1), m2(3,3,CV_8UC1), m3;
m1= (cv::Mat_<unsigned char>(3,3)<<1,0,1, 0,0,1, 0,1,1);
m2= (cv::Mat_<unsigned char>(3,3)<<0,1,1, 1,0,1, 0,1,0);
print(m1);
print(m2);
print(m1-m2);
print(m1&m2);
bitwise_xor(m1,m2,m3);
print(m3);
print(sum(m3)[0]);
return 0;
}
#include <opencv2/core/core.hpp>
#include <iostream>
using namespace std;
#define print(var) std::cout<<#var"= "<<endl<<(var)<<std::endl
int main(int argc, char**argv)
{
cv::Mat_<double> m1(3,3);
m1= (cv::Mat_<double>(3,3)<<1,0,1, 0,0,1, 0,1,1);
cv::Vec<double,3> v1;
v1= cv::Vec<double,3>(1,2,3);
print(m1);
print(cv::Mat(v1));
print(m1*cv::Mat(v1));
cv::Vec<double,9> vm2;
cv::Mat_<double> m2(3,3,vm2.val);
m1= (cv::Mat_<double>(3,3)<<1,0,1, 0,0,1, 0,1,1);
cv::Mat(m1.inv()).copyTo(m2);
print(cv::Mat(vm2));
print(m2);
print(m2*2.0);
// print(m2*cv::Vec3b(3,1,0));
print(m2*cv::Mat(v1));
cv::Mat(m2*cv::Mat(v1)).copyTo(v1);
print(cv::Mat(v1));
cv::Vec<double,3> v3(3,2,1);
print(cv::Mat(v1));
print(cv::Mat(v3));
print(cv::Mat(v1+v3));
print(cv::Mat(v1*2.0));
print(norm(v1));
print(cv::Mat(v1*(1.0/norm(v1))));
cv::normalize(v1,v1);
print(cv::Mat(v1));
cv::Matx<double,3,3> m3(1,2,3, 4,5,6, 7,8,9);
m3<<1,2,3, 4,5,6, 7,8,9;
print(cv::Mat(m3));
print(cv::Mat(v3));
print(cv::Mat(m3*v3));
v3= cv::Mat(m3*v3);
print(cv::Mat(v3));
// elemental access
print(cv::Mat(v1));
print(m1);
print(v1(0));
print(v1(1));
print(m1(0,0));
print(m1(0,1));
print(m1(0,2));
return 0;
}
A simplest image processing would be converting the color of an input image to gray scale. Execute the following example: there are two windows; one is an original image, and the other is a gray-scale image. The conversion code is quite simple: just calling a function cvtColor.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
int main(int argc, char **argv)
{
cv::VideoCapture cap(0); // open the default camera
if(argc==2)
{
cap.release();
cap.open(atoi(argv[1]));
}
if(!cap.isOpened()) // check if we succeeded
{
std::cerr<<"no camera!"<<std::endl;
return -1;
}
std::cerr<<"camera opened"<<std::endl;
cv::namedWindow("camera",1);
cv::namedWindow("gray",1);
cv::Mat frame,gray;
cap >> frame;
for(;;)
{
cap >> frame; // get a new frame from camera
cv::cvtColor(frame,gray,CV_BGR2GRAY);
cv::imshow("camera", frame);
cv::imshow("gray", gray);
if(cv::waitKey(10) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
More Examples †
Basic Examples †
Fancy Examples †
- http://www.learnopencv.com/
- http://abhishek4273.com/tag/calcmotiongradient/
- http://opencvexamples.blogspot.com/
Google "OpenCV examples", you will find many examples!