Image Processing (1) Basic Concepts and Introduction of OpenCV

Similar documents
Image Processing (1) Basic Concepts and Introduction of OpenCV

ARTIFICIAL INTELLIGENCE AND PYTHON

Image Processing (2) Point Operations and Local Spatial Operations

ModBot Software Documentation 4CAD April 30, 2018

Python for Scientists

Introduction to Computer Vision Laboratories

Interactive Mode Python Pylab

An Implementation on Object Move Detection Using OpenCV

Programming for Non-Programmers

INTRODUCTION TO DATA VISUALIZATION WITH PYTHON. Working with 2D arrays

HW0 v3. October 2, CSE 252A Computer Vision I Fall Assignment 0

PYTHON FOR MEDICAL PHYSICISTS. Radiation Oncology Medical Physics Cancer Care Services, Royal Brisbane & Women s Hospital

MS6021 Scientific Computing. TOPICS: Python BASICS, INTRO to PYTHON for Scientific Computing

Scientific Computing: Lecture 1

Digital Image Fundamentals. Prof. George Wolberg Dept. of Computer Science City College of New York

Computer Vision

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

Programming for Engineers in Python

Introduction to Python

LABORATORY OF DATA SCIENCE. Python & Spyder- recap. Data Science & Business Informatics Degree

A Tutorial on VLFeat

Multimedia Retrieval Exercise Course 2 Basic Knowledge about Images in OpenCV

Introduction to Machine Learning. Useful tools: Python, NumPy, scikit-learn

tutorial : modeling synaptic plasticity

The Python interpreter

More on Images and Matlab

Formal Proposal (Special Sensor) [NOTE: Jump to Experimental Layout & Result Section] Si-Fo. The Universal Signs Follower.

Multimedia Retrieval Exercise Course 2 Basic of Image Processing by OpenCV

Week Two. Arrays, packages, and writing programs

CME 193: Introduction to Scientific Python Lecture 1: Introduction

CITS 4402 Computer Vision

Programming and Pictures: Computational Photography and Applications to Math and Physics. GISA 2016 Session 131 Marist School Christopher Michaud

Scientific Computing with Python. Quick Introduction

Matplotlib Python Plotting

1 CSE 252A Computer Vision I Fall 2017

Raspberry Pi Using Open CV which Has The Installing,making Programs And Performance

Lesson 2: Preparation

python 01 September 16, 2016

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15

Introduction to Wavelets

Episode 1 Using the Interpreter

PS6-DCT-Soln-correction

CpSc 101, Fall 2015 Lab7: Image File Creation

[Programming Assignment] (1)

Arduino 101 AN INTRODUCTION TO ARDUINO BY WOMEN IN ENGINEERING FT T I NA A ND AW E S O ME ME NTO R S

Scientific Computing using Python

LECTURE 22. Numerical and Scientific Computing Part 2

Python for Earth Scientists

OpenCV. Rishabh Maheshwari Electronics Club IIT Kanpur

INTRO TO PYTHON FOR DATA SCIENCE. Functions

Introduction to Programming with Python 3, Ami Gates. Chapter 1: Creating a Programming Environment

Object Move Controlling in Game Implementation Using OpenCV

Numerical Calculations

Lecture #3. MATLAB image processing (cont.) Histograms Mathematics of image processing Geometric transforms Image Warping.

Scientific Python. 1 of 10 23/11/ :00

Pandas and Friends. Austin Godber Mail: Source:

A. Python Crash Course

Sunpy Python for Solar Physics Juan Carlos Martínez Oliveros

MATPLOTLIB. Python for computational science November 2012 CINECA.

Week - 01 Lecture - 04 Downloading and installing Python

Face and Nose Detection in Digital Images using Local Binary Patterns

Mobile Camera Based Text Detection and Translation

The SciPy Stack. Jay Summet

Python Programming. Hans-Petter Halvorsen.

