BogoToBogo
  • Home
  • About
  • Big Data
  • Machine Learning
  • AngularJS
  • Python
  • C++
  • go
  • DevOps
  • Kubernetes
  • Algorithms
  • More...
    • Qt 5
    • Linux
    • FFmpeg
    • Matlab
    • Django 1.8
    • Ruby On Rails
    • HTML5 & CSS

C++ OpenCV

cplusplus_icon.png




Bookmark and Share





bogotobogo.com site search:







OpenCV 3 Tutorial

image & video processing



Installing on Ubuntu 13

Mat(rix) object (Image Container)

Creating Mat objects

The core : Image - load, convert, and save

Smoothing Filters A - Average, Gaussian

Smoothing Filters B - Median, Bilateral





OpenCV 3 image and video processing with Python



OpenCV 3 with Python

Image - OpenCV BGR : Matplotlib RGB

Basic image operations - pixel access

iPython - Signal Processing with NumPy

Signal Processing with NumPy I - FFT and DFT for sine, square waves, unitpulse, and random signal

Signal Processing with NumPy II - Image Fourier Transform : FFT & DFT

Inverse Fourier Transform of an Image with low pass filter: cv2.idft()

Image Histogram

Video Capture and Switching colorspaces - RGB / HSV

Adaptive Thresholding - Otsu's clustering-based image thresholding

Edge Detection - Sobel and Laplacian Kernels

Canny Edge Detection

Hough Transform - Circles

Watershed Algorithm : Marker-based Segmentation I

Watershed Algorithm : Marker-based Segmentation II

Image noise reduction : Non-local Means denoising algorithm

Image object detection : Face detection using Haar Cascade Classifiers

Image segmentation - Foreground extraction Grabcut algorithm based on graph cuts

Image Reconstruction - Inpainting (Interpolation) - Fast Marching Methods

Video : Mean shift object tracking

Machine Learning : Clustering - K-Means clustering I

Machine Learning : Clustering - K-Means clustering II

Machine Learning : Classification - k-nearest neighbors (k-NN) algorithm


Preface
OpenCV.org.png

The OpenCV Reference Manual, Release 2.4.3(pdf)

Meet Computer Vision professionals from OpenCV.org at LinkedIn OpenCV.org







OpenCV Install

In this chapter, I tried to install OpenCV on Windows 8 and Linux Fedora 18 using Visual Studio and Eclipse (Kepler/Juno). But not all of them was satisfactory, and got mixed results.

  1. Visual studio 2012 - 32 bit OpenCV - OK
  2. Visual studio 2012 - 64 bit OpenCV - Not tried
  3. Eclipse Kepler (Windows 8) - 32 bit OpenCV with precompiled - Not satisfactory (needed some additional work)
  4. Eclipse Kepler (Windows 8) - 32 bit OpenCV compiled with MinGW by myself - OK
  5. Eclipse Kepler (Windows 8) - 64 bit OpenCV with MinGW64 - Not successful (I will try again later)
  6. Eclipse Juno on Fedora 18 - 32 bit OpenCV - OK


OpenCV with Visual Studio 2012 (VC11)

Here are the steps to run the first OpenCV program on Windows 8 using Visual Studio 2012 (vc11).

  1. Install OpevCV - C:\opencv
  2. Setup the Windows environment
    1. Set Environment Variable : OPENCV_DIR = C:\OpneCV246\build\
    2. Set Path = %OPENCV_DIR%\x86\vc11\bin
  3. Visual Studio setup
    1. Configuration Properties -> C/C++ -> General -> Additional Include Directories
      $(OPENCV_DIR)\include\opencv;$(OPENCV_DIR)\include
    2. Linker -> General -> Additional Library Directories
      $(OPENCV_DIR)\x86\vc11\lib
    3. Linker ->Input
      Put all the libraries from C:\opencv\build\x86\vc11\lib
      such as opencv_calib3d246.lib, opencv_calib3d246d.lib ...
  4. Run the code shown below, then we'll have the picture on OpenCV window.
  5. Note: I had several errors but most of them were related to some mistakes made during the steps listed here. So, if something wrong during the compile/run, we need to check if we got all the steps right rather than trying to fix obscure errors.
    I'll try x64 again later.
