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

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

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

OpenCV. Basics. Department of Electrical Engineering and Computer Science

1 Background and Introduction 2. 2 Assessment 2

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

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

CS 376b Computer Vision

Image Processing (1) Basic Concepts and Introduction of OpenCV

OpenCV. Rishabh Maheshwari Electronics Club IIT Kanpur

EE795: Computer Vision and Intelligent Systems

Section 7.2 Volume: The Disk Method

0 Graphical Analysis Use of Excel

Laboratory of Applied Robotics

Русинович Андрей Сергеевич

Lecture 3 - Intensity transformation

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

EECS490: Digital Image Processing. Lecture #23

Binary Image Processing. Introduction to Computer Vision CSE 152 Lecture 5

ME 120: Impeller Model in Solidworks 1/6

Tutorial 3: Constructive Editing (2D-CAD)

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

CS443: Digital Imaging and Multimedia Binary Image Analysis. Spring 2008 Ahmed Elgammal Dept. of Computer Science Rutgers University

Coordinate Geometry. Coordinate geometry is the study of the relationships between points on the Cartesian plane

Object Move Controlling in Game Implementation Using OpenCV

HOUGH TRANSFORM. Plan for today. Introduction to HT. An image with linear structures. INF 4300 Digital Image Analysis

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

Introduction to OpenCV

Brief 3ds max Shaping Tutorial

1. (10 pts) Order the following three images by how much memory they occupy:

MAT 003 Brian Killough s Instructor Notes Saint Leo University

Assignment 1 - Use OpenCV for camera calibration

EECS490: Digital Image Processing. Lecture #17

Lecture 18 Representation and description I. 2. Boundary descriptors

IN5520 Digital Image Analysis. Two old exams. Practical information for any written exam Exam 4300/9305, Fritz Albregtsen

INTRODUCTION TO COMPUTER GRAPHICS. It looks like a matrix Sort of. Viewing III. Projection in Practice. Bin Sheng 10/11/ / 52

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

morphology on binary images

IMAGE PROCESSING AND OPENCV. Sakshi Sinha Harshad Sawhney

Introduction to Digital Image Processing

CoE4TN4 Image Processing

by modifying the glutinitwindowsize() function you can change the screen size to whatever you please.

S206E Lecture 3, 5/15/2017, Rhino 2D drawing an overview

COMP 175 COMPUTER GRAPHICS. Ray Casting. COMP 175: Computer Graphics April 26, Erik Anderson 09 Ray Casting

BOOLEAN EXPRESSIONS CONTROL FLOW (IF-ELSE) INPUT/OUTPUT. Problem Solving with Computers-I

The feature set you are required to implement in your ray tracer is as follows (by order from easy to hard):

The x coordinate tells you how far left or right from center the point is. The y coordinate tells you how far up or down from center the point is.

Processing of binary images

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Intensity Transformation and Spatial Filtering

In this lecture. Background. Background. Background. PAM3012 Digital Image Processing for Radiographers

Solution Notes. COMP 151: Terms Test

An Implementation on Object Move Detection Using OpenCV

CpSc 101, Fall 2015 Lab7: Image File Creation

Interlude: Solving systems of Equations

SOLIDWORKS: Lesson III Patterns & Mirrors. UCF Engineering

Implementation of Hand Detection based Techniques for Human Computer Interaction

Multimedia Retrieval Exercise Course 2 Basic Knowledge about Images in OpenCV

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); }

ECG782: Multidimensional Digital Signal Processing

How to...create a Video VBOX Gauge in Inkscape. So you want to create your own gauge? How about a transparent background for those text elements?

Camera Models and Image Formation. Srikumar Ramalingam School of Computing University of Utah

Building Detection. Guillem Pratx LIAMA. 18 August BACKGROUND 2 2. IDEA 2

2-D Geometry for Programming Contests 1

3D Geometry and Camera Calibration

Programming Techniques Workshop for Mindstorms EV3. Opening doors to the worlds of science and technology for Oregon s youth

ENG 7854 / 9804 Industrial Machine Vision. Midterm Exam March 1, 2010.

Lab 4. Out: Friday, February 25th, 2005

Geometry. Zachary Friggstad. Programming Club Meeting

EXAM SOLUTIONS. Image Processing and Computer Vision Course 2D1421 Monday, 13 th of March 2006,

CIRCULAR INTERPOLATION COMMANDS

ezimagex2 User s Guide Version 1.0

SPRINGBOARD UNIT 5 GEOMETRY

Introduction to Geometry

Image Processing (1) Basic Concepts and Introduction of OpenCV

Computer Graphics : Bresenham Line Drawing Algorithm, Circle Drawing & Polygon Filling