(DRAFT) PYTHON FUNDAMENTALS II: NUMPY & MATPLOTLIB

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:

Python for Science and Engineering

5 File I/O, Plotting with Matplotlib

Filtering (I) Agenda. Getting to know images. Image noise. Image filters. Dr. Chang Shu. COMP 4900C Winter 2008

Cross-platform Mobile Document Scanner

PyRealSense Documentation

CS4442/9542b Artificial Intelligence II prof. Olga Veksler

Filtering (I) Dr. Chang Shu. COMP 4900C Winter 2008

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

CS4442/9542b Artificial Intelligence II prof. Olga Veksler

CS1 Lecture 2 Jan. 16, 2019

IMAGE PROCESSING AND OPENCV. Sakshi Sinha Harshad Sawhney

AMath 483/583 Lecture 2. Notes: Notes: Homework #1. Class Virtual Machine. Notes: Outline:

AMath 483/583 Lecture 2

Python for Astronomers. Week 1- Basic Python

Lab 1 - Basic ipython Tutorial (EE 126 Fall 2014)

OREKIT IN PYTHON ACCESS THE PYTHON SCIENTIFIC ECOSYSTEM. Petrus Hyvönen

Lecture 15: High Dimensional Data Analysis, Numpy Overview

Computer and Machine Vision

Introduction to Python for Scientific Computing

Manual_implementation_of_the_Mersenne_twister_PseudoRandom_N

Computer and Machine Vision

Progress Report of Final Year Project

Scientific Programming. Lecture A08 Numpy

cosmos_python_ Python as calculator May 31, 2018

Jython. secondary. memory

Segmentation I: Edges and Lines

IMPLEMENTATION OF COMPUTER VISION TECHNIQUES USING OPENCV

Computer Vision & Deep Learning

Project #1 Seamcarve

Using the Matplotlib Library in Python 3

Free Software Alternatives to Commercial Math Software

CS 110 Computer Architecture. Lecture 2: Introduction to C, Part I. Instructor: Sören Schwertfeger.

Introduction to OpenCV. Marvin Smith

Transcription:

Intelligent Control Systems Image Processing (1) Basic Concepts and Introduction of OpenCV Shingo Kagami Graduate School of Information Sciences, Tohoku University swk(at)ic.is.tohoku.ac.jp http://www.ic.is.tohoku.ac.jp/ja/swk/

Basic Motivation e.g. Vision-based Control of Robots image acquisition (will be covered in TESP2018 lecture; Aug 7) image processing robot control (have been covered by Prof. Hashimoto s part) 2

Schedule (tentative) We focus on theories and implementations of basic visual tracking methods, which give foundations of image processing for visual servoing. July 6: Intro: Image Processing Programming July 13: Image Processing Basics (Filtering, Colors) July 20: Object Tracking (1) July 27: Object Tracking (2) Aug 9: Final Report due 3

Digital Images Analog distribution of light intensity 2-D discretization (into pixels) quantization of intensity (ADC) A digital image: 2-D array of pixel values Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2016 (2) 5

Pixel Value (analog) light intensity; illuminance; voltage (digital) pixel value; intensity value; gray level; grayscale value 255 0...... 128 quantized into [0, 255] integer: 8-bit grayscale image cf. binary image (= 1-bit grayscale) Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2016 (2) 6

Expression of a Digital Image M N pixels digital image: { F x,y }, x = 0, 1,, M-1, y = 0, 1,, N-1 Pixel value at (x, y): F x,y F 0,0 F 1,0 F 2,0 F M-1,0 F 0,1 F 1,1 F 2,1 F M-1,1 x axis y axis F 0,N-1 F 1,N-1 F 2,N-1 F M-1,N-1 Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2016 (2) 7

Example in C M #define M 640 #define N 480 unsigned char image[m * N]; N 8-bit image[m * y + x] = 30; // F(x, y) := 30 2-D array is not convenient in C (e.g. not flexible in sizes) 1-D array is often preferred Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2016 (2) 8