#include <cv.h>
#include <highgui.h>

int main()
{
          IplImage* img = cvLoadImage("C:/MyOpenCV/Images/MyPic.png");
          cvNamedWindow("MyOpenCV");
          cvShowImage("MyOpenCV", img);
          cvWaitKey(0);
          cvDestroyWindow("MyOpenCV");
          cvReleaseImage(&img;);
          return 0;
}

Compile and run, then we get the picture as below.


OpenCV_First_pic.png



OpenCV with Eclipse Kepler and MinGW on Windows 8

I had some issues on this. Probably related to 32/64. My Machine is x64 amd but I ended up using x86.

  1. Install Eclipse Kepler.
  2. CDT plugin
    1. Help->Install New Sofware...
    2. In Install popup, put this url : http://download.eclipse.org/tools/cdt/releases/kepler
      which we can get from http://www.eclipse.org/cdt/downloads.php.
    3. Click "Add...", then type in this tag: CDT_8_2_0
  3. Install MinGW
    In my case, I put it under "eclipse" directory.
  4. Install OpenCV2.4.6.
    1. Project Properties->C/C++ General->Paths and Symbols
    2. In Includes tab, add this: C:\opencv\build\include
    3. In Libraries tab, add this:
      libopencv_core246, libopencv_highgui246, libopencv_imgproc246
    4. In Library Paths tab, add this: C:\opencv\build\x86\mingw\lib
  5. System Environment setting:
    C:\eclipse\MinGW\bin;C:\opencv\build\x86\mingw\bin;

The sample program looks like this:

#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <iostream>

using namespace std;
using namespace cv;

int main( int argc, char** argv )
{

  Mat image;
  image = imread( argv[1], CV_LOAD_IMAGE_COLOR );

  if( argc != 2 || !image.data )
    {
      printf( "No image data \n" );
      return -1;
    }

  namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
  imshow( "Display Image", image );

  waitKey(0);

  return 0;
}

Build script:

13:31:57 **** Rebuild of configuration Debug for project DisplayImage ****
Info: Internal Builder is used for build
g++ "-IC:\\opencv\\build\\include" -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\DisplayImage.o" "..\\src\\DisplayImage.cpp" 
g++ "-LC:\\opencv\\build\\x86\\mingw\\lib" -o DisplayImage.exe "src\\DisplayImage.o" -llibopencv_core246 -llibopencv_highgui246 -llibopencv_imgproc246 

13:32:01 Build Finished (took 4s.116ms)

Run it either from IDE or via prompt window using flower.png:

$ DisplayImage C:\TEST\flower.png

DisplayImage.png

Note

  1. Could not make 64bit working.
  2. I needed some help to setup environment:
    1. Run mingw64env.cmd file whose contents looks like this:
      @title MinGW-w64 64-bit GCC build environment
      
      @echo Setting up environment for MinGW-w64 GCC 64-bit...
      
      @set PATH=%CD%\bin;%PATH%
      
      @call cmd
             
    2. After that, run the following command on the prompt window:
      gcc -Wl,--add-stdcall-alias -I"C:\Program Files\Java\jdk1.7.0_21\include" -shared -o macro_NativeMacropaco-PC.dll macro_NativeMacro.c -m64
            



OpenCV - with CMake & MinGW