Image Processing: Final Exam November 10, :30 10:30

JEOPARDY! JEOPARDY JEOPARDY. April 18, Unit 04, Review Jeopardy.notebook $100 $100 $100 $200 $200 $200 $200 $200 $200 $300 $400 $400

Module Contact: Dr R J Lapeer, CMP Copyright of the University of East Anglia Version 1

Real Time Data Plotting

Google SketchUp. and SketchUp Pro 7. The book you need to succeed! CD-ROM Included! Kelly L. Murdock. Master SketchUp Pro 7 s tools and features

(Section 6.2: Volumes of Solids of Revolution: Disk / Washer Methods)

C E N T E R A T H O U S T O N S C H O O L of H E A L T H I N F O R M A T I O N S C I E N C E S. Image Operations II

Announcements. Written Assignment2 is out, due March 8 Graded Programming Assignment2 next Tuesday

Extracting Layers and Recognizing Features for Automatic Map Understanding. Yao-Yi Chiang

COS340A Assignment 1 I Pillemer Student# March 25 th 2007 p1/15

Problem Solving: Storyboards for User Interaction

EDAN30 Photorealistic Computer Graphics. Seminar 2, Bounding Volume Hierarchy. Magnus Andersson, PhD student

Programming Assignment Multi-Threading and Debugging 2

Drawing in 3D (viewing, projection, and the rest of the pipeline)

Assignment 3 Functions, Graphics, and Decomposition

Parametric Representation of Scroll Geometry with Variable Wall Thickness. * Corresponding Author: ABSTRACT 1.

Albert M. Vossepoel. Center for Image Processing

Computer Graphics Prof. Sukhendu Das Dept. of Computer Science and Engineering Indian Institute of Technology, Madras Lecture - 14

Computer Vision: Lecture 3

VC 16/17 TP5 Single Pixel Manipulation

CS 315 Data Structures Fall Figure 1

Today s Learning Objectives

ECG782: Multidimensional Digital Signal Processing

Homework #1. Displays, Image Processing, Affine Transformations, Hierarchical modeling, Projections

CS Operating Systems Lab 3: UNIX Processes

Transcription:

Lab 6 February 17, 2016 NAME NAME This lab assignment should be done in teams of two. Go through the exercises below and show me your results. If you don t finish by the end of today s class, you may show me during the next class. The task for today is to use OpenCV to detect a target composed of 5 contrasting concentric circles (CCCs) in images from a live camera, or video, and compute the pose of the target. The dimensions (inches) of the target pattern are shown in the figure. Create an OpenCV project. If you have an existing OpenCV project, the easiest thing is to copy the entire folder of that project; otherwise follow the steps from the previous lecture to set up a new project. Finding the Target Pattern Enter the following code for the main program. Note after you copy and paste code, it is a good idea to clean up the formatting. You can do this by going to Edit->Advanced->Format Document. /* Detect a 5 CCC target, and find its pose. */ #include <iostream> // This includes everything we need. #include <opencv2/opencv.hpp> // Function prototypes go here. std::vector<cv::point2d> findtargets(cv::mat Image); std::vector<cv::point2d> ordertargets(std::vector<cv::point2d> alltargets); int main(int argc, char* argv[]) { cv::videocapture cap(0); // open the camera //cv::videocapture cap("fiveccc.avi"); // or a video if (!cap.isopened()) { // check if we succeeded printf("error can't open the camera\n"); system("pause");

return EXIT_FAILURE; printf("hit ESC key to quit...\n"); // Loop until no more images are available, or person hits "ESC" key. while (true) { cv::mat imageinput; cap >> imageinput; // get image from camera or video file if (imageinput.empty()) break; // Convert to gray. cv::mat imageinputgray; cvtcolor(imageinput, imageinputgray, cv::color_bgr2gray); // Find all CCC targets. std::vector<cv::point2d> alltargets; alltargets = findtargets(imageinputgray); // Draw dots on all targets found. for (unsigned int i = 0; i < alltargets.size(); i++) { cv::circle(imageinput, alltargets[i], // center 3, // radius cv::scalar(0, 0, 255), // color 1); // negative thickness=filled // Find the 5 CCC target pattern, and put the targets in the order // 1 2 3 // 4 5 std::vector<cv::point2d> orderedtargets; orderedtargets = ordertargets(alltargets); // Optionally label targets in the image. for (unsigned int i = 0; i<orderedtargets.size(); i++) { char szlabel[50]; sprintf_s(szlabel, "%d", i); puttext(imageinput, szlabel, cv::point2d(orderedtargets[i]), cv::font_hershey_plain, // font face 2.0, // font scale cv::scalar(0, 255, 0), // font color 2); // thickness imshow("image", imageinput); // Wait for xxx ms (0 means wait until a keypress) if (cv::waitkey(1) == 27) break; // hit ESC (ascii code 27) to quit return EXIT_SUCCESS;