A Simple Example Code in C binarization (or thresholding) #define M 640 #define N 480 #define THRESHOLD 128 unsigned char image[m * N]; int i, j; for (j = 0; j < N; j++) { for (i = 0; i < M; i++) { if (image[m * j + i] >= THRESHOLD) { image[m * j + i] = 255; } else { image[m * j + i] = 0; } } } Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2016 (2) 9

Image Processing Libraries Writing image processing programs by your own in this way is possible, but cumbersome How do you read image/video from a file or a camera? How do you display images? There are many libraries or toolkits convenient for image processing, and using some of them is a good choice In this lecture, we use OpenCV for Python note: our goal is not to show you detailed features of OpenCV, but to lecture basic concepts of image processing by using OpenCV as a tool Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2016 (2) 10

OpenCV (Open Computer Vision Library) http://opencv.org/ A de facto standard library in Computer Vision community implements many state-of-the-art algorithms offers simple but easy-to-use I/O and GUI Runs on Windows, Mac, Linux, Android, ios etc. Language: C++ (also supports: python, Java) Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2016 (2) 11

Python https://www.python.org/ A lightweight scripting language easy to write, easy to run becoming popular particularly in scientific data processing a lot of useful modules are available Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2016 (2) 12

Setup (for Windows) A portable package for this class is available (prepared in USB memories). It requires 6 GB disk space. How this package is prepared: Anaconda 5.3 with Python 3.6 for Windows (64 bit) https://www.anaconda.com/ arbitrary folder of your choice Install for just me Destination: C: ic2018 Anaconda3 Uncheck all the Advanced Options In Command Prompt with PATH variable set (see next page): > pip install opencv-python > pip install opencv-contrib-python 13

Running Codes Run Command Prompt (cmd.exe): > set ANACONDA_DIR=C: ic2018 Anaconda3 > set PATH=%ANACONDA_DIR%;%ANACONDA_DIR% Scripts;%ANACOND A_DIR% Library bin;%path% Within this Command Prompt, the installed version of python is active. > cd C: ic2018 sample > python thresh.py > start spyder (Spyder: Tools -> Preferences -> General -> Advanced Settings -> Language) 14

Interactive Execution In IPython window of spyder, you can interactively execute Python codes. cd C:/ic2018/sample/ import cv2 img = cv2.imread('lena.jpg') cv2.imshow('testwin', img) cv2.waitkey(0) Hit any key on the image window to proceed cv2.destroyallwindows() import matplotlib.pyplot as plt plt.imshow(img) # Oops! plt.imshow(cv2.cvtcolor(img, cv2.color_bgr2rgb)) OpenCV uses (Blue, Green, Red) order to encode pixel colors, while matplotlib uses (Red, Green, Blue) 15

Full Python Code for Thresholding (1/2) import cv2 import numpy as np OpenCV module numpy module def threshold_impl(src, thresh, maxval): definition of a function width = src.shape[1] Note that indentation matters height = src.shape[0] dest = np.zeros_like(src) zero-initialized image with the same size as src for j in range(height): for i in range(width): if src[j, i] > thresh: dest[j, i] = maxval else: dest[j, i] = 0 return dest range(n) generates a list [0, 1, 2,, n-1] Indices for pixel access are given in [Y-axis, X-axis] order (= [row, column] order) 16

Full Python Code for Thresholding (2/2) if name == ' main ': An idiom to define main routine input = cv2.imread('lena.jpg', cv2.imread_grayscale) imread() method in cv2 module is called retval, thresh_cv = cv2.threshold(input, 128, 255, cv2.thresh_binary) thresh_np = np.full_like(input, 255) * (input > 128) thresh_my = threshold_impl(input, 128, 255) cv2.imshow("test1", thresh_cv) cv2.imshow("test2", thresh_np) cv2.imshow("test3", thresh_my) cv2.waitkey(0) cv2.destroyallwindows() A function can return multiple values (window name, image) Wait infinitely until any key is pressed, while refreshing the graphics 17