Since I had numerous issues with pre-compiled version of OpenCV, in this section, I'll build it using CMake. The detailed process is well documented in stackoverflow.

  1. Download and install MinGW and add to the system PATH with C:/eclipse/MinGW/bin
  2. Download cmake from http://www.cmake.org and install it
  3. Download OpenCV2.4.6 Windows version
  4. Install/unzip Opencv to C:\OpenCV246\ (README,index.rst and CMakeLists.txt are there with all subfolders)
  5. Run CMake GUI tool, then
  6. Choose C:\OpenCV246\ as source
  7. Choose the destination, C:\OpenCV246MinGW\x86 where to build the binaries
  8. Press Configure button, choose MinGW Makefiles as the generator. There are some red highlights in the window, choose options as you need.
  9. Press the Configure button again. Configuring is now done.
  10. Press the Generate button.

    cMakeGui.png
  11. Exit the program when the generating is done.
  12. Exit the Cmake program.
  13. Run the command line mode (cmd.exe) and go to the destination directory C:\OpenCV246MinGW\x86
  14. Type mingw32-make. You will see a progress of building binaries. If the command is not found, you must make sure that the system PATH is added with C:/eclipse/MinGW/bin. The build continues according the chosen options to a completion.
  15. Add the destination's bin directory, C:\OpenCV246MinGW\x86\bin, to System PATH
  16. RESTART COMPUTER
  17. Go to the Eclipse CDT IDE, create a C++ program using the sample OpenCV code.
  18. Go to Project > Properties > C/C++ Build > Settings > GCC C++ Compiler > Includes, and add the source OpenCV folder C:\OpenCV246\build\include
  19. Go to Project > Properties > C/C++ Build > Settings > MinGW C++ Linker > Libraries, and add to the Libraries (-l) one by one: opencv_calib3d246 opencv_contrib246 opencv_core246 opencv_features2d246 opencv_flann246 opencv_gpu246 opencv_highgui246 opencv_imgproc246 opencv_legacy246 opencv_ml246 opencv_nonfree246 opencv_objdetect246 opencv_photo246 opencv_stitching246 opencv_video246 opencv_videostab246
  20. Add the built OpenCV library folder, C:\OpenCV246MinGW\x86\lib to Library search path (-L).

References on MinGW64: http://qt-project.org/wiki/MinGW-64-bit.




OpenCV - Eclipse Juno with CDT plugin on Fedora

OpenCV install: we need to install opencv-devel as well.

  1. opencv.x86_64 0:2.4.5-1.fc18: "sudo yum install opencv"
  2. opencv-devel.x86_64 0:2.4.5-1.fc18: "sudo yum install opencv-devel"

Eclipse

  1. Go to Project->Properties
  2. In C/C++ Build, click on Settings. At the right, choose the Tool Settings Tab. Here we will enter the headers and libraries info:
    1. To find opencv files: type in "pkg-config --cflags opencv", then in my system, it is "-I/usr/include/opencv"
    2. In GCC C++ Compiler, go to Includes. In Include paths(-l) we should include the path of the folder where opencv was installed. In my system, this is /usr/include/opencv.
    3. Now go to GCC C++ Linker, there we have to fill two spaces:
      First in Library search path (-L) we have to write the path to where the opencv libraries reside, in my case the path is:
      /usr/lib64
      
      Then in Libraries(-l) add the OpenCV libraries that we may need. Usually just the 3 first on the list below are enough (for simple applications):
      opencv_core opencv_imgproc opencv_highgui opencv_ml opencv_video opencv_features2d opencv_calib3d opencv_objdetect opencv_contrib opencv_legacy opencv_flann

      To find out where the libs are:

      $ pkg-config --libs opencv
      -lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab  
      

    4. Hit OK.
  3. Now, the project should be ready to be built. For this, go to Project->Build all.




User Guide - Image Operations

This is based on Operations with images.

Load an image from a file:

Mat img = imread(filename, CV_LOAD_IMAGE_COLOR);

If you read a jpg (or png) file, a 3 channel image is created by default. If you need a grayscale image, use:

Mat img = imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
...
namedWindow( "GrayscaleImage", CV_WINDOW_AUTOSIZE );
imshow( "GrayscaleImage", image );

flower_grayscale.png

More to come...







Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization

YouTubeMy YouTube channel

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong






Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong






C++ Tutorials

C++ Home

Algorithms & Data Structures in C++ ...

Application (UI) - using Windows Forms (Visual Studio 2013/2012)

auto_ptr