Add two additional functions to your project. To do this, go to the Solution Explorer panel, right click on Source Files, and select Add->New Item. Name one file findtargets.cpp and the other ordertargets.cpp. The first function finds all CCC targets in the image, using the methods described in the lecture. The other function puts the found targets into the correct order; namely: 0 1 2 3 4 The code for these two functions are given at the end of this document. Copy and paste the code into the files. Run the program and verify that it finds the target pattern and labels the targets in the right order. The output should look like the image below. Finding the Pose Enter the following code in your main loop, after the point where you label the targets, but before displaying the image. Certain places in the text have been overwritten by $$$... you will have to figure out what should go there. if (orderedtargets.size() == 5) { // Calculate the pose. cv::mat rotvec,transvec; bool foundpose = cv::solvepnp( $$$, // model points $$$, // image points K, // intrinsic camera parameter matrix dist, // distortion coefficients rotvec,transvec); // output rotation and translation if (foundpose) { std::vector<cv::point3d> pointsaxes; std::vector<cv::point2d> pointsimage; // Draw the xyz coordinate axes on the image. pointsaxes.push_back(cv::point3d(0, 0, 0)); // origin

pointsaxes.push_back(cv::point3d($$$,$$$,$$$)); pointsaxes.push_back(cv::point3d($$$,$$$,$$$)); pointsaxes.push_back(cv::point3d($$$,$$$,$$$)); // x axis // y axis // z axis cv::projectpoints(pointsaxes, rotvec, transvec, K, dist, pointsimage); line(imageinput, pointsimage[0], pointsimage[1], cv::scalar(0, 0, 255), 2); line(imageinput, pointsimage[0], pointsimage[2], cv::scalar(0, 255, 0), 2); line(imageinput, pointsimage[0], pointsimage[3], cv::scalar(255, 0, 0), 2); Enter the following code prior to the main loop. Certain places in the text have been overwritten by $$$... you will have to figure out what should go there. // Create 3D object model points, in the order // 0 1 2 // 3 4 std::vector<cv::point3d> pointsmodel; pointsmodel.push_back(cv::point3d( 3.7, $$$, 0)); pointsmodel.push_back(cv::point3d($$$, $$$, $$$)); pointsmodel.push_back(cv::point3d($$$, $$$, $$$)); pointsmodel.push_back(cv::point3d($$$, $$$,$$$)); pointsmodel.push_back(cv::point3d($$$, $$$,$$$)); // Camera intrinsic matrix double K_[3][3] = { { 675, 0, 320, { 0, 675, 240, { 0, 0, 1 ; cv::mat K = cv::mat(3, 3, CV_64F, K_).clone(); cv::mat dist = cv::mat::zeros(5, 1, CV_64F); // distortion coeffs Run the program and verify that it displays the coordinate axes correctly. The output should look like the image below. Show the instructor your running program.

Code for the function findtargets : #include <opencv2/opencv.hpp> // Parameters for finding CCC targets. #define DPIXEL 3.0 // max distance between centroids // Find all CCC targets on the image. Return a vector (list) of their locations. std::vector<cv::point2d> findtargets(cv::mat Image) { std::vector<cv::point2d> targets; cv::mat imagethresh; // Do Otsu global thresholding is faster than adaptive thresholding. cv::threshold(image, imagethresh, // output thresholded image 0, // threshold value 255, // output value cv::thresh_otsu cv::thresh_binary_inv); // threshold_type invert //// Do adaptive threshold... this compares each pixel to a local //// mean of the neighborhood. The result is a binary image, where //// dark areas of the original image are now white (1's). //adaptivethreshold(image, // imagethresh, // output thresholded image // 255, // output value where condition met // cv::adaptive_thresh_gaussian_c, // local neighborhood // cv::thresh_binary_inv, // threshold_type invert // 91, // blocksize (any large number) // 0); // a constant to subtract from mean // Apply morphological operations to get rid of small (noise) regions. cv::mat structuringelmt = cv::getstructuringelement(cv::morph_ellipse, cv::size(3, 3)); cv::mat imageopen; morphologyex(imagethresh, imageopen, cv::morph_open, structuringelmt); cv::mat imageclose; morphologyex(imageopen, imageclose, cv::morph_close, structuringelmt); // Now find contours. std::vector<std::vector<cv::point>> contours; std::vector<cv::vec4i> hierarchy; cv::findcontours( imageclose, // input image (is destroyed) contours, // output vector of contours hierarchy, // hierarchical representation cv::retr_ccomp, // retrieve all contours cv::chain_approx_none); // all pixels of each contours // Analyze components and find CCCs. for (unsigned int i1 = 0; i1 < (int)contours.size(); i1++) { int i2 = hierarchy[i1][2]; if (i2 < 0) continue; // See if it has a child inside // Compute centroids of inner and outer regions. cv::moments mu1 = cv::moments(contours[i1]); cv::point2d x1(mu1.m10 / mu1.m00, mu1.m01 / mu1.m00);

cv::moments mu2 = cv::moments(contours[i2]); cv::point2d x2(mu2.m10 / mu2.m00, mu2.m01 / mu2.m00); // Check if centroids coincide. if (norm(x1 x2) > DPIXEL) continue; // Check the "circularity" ratio of the outer region, which is // the ratio of area to perimeter squared: R = 4*pi*A/P^2. // R is 1 for a circle, and pi/4 for a square. double P1 = arclength(contours[i1], true); double A1 = contourarea(contours[i1]); if (4 * 3.1415*A1 / (P1*P1) < 3.1415 / 4) // Let's say that we want our region to be at least as round as a square. continue; // This must be a valid target; add it to the output list. targets.push_back(x1); return targets;

Code for the function ordertargets : #include <opencv2/opencv.hpp> // This function tries to find the 5 target pattern that looks like this // 1 2 3 // 4 5 // It puts the targets in that order, and returns them. std::vector<cv::point2d> ordertargets(std::vector<cv::point2d> alltargets) { std::vector<cv::point2d> orderedtargets; unsigned int i1, i2, i3, i4, i5; unsigned int nccc = alltargets.size(); // Find 3 CCCs that are in a line. double dmin = 1e9; // distance from a CCC to the midpt between points 1,3 double d13 = 1; // the distance between points 1,3 for (unsigned int i = 0; i<nccc; i++) { for (unsigned int j = i + 1; j<nccc; j++) { // Get the mid point between i,j. cv::point2d midpt = (alltargets[i] + alltargets[j]) * 0.5; // Find the CCC that is closest to this midpoint. for (unsigned int k = 0; k<nccc; k++) { if (k == i k == j) continue; double d = norm(alltargets[k] midpt); // distance from midpoint if (d < dmin) { // This is the minimum found so far; save it. dmin = d; i1 = i; i2 = k; i3 = j; d13 = norm(alltargets[i] alltargets[j]); // If the best distance from the midpoint is < 30% of the distance between // the two other points, then we probably have our colinear set. if (dmin / d13 > 0.3) return orderedtargets; // return an empty list /* We have found 3 colinear targets: p1 p2 p3. Now find the one closest to p1; call it p4. */ dmin = 1e9; for (unsigned int i = 0; i<nccc; i++) { if (i!= i1 && i!= i2 && i!= i3) { double d = norm(alltargets[i] alltargets[i1]); if (d < dmin) { dmin = d; i4 = i; if (dmin > 1e7) return orderedtargets; // return an empty list

/* Now find the one closest to p3; call it p5. */ dmin = 1e9; for (unsigned int i = 0; i<nccc; i++) { if (i!= i1 && i!= i2 && i!= i3 && i!= i4) { double d = norm(alltargets[i] alltargets[i3]); if (d < dmin) { dmin = d; i5 = i; if (dmin > 1e7) return orderedtargets; // return an empty list // Now, check to see where p4 is with respect to p1,p2,p3. If the // signed area of the triangle p1 p3 p4 is negative, then we have // the correct order; ie // 1 2 3 // 4 5 // Otherwise we need to switch the order; ie // 3 2 1 // 5 4 // Signed area is the determinant of the 2x2 matrix [ p4 p1, p3 p1 ] cv::vec2d p41 = alltargets[i4] alltargets[i1]; cv::vec2d p31 = alltargets[i3] alltargets[i1]; double m[2][2] = { { p41[0], p31[0],{ p41[1], p31[1] ; double det = m[0][0] * m[1][1] m[0][1] * m[1][0]; // Put the targets into the output list. if (det < 0) { orderedtargets.push_back(alltargets[i1]); orderedtargets.push_back(alltargets[i2]); orderedtargets.push_back(alltargets[i3]); orderedtargets.push_back(alltargets[i4]); orderedtargets.push_back(alltargets[i5]); else { orderedtargets.push_back(alltargets[i3]); orderedtargets.push_back(alltargets[i2]); orderedtargets.push_back(alltargets[i1]); orderedtargets.push_back(alltargets[i5]); orderedtargets.push_back(alltargets[i4]); return orderedtargets;