OpenCV. Basics. Department of Electrical Engineering and Computer Science

Similar documents
Multimedia Retrieval Exercise Course 2 Basic Knowledge about Images in OpenCV

Visual Computing in OpenCV Lecture 2: Dense Matrices

OpenCV introduction. Mašinska vizija, 2017.

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Colorado School of Mines. Computer Vision. Professor William Hoff Dept of Electrical Engineering &Computer Science.

Introduction to OpenCV

1. Introduction to the OpenCV library

Computer and Machine Vision

CSCI 512 / EENG 512 Computer Vision Spring Lab 6. February 17, 2016

Interaction Technology

CS 376b Computer Vision

ECE 661 HW_4. Bharath Kumar Comandur J R 10/02/2012. In this exercise we develop a Harris Corner Detector to extract interest points (such as

GeoInt Accelerator Platform to start developing with GPUs GPU access, GPU-accelerated apps and libraries Register to learn more:

PROGRAMMING IN C++ CVIČENÍ

Colorado School of Mines. Computer Vision. Professor William Hoff Dept of Electrical Engineering &Computer Science.

OpenCV Introduction. CS 231a Spring April 15th, 2016

Multimedia Retrieval Exercise Course 2 Basic of Image Processing by OpenCV

Intel Array Building Blocks (Intel ArBB) Technical Presentation

CSCE574 Robotics Spring 2014 Notes on Images in ROS

CSE 333. Lecture 10 - references, const, classes. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

LAB SESSION 1 INTRODUCTION TO OPENCV

Image Processing (1) Basic Concepts and Introduction of OpenCV

OpenCV. OpenCV Tutorials OpenCV User Guide OpenCV API Reference. docs.opencv.org. F. Xabier Albizuri

Check the Desktop development with C++ in the install options. You may want to take 15 minutes to try the Hello World C++ tutorial:

Common Misunderstandings from Exam 1 Material

PART I - A very brief introduction

Array Accessing and Strings ENGR 1181 MATLAB 3

- It computes the Standard Deviation by calculating the difference of each channel (R,G,B and A) of a pixel.

Robot Vision Systems Lecture 3: Methods for Dense Matrices in OpenCV

Homework 4. Any questions?

C, C++, Fortran: Basics

Array Accessing and Strings ENGR 1187 MATLAB 3

ECE661 Computer Vision : HW2

Homework 3 Eye Detection

Communication With the Outside World

Chapter 2. Procedural Programming

XPM 2D Transformations Week 2, Lecture 3

C++ Namespaces, Exceptions

ECE 661 HW 1. Chad Aeschliman

Introduction to OpenCV. Marvin Smith

XPM 2D Transformations Week 2, Lecture 3

Motivation was to facilitate development of systems software, especially OS development.

C++ Basics. Brian A. Malloy. References Data Expressions Control Structures Functions. Slide 1 of 24. Go Back. Full Screen. Quit.

CSE 333 Lecture 9 - intro to C++

Functions. Functions in C++ Calling a function? What you should know? Function return types. Parameter Type-Checking. Defining a function

Announcements. Lecture 04b Header Classes. Review (again) Comments on PA1 & PA2. Warning about Arrays. Arrays 9/15/17

SHADER PROGRAMMING. Based on Jian Huang s lecture on Shader Programming

Image Steganalysis Image Steganography

CE221 Programming in C++ Part 1 Introduction

CSCI-1200 Data Structures Fall 2017 Lecture 12 Advanced Recursion

Twister: Language Reference Manual

ECE Fall 2018, Test 3

Motivation was to facilitate development of systems software, especially OS development.

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

Computer Science II Lecture 2 Strings, Vectors and Recursion

Vector and Free Store (Vectors and Arrays)

Evolution of programming languages

Lecture 2 More C++ Jon Macey

Java Basic Syntax. Java vs C++ Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology

Cours de C++ Introduction

CSCI-1200 Data Structures Spring 2015 Lecture 2 STL Strings & Vectors

C++ Programming Fundamentals

Transportation Informatics Group, ALPEN-ADRIA University of Klagenfurt. Transportation Informatics Group University of Klagenfurt 12/24/2009 1

Looping and Counting. Lecture 3 Hartmut Kaiser hkaiser/fall_2012/csc1254.html

QStringView. everywhere. Marc Mutz, Senior Software Engineer at KDAB

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

typedef Labeling<unsigned char,short> LabelingBS; typedef Labeling<unsigned char,short>::regioninfo RegionInfoBS;

ECE 661 HW6 Report. Lu Wang 10/28/2012

CSCI-1200 Data Structures Fall 2017 Lecture 2 STL Strings & Vectors

Intermediate Programming, Spring 2017*

C++ Primer. CS 148 Autumn

Introduction. Program construction in C++ for Scientific Computing. School of Engineering Sciences. Introduction. Michael Hanke.

Midterm Exam #2 Spring (1:00-3:00pm, Friday, March 15)

PARALUTION - a Library for Iterative Sparse Methods on CPU and GPU

Introduction to C++ Systems Programming

Computer Graphics (4731) Lecture 4: 2D Graphics Systems (Drawing Polylines, tiling, & Aspect Ratio)

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

CSE 333 Lecture 2 - arrays, memory, pointers

Contents. Custom data type struct data type Fixed length memory Alias data type

Dynamic memory allocation

Programmazione. Prof. Marco Bertini

Lecture 6: Parallel Matrix Algorithms (part 3)

CS242 COMPUTER PROGRAMMING

Professor Terje Haukaas University of British Columbia, Vancouver C++ Programming

CS

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

EMBEDDED SYSTEMS PROGRAMMING Language Basics

Suppose that you want to use two libraries with a bunch of useful classes and functions, but some names collide:

Functions. Arizona State University 1

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

Biostatistics 615/815 Lecture 13: Programming with Matrix

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

Generalizing vesselness with respect to dimensionality and shape Release 1.00

CSE 333 Lecture smart pointers

primitive arrays v. vectors (1)

III. Classes (Chap. 3)

stanford hci group / cs377s Lecture 8: OpenCV Dan Maynes-Aminzade Designing Applications that See

Introduction to Internet of Things Prof. Sudip Misra Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Transcription:

OpenCV Basics 1

OpenCV header file OpenCV namespace OpenCV basic structures Primitive data types Point_ Size_ Vec Scalar_ Mat Basics 2

OpenCV Header File #include <opencv2/opencv.hpp>.hpp is a convention for C++ language header files OpenCV has a modular structure that includes several shared or static libraries opencv.hpp is like an umbrella file that includes some of the basic modules: core: core functionality highgui: high level GUI imgproc: image processing It is ok to manually include each individual module #include <opencv2/core/core.hpp> 3

OpenCV Namespace All the OpenCV classes and functions are placed into the cv namespace. So in your code, Use the cv:: specifier, or Use the using namespace cv; directive. Some of the current or future OpenCV names may conflict with C++ STL or other libraries. In this case, use the explicit namespace specifiers to resolve the name conflicts: cv::log vs std::log cv::max vs std::max 4

Primitive data types: OpenCV Basic Structures Unsigned char Bool Signed char Unsigned short Signed short Int Float Double Or a tuple of values of one of these types 5

OpenCV Basic Structures Point_, template class for 2D points Members: x, y Type aliases: Examples: 6

OpenCV Basic Structures Size_, template class for specifying the size of an image or rectangle. Members: width, height Type aliases: Example: cv::size sz(640, 480); // a VGA image size 7

OpenCV Basic Structures Vec, template class for short numerical vectors Commonly used in OpenCV to describe pixel types of multi channel arrays. See Mat for examples Use operator[] to access the elements of Vec Type aliases: 8

OpenCV Basic Structures Scalar_, template class for a 4 element Vec Widely used in OpenCV to pass pixel values Type alias: Example: cv::scalar color(0, 0, 255); // Red. OpenCV s default is BGR 9

OpenCV Basic Structures Mat, OpenCV C++ n dimensional dense array class It is used to represent images and matrices. It is a header that points to a block of data which is allocated either automatically or by the user. It is managed via a reference counting mechanism such that the array data is deallocated when no one points to it. mat1 mat1 mat2 RC=3 mat2 RC=0 Deallocated mat3 mat3 10

OpenCV Basic Structures Any primitive type can be defined by an identifier in the form: CV_<bitDepth>{U S F}(C<numberOfChannels>) CV_8U CV_8UC2 CV_32FC4 http://ninghang.blogspot.com/2012/11/list of mat type in opencv.html 11

OpenCV Mat Creation & Initialization Mat constructors mostly used: 12

OpenCV Mat Creation & Initialization OpenCV treats matrices in row major order, while MatLab is column major. 13

OpenCV Mat Element access: double elem = M.at<double>(row, col) * 6.0; M.at<cv::Vec3b>(row, col) = cv::vec3b(255,255,255); Row access: M.row(3) = M.row(3) + M.row(5) * 3; Expressions: 14

Optional for VS User: Image Watch Image Watch is a Visual Studio plug in to visualize in memory images while debugging an application. http://docs.opencv.org/3.1.0/d4/d14/tutorial_windows_visua l_studio_image_watch.html 15

/* OpenCV Basics */ Example // Include this if using Visual Studio //#include <stdafx.h> #include <opencv2/opencv.hpp> int main(int argc, char* argv[]) { // Point_ std::cout << std::endl << "Point_" << std::endl; cv::point2f a(0.5f, 0.f), b(0.f, 0.6f); cv::point pt = (a + b) * 10.f; std::cout << "a = (" << a.x << ", " << a.y << ")" << std::endl; std::cout << "pt = " << pt << std::endl; if (a == b) std::cout << "a==b" << std::endl; else std::cout << "a!=b" << std::endl; // Size_ std::cout << std::endl << "Size_" << std::endl; cv::mat image = // Read an image. cv::imread("c:/sw/opencv/sources/opencv/samples/data/lena.jpg"); if (image.empty()) { std::cout << "Hey! Can't read the image!" << std::endl; system("pause"); return EXIT_FAILURE; } cv::size sz(image.cols, image.rows); std::cout << "sz = " << sz << ", area = " << sz.area() << std::endl; 16

Example // Vec std::cout << std::endl << "Vec" << std::endl; cv::vec3d vx(1, 0, 0), vy(0, 1, 0), v, vz(0, 0, 0); v = vx.dot(vy); // Dot product vz[2] = 1; // Modify the 3rd element to be 1 std::cout << "v = " << v << std::endl; std::cout << "vz = " << vz << std::endl; // Scalar_ std::cout << std::endl << "Scalar_" << std::endl; cv::scalar redscalar(0, 0, 255); cv::rectangle(image, pt, pt * 80, redscalar, 3); std::cout << "See the red rectangle on the output image." << std::endl; // Mat std::cout << std::endl << "Mat" << std::endl; cv::scalar bluescalar(255, 0, 0); int typeimage = image.type(); cv::mat doodle(sz, typeimage, bluescalar); std::cout << "Image Type: " << typeimage << " (Look up the type mapping number in the lecture slides)" << std::endl; if (typeimage == CV_8UC3) std::cout << "Image is an 8 bit unsigned char 3 channel matrix." << std::endl; else std::cout << "Image is NOT an 8 bit unsigned char 3 channel matrix." << std::endl; 17

Example cv::vec3b greenvec(0, 255, 0); // use row & col to access an element for (int row = 100; row < 200; row++) for (int col = 100; col < 200; col++) doodle.at<cv::vec3b>(row, col) = greenvec; cv::vec3b orangevec(0, 127, 255); // use Point to access an element for (int y = 200; y < doodle.rows / 2; y++) for (int x = 200; x < doodle.cols / 2; x++) doodle.at<cv::vec3b>(cv::point(x, y)) = orangevec; cv::mat imagedoodled; imagedoodled = image + doodle; // This will not work as expected because.row() only forms a header. imagedoodled.row(300) = doodle.row(1); // This is recommended. doodle.row(1).copyto(imagedoodled.row(300)); cv::namedwindow("image"); cv::namedwindow("doodle"); cv::namedwindow("imagedoodled"); // Show the image in the window cv::imshow("image", image); cv::imshow("doodle", doodle); cv::imshow("imagedoodled", imagedoodled); // Wait until a keypress cv::waitkey(0); } return EXIT_SUCCESS; 18