Binary Tree Example Code

Blackjack with Qt

Boost - shared_ptr, weak_ptr, mpl, lambda, etc.

Boost.Asio (Socket Programming - Asynchronous TCP/IP)...

Classes and Structs

Constructor

C++11(C++0x): rvalue references, move constructor, and lambda, etc.

C++ API Testing

C++ Keywords - const, volatile, etc.

Debugging Crash & Memory Leak

Design Patterns in C++ ...

Dynamic Cast Operator

Eclipse CDT / JNI (Java Native Interface) / MinGW

Embedded Systems Programming I - Introduction

Embedded Systems Programming II - gcc ARM Toolchain and Simple Code on Ubuntu and Fedora

Embedded Systems Programming III - Eclipse CDT Plugin for gcc ARM Toolchain

Exceptions

Friend Functions and Friend Classes

fstream: input & output

Function Overloading

Functors (Function Objects) I - Introduction

Functors (Function Objects) II - Converting function to functor

Functors (Function Objects) - General



Git and GitHub Express...

GTest (Google Unit Test) with Visual Studio 2012

Inheritance & Virtual Inheritance (multiple inheritance)

Libraries - Static, Shared (Dynamic)

Linked List Basics

Linked List Examples

make & CMake

make (gnu)

Memory Allocation

Multi-Threaded Programming - Terminology - Semaphore, Mutex, Priority Inversion etc.

Multi-Threaded Programming II - Native Thread for Win32 (A)

Multi-Threaded Programming II - Native Thread for Win32 (B)

Multi-Threaded Programming II - Native Thread for Win32 (C)

Multi-Threaded Programming II - C++ Thread for Win32

Multi-Threaded Programming III - C/C++ Class Thread for Pthreads

MultiThreading/Parallel Programming - IPC

Multi-Threaded Programming with C++11 Part A (start, join(), detach(), and ownership)

Multi-Threaded Programming with C++11 Part B (Sharing Data - mutex, and race conditions, and deadlock)

Multithread Debugging

Object Returning

Object Slicing and Virtual Table

OpenCV with C++

Operator Overloading I

Operator Overloading II - self assignment

Pass by Value vs. Pass by Reference

Pointers

Pointers II - void pointers & arrays

Pointers III - pointer to function & multi-dimensional arrays

Preprocessor - Macro

Private Inheritance

Python & C++ with SIP

(Pseudo)-random numbers in C++

References for Built-in Types

Socket - Server & Client

Socket - Server & Client 2

Socket - Server & Client 3

Socket - Server & Client with Qt (Asynchronous / Multithreading / ThreadPool etc.)

Stack Unwinding

Standard Template Library (STL) I - Vector & List

Standard Template Library (STL) II - Maps

Standard Template Library (STL) II - unordered_map

Standard Template Library (STL) II - Sets

Standard Template Library (STL) III - Iterators

Standard Template Library (STL) IV - Algorithms

Standard Template Library (STL) V - Function Objects

Static Variables and Static Class Members

String

String II - sstream etc.

Taste of Assembly

Templates

Template Specialization

Template Specialization - Traits

Template Implementation & Compiler (.h or .cpp?)

The this Pointer

Type Cast Operators

Upcasting and Downcasting

Virtual Destructor & boost::shared_ptr

Virtual Functions



Programming Questions and Solutions ↓

Strings and Arrays

Linked List

Recursion

Bit Manipulation

Small Programs (string, memory functions etc.)

Math & Probability

Multithreading

140 Questions by Google



Qt 5 EXPRESS...

Win32 DLL ...

Articles On C++

What's new in C++11...

C++11 Threads EXPRESS...

Go Tutorial

OpenCV...








Contact

BogoToBogo
contactus@bogotobogo.com

Follow Bogotobogo

About Us

contactus@bogotobogo.com

YouTubeMy YouTube channel
Pacific Ave, San Francisco, CA 94115

Pacific Ave, San Francisco, CA 94115

Copyright © 2024, bogotobogo
Design: Web Master