http://www.numpy.org/ Numpy module A de factor standard library for scientific computing with Python numpy array is used to represent images in OpenCV (Note: different from OpenCV in C++, which uses cv::mat class) list in Python is flexible but inefficient x = [0, 1, "apple", [2, 4]] numpy array is efficient because it is a straightforward array with fixed data type import numpy as np x = np.array([[1,2,3], [4,5,6], [7,8,9]]) 18

Video Processing Example: Thresholding if name == ' main ': cap = cv2.videocapture('vtest.avi') #cap = cv2.videocapture(0) while True: retval, input = cap.read() if retval == False: break input = cv2.cvtcolor(input, cv2.color_bgr2gray) thresh_img = threshold_impl(input, 128, 255) cv2.imshow("test", thresh_img) key = cv2.waitkey(30) if key == ord('q'): break cv2.destroyallwindows() images are captured from video file or a camera device (specified by integer index) break if no image is available grayscale image is assumed 19

Just-In-Time Compilation by numba https://numba.pydata.org/ Iterating through the pixels using for loops is extremely slow in Python. Good practice is to use numpy methods thoughtfully, but it is not main focus of this course We use numba module as a workaround: the function is compiled when it is executed for the first time and therefore runs fast for the second time and on from numba import jit @jit def threshold_impl(src, thresh, maxval): 20

Trackbars if name == ' main ': cv2.namedwindow('test') cv2.createtrackbar('thresh', 'test', 128, 255, donothing) cap = cv2.videocapture('vtest.avi') while True: retval, input = cap.read() if retval == False: break input = cv2.cvtcolor(input, cv2.color_bgr2gray) thresh_val = cv2.gettrackbarpos('thresh', 'test') thresh_img = threshold_impl(input, thresh_val, 255) cv2.imshow("test", thresh_img) key = cv2.waitkey(30) if key == ord('q'): break cv2.destroyallwindows() 21

Video Processing Example: Frame Difference input + + + + + + output t 22

Frame Difference if name == ' main ': cap = cv2.videocapture('vtest.avi') retval, prev_frame = getgrayframe(cap) while True: retval, input = getgrayframe(cap) if retval == False: break diff_img = np.uint8((np.int16(input) - np.int16(prev_frame)) / 2 + 128) prev_frame = input cv2.imshow("test", diff_img) key = cv2.waitkey(30) if key == ord('q'): break cv2.destroyallwindows() converted to 16-bit integer image to deal with negative values, and then converted back to 8-bit unsigned integer wait for 30 ms until any key is hit; Break if the pressed key code is that of q 23

Histogram of Pixel Values H(u) 3000 2500 2000 1500 1000 500 0 0 50 100 150 200 250 u where S(u) is a set of pixels having values belonging to the bin u 24

https://matplotlib.org/ Plotting Histogram matplotlib is a plotting library with MATLAB-like interface import matplotlib.pyplot as plt def drawhistogram(src): width = src.shape[1] height = src.shape[0] pix_val_hist = np.zeros(256) for j in range(height): for i in range(width): pix_val_hist[src[j, i]] += 1 plt.bar(range(256), pix_val_hist) abscissa values ordinate values 25

Converting Pixel Values 255 output value 0 p1 p2 input value clip values between 0 and 255 converted_img = np.uint8(np.clip( (255 * (np.int32(input) - vmin)) / (vmax - vmin), 0, 255) ) 26

References Slides and sample codes are available at http://www.ic.is.tohoku.ac.jp/~swk/lecture/ Reference manuals for OpenCV and numpy are in: https://docs.opencv.org/3.4.1/ http://www.numpy.org/ A. Kaehler, G. Bradski: Learning OpenCV 3, O Reilly, 2017. ( 詳解 OpenCV 3, オライリー ジャパン, 2018) Unfortunately, the codes are in C++ 27