Top/lib/OpenCV_cpp
English | Japanese
English | Japanese

Menu

  • Top
  • Search 検索
  • Writers 著者リスト
  • Online forum フォーラム
↑

Recent

2021-08-17
  • os/Linux
2019-01-17
  • eq/LulzBot
2018-12-10
  • dev/Motoman
2018-07-05
  • tool/Git
2018-05-02
  • lib/Eigen
  • writer/Ilya_Ardakani
2018-04-26
  • lib/Deformable_Simulators
  • Top
2018-04-23
  • writer/Naoya_Chiba
  • MenuBar
  • writer
2018-04-22
  • lang/Python
2018-04-21
  • lang/C++
2018-04-19
  • Editor(editor)/Assignment
  • Editor(editor)
  • lang/C++/Exercise
2018-04-18
  • SandBox2
  • lang/Python/Exercise
  • soft/FreeCAD
  • lib/Chainer
Access: 2/982 - Editor / Admin

OpenCV(C++)

Akihiko Yamaguchi
Abstract 概要
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):

cv2-capture.cpp

#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.

cv2-mat.cpp

#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;
}

cv2-mat2.cpp

#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.

cv2-convert-to-gray.cpp

#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 †

  • http://docs.opencv.org/2.4/doc/tutorials/tutorials.html
  • http://docs.opencv.org/master/examples.html
↑

Fancy Examples †

  • http://www.learnopencv.com/
    • Code: https://github.com/spmallick/learnopencv
  • http://abhishek4273.com/tag/calcmotiongradient/
  • http://opencvexamples.blogspot.com/

Google "OpenCV examples", you will find many examples!




Last-modified:2018-04-18 (Wed) 04:35:31 (2591d)
Link: Top(2582d) writer/Akihiko_Yamaguchi(2591d)
Site admin: Akihiko Yamaguchi.
Written by: Akihiko Yamaguchi.
System: PukiWiki 1.5.0. PHP 5.2.17. HTML conversion time: 1.361 sec.