ECE 661 HW 1. Chad Aeschliman

Similar documents
C = i. d(x i, ˆx i ) 2 + d( x i, Ĥˆx i) 2

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

Homework - 2. Sriram Karthik Badam. September 6, 2012

ECE661 Computer Vision : HW1

ECE661 Computer Vision : HW2

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

Multimedia Retrieval Exercise Course 2 Basic of Image Processing by OpenCV

OpenCV. Rishabh Maheshwari Electronics Club IIT Kanpur

Homework - 3. Sriram Karthik Badam. September 18, 2012

IMAGE PROCESSING AND OPENCV. Sakshi Sinha Harshad Sawhney

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

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

Homework 3 Eye Detection

Introduction to Languages for Scientific Computing, winter semester 14/15: Final Exam

Multimedia Retrieval Exercise Course 2 Basic Knowledge about Images in OpenCV

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

OpenCV. Basics. Department of Electrical Engineering and Computer Science

ECE 661: Homework #3

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

C programming for beginners

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Discussion 3 Richard Guo Advanced C 01/28/09

Assignment 1 - Use OpenCV for camera calibration

File IO and command line input CSE 2451

Pointers and scanf() Steven R. Bagley

Solutions to Chapter 8

LAB SESSION 1 INTRODUCTION TO OPENCV

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

Introduction to OpenCV

Dynamic memory allocation

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

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Representation of image data

Computer Science & Engineering 150A Problem Solving Using Computers

ECE661 Computer Vision : HW6

ESC101N: Fundamentals of Computing End-sem st semester

Worksheet 4 Basic Input functions and Mathematical Operators

Exercises C-Programming

AMCAT Automata Coding Sample Questions And Answers

CS-211 Fall 2017 Test 1 Version Practice For Test on Oct. 2, Name:

Array Initialization

Fundamental of Programming (C)

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Scan Converting Lines

ECE 5273 HW 2 Solution

Why arrays? To group distinct variables of the same type under a single name.

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

#3. (Recursion) Write a recursive function to compute f(x) = f(x - 1) + f(x - 2) with f(0) = 0 and f(1) = 1.

CS 789 Multiprocessor Programming. Optimizing the Sequential Mandelbrot Computation.

1. Introduction to the OpenCV library

Introduction to OpenCV. Marvin Smith

Interaction Technology

Camera Calibration. Schedule. Jesus J Caban. Note: You have until next Monday to let me know. ! Today:! Camera calibration

CS16 Midterm Exam 1 E01, 10S, Phill Conrad, UC Santa Barbara Wednesday, 04/21/2010, 1pm-1:50pm

ECE 5273 HW 6 Solution

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

ECE264 Summer 2013 Exam 1, June 20, 2013

CS 223: Data Structures and Programming Techniques. Exam 2

Assignment 6. Q1. Create a database of students using structures, where in each entry of the database will have the following fields:

Decision Making and Loops

Overview. By end of the week:

QUIZ: loops. Write a program that prints the integers from -7 to 15 (inclusive) using: for loop while loop do...while loop

Answer all questions. Write your answers only in the space provided. Full marks = 50

Principles of Programming. Chapter 6: Arrays

ECE264 Spring 2013 Final Exam, April 30, 2013

ECE264 Spring 2014 Exam 2, March 11, 2014

UNIT 6. STRUCTURED DATA TYPES PART 1: ARRAYS

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

ECE 5273 HW 1 Solution

ECE264 Fall 2013 Exam 1, September 24, 2013

NIOS II Pixel Display

LABORATORY MANUAL APPLIED NUMERICAL TECHNIQUES AND COMPUTING LAB. ME 321 F

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Arrays and Pointers in C. Alan L. Cox

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Intermediate Programming, Spring 2017*

Lesson 7. Reading and Writing a.k.a. Input and Output

Chapter 6. Introduction to OpenCV

Assignment 1 Clarifications

COP Programming Concepts Spring 1999 CLOSED BOOK Exam #1 100 Points NAME

Subject: Fundamental of Computer Programming 2068

Memory Management. CSC215 Lecture

1 SAT-DANCE-HEULE INTRO 1

Final CSE 131B Spring 2004

Rasterization. COMP 575/770 Spring 2013

Procedures, Parameters, Values and Variables. Steven R. Bagley

CMSC 104 -Lecture 5 John Y. Park, adapted by C Grasso

Monte Carlo Methods. Named by Stanislaw Ulam and Nicholas Metropolis in 1940s for the famous gambling casino in Monaco.

heman Documentation Release r1 Philip Rideout

Understanding Pointers

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

Arrays and Strings. Arash Rafiey. September 12, 2017

Program Organization and Comments

ECE 2035 A Programming Hw/Sw Systems Spring problems, 8 pages Final Exam Solutions 29 April 2015

09 - Designing Surfaces. CSCI-GA Computer Graphics - Fall 16 - Daniele Panozzo

Drawing and Coordinate Systems

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

EXAMINATIONS 2017 TRIMESTER 2

SECTION A TRUE / FALSE QUESTIONS (10 MARKS) (INSTRUCTION: Please answer all 10 questions)

Binary Search Tree 1.0. Generated by Doxygen Mon Jun :12:39

Transcription:

ECE 661 HW 1 Chad Aeschliman 2008-09-09 1 Problem The problem is to determine the homography which maps a point or line from a plane in the world to the image plane h 11 h 12 h 13 x i = h 21 h 22 h 23 x w (1) h 31 h 32 1 where h 33 has been set to 1 without loss of generality and x i and x w are in P 3. Four corresponding sets of coordinates in R 2,(x i k,yi k ) and (xw k,yw k ), k =0,..., 3, are provided. 2 Solution Plugging one set of corresponding equations into 1 gives the relationship x i k h 11 h 12 h 13 x w yk i k = h 21 h 22 h 23 aw k yk waw k 1 h 31 h 32 1 a w k By carrying out the multiplication, dividing out the scale factor a w k, and simplifying we obtain two relationships x i k = x w k h 11 + yk w h 12 + h 13 x i kx w k h 31 x i kyk w h 31 y i k = x w k h 21 + y w k h 22 + h 23 y i kx w k h 31 y i ky w k h 31 Combining the equations resulting from all four correspondences into a matrix equation gives the form x i 0 x w y i 0 y0 w 1 0 0 0 x i 0x w 0 x i 0y0 w h 11 0 0 0 0 x w x i 0 y0 w 1 y0x i w 0 y0y i w 0 h 12 1 x w y1 i 1 y1 w 1 0 0 0 x i 1x w 1 x i 1y w 1 h 13 x i = 0 0 0 x w 1 y1 w 1 y1x i w 1 y1y i w 1 h 21 2 x w y2 i 2 y2 w 1 0 0 0 x i 2x w 2 x i 2y2 w h 22 0 0 0 x w x i 2 y2 w 1 y2x i w 2 y2y i 2 w h 23 3 x w y3 i 3 y3 w 1 0 0 0 x i 3x w 3 x i 3y3 w h 31 0 0 0 x w 3 y3 w 1 y3x i w 3 y3y i 3 w h 32 1

This matrix equation can then be solved in opencv to give H. To construct the corrected image, the goal is to set up a grid of evenly spaced points in the world plane with each point representing one pixel in the corrected image and then mapping this grid of points to the image using H. The extents of the grid are determined by mapping the corners of the image plane to the world plane using H 1. The smallest and largest x w s and y w s form the extents of the grid. The spacing of the grid points is chosen to set the resolution of the corrected image and the convention was chosen to preserve the width of the original image with the height adjusting as necessary. In general a grid point does not map to a particular pixel in the original image so linear interpolation was used. 3 Results 8 pairs of original and corrected images are included at the end after the code. 4 Code #include cv.h #include highgui.h #include <stdio.h> #include <stdlib.h> // utility function which prints out a matrix void printmatrix(cvmat M, int rows, int cols, const char name) int indent size ; int i,j; // print the matrix name indent size = printf( %s =, name) ; // print out the matrix for ( i =0;i<rows ; i++) // start of a row printf( [ ); for ( j =0;j<cols ; j++) if (j>0) printf(, ); printf( %11.5lg,cvmGet(M, i, j ) ) ; printf( ]\ n ) ; // indent the next line for ( j =0;j<indent size ; j++) printf( ); 2

printf( \n ) ; // main function, reads in an image f i l e and corresponding // coordinates and computes a perspective corrected image int main( int argc, char argv ) char filename ; IplImage image ; IplImage corrected image ; CvMat H; CvMat invh ; int i,j,k; double xw [ 4 ] ; double yw [ 4 ] ; double xi [4]; double yi [4]; // attempt to read in the image file if (argc >= 2) filename = argv [1]; else cvreleaseimage(&image) ; printf( Usage : hw1 filename ) ; return 1; if (( image = cvloadimage( filename,1) ) == 0) printf( Could not read the file! ); return 2; // read user input of the corresponding coordinates in the // world and image for ( i =0; i <4; i ++) printf( Enter world coordinate %d as x,y:,i); scanf( %lf,%lf,xw+i,yw+i ) ; printf( Enter image coordinate %d as x,y:,i); scanf( %lf,%lf,xi+i, yi+i ) ; printf( \n ) ; // set up the problem as a matrix equation double sol matrix [64]; double sol vector [8]; for ( i =0;i <4; i++) sol matrix [2 i 8+0] = xi [ i ] ; sol matrix [2 i 8+1] = yi [ i ] ; sol matrix [2 i 8+2] = 1; sol matrix [2 i 8+3] = 0; 3

sol matrix [2 i 8+4] = 0; sol matrix [2 i 8+5] = 0; sol matrix [2 i 8+6] = xw [ i ] xi [ i ]; sol matrix [2 i 8+7] = xw [ i ] yi [ i ]; sol matrix [(2 i +1) 8+0] = 0; sol matrix [(2 i +1) 8+1] = 0; sol matrix [(2 i +1) 8+2] = 0; sol matrix [(2 i +1) 8+3] = xi [ i ] ; sol matrix [(2 i +1) 8+4] = yi [ i ] ; sol matrix [(2 i +1) 8+5] = 1; sol matrix [(2 i +1) 8+6] = yw [ i ] xi [ i ]; sol matrix [(2 i +1) 8+7] = yw [ i ] yi [ i ]; sol vector [2 i] = xw[i]; sol vector [2 i +1] = yw[ i ] ; // solve the problem and copy the solution into invh CvMat temp = cvcreatemat (8,1,CV 64FC1) ; invh = cvcreatemat (3,3,CV 64FC1) ; CvMat A; CvMat B; cvinitmatheader(&a,8,8,cv 64FC1, s o l matrix,cvautostep) ; cvinitmatheader(&b,8,1,cv 64FC1, s o l vector,cvautostep) ; cvsolve(&a, &B, temp, CV LU) ; for ( i =0;i <8; i++) cvmset ( invh, i /3, i %3,cvmGet(temp, i, 0 ) ) ; cvmset(invh,2,2,1.0) ; cvreleasemat(&temp) ; // get H from Hˆ 1 H = cvcreatemat (3,3,CV 64FC1) ; cvinvert(invh,h,cv LU) ; // display H and Hˆ 1 printmatrix(invh,3,3, Hˆ 1 ) ; printmatrix(h,3,3, H ) ; // we need to determine the real world bounds that the // image represents double max x = 1.0e100 ; double max y = 1.0e100 ; double min x = 1.0e100 ; double min y = 1.0e100 ; CvMat world coord = cvcreatemat (3,4,CV 64FC1) ; CvMat image coord = cvcreatemat (3,4,CV 64FC1) ; // pick some representative image coordinates cvmset(image coord,0,0,0) ; cvmset(image coord,1,0,0) ; cvmset(image coord,2,0,1) ; cvmset(image coord,0,1,0) ; 4

cvmset(image coord,1,1,image >height 1) ; cvmset(image coord,2,1,1) ; cvmset(image coord,0,2,image >width 1) ; cvmset(image coord,1,2,0) ; cvmset(image coord,2,2,1) ; cvmset(image coord,0,3,image >width 1) ; cvmset(image coord,1,3,image >height 1) ; cvmset(image coord,2,3,1) ; // apply Hˆ 1 to get the corresponding world coordinates cvmatmul(invh, image coord, world coord) ; // now check the bounds for ( i =0;i <4; i++) double w = cvmget(world coord,2, i ) ; double real x = cvmget( world coord,0, i )/w; double real y = cvmget( world coord,1, i )/w; if (real x < min x) min x = real x; if (real y < min y) min y = real y; if (real x > max x) max x = real x; if (real y > max y) max y = real y; // determine the correct size for the new image assuming that // the new width should match the old width double scale factor = ((double) (image >width) ) /(max x min x); int new height = (int) (( max y min y) scale factor); corrected image = cvcreateimage( cvsize (image >width, new height), IPL DEPTH 8U, 3) ; cvzero(corrected image) ; // compute the new image by applying H to a grid of points in // the world coordinate system double step size = 1.0/ scale factor ; CvMat input coord = cvcreatemat (3,1,CV 64FC1) ; CvMat output coord = cvcreatemat(3,1,cv 64FC1) ; cvmset(input coord,2,0,1.0) ; for ( i =0; i<corrected image >width ; i++) cvmset(input coord,0,0,((double) i) step size+min x); for ( j =0; j<corrected image >height ; j++) double xi,yi,fx,fy; cvmset(input coord,1,0,((double) j) step size+min y); // compute the associated image coordinate cvmatmul(h, input coord, output coord) ; xi = cvmget( output coord, 0, 0 ) /cvmget( output coord,2,0) ; 5

yi = cvmget( output coord, 1, 0 ) /cvmget( output coord,2,0) ; // i f outside of the image then move on if (xi<0 yi <0 xi>=(image >width 1) yi>=(image >height 1)) continue ; // compute the fractional component of the image coord. fx = xi ( int)xi; fy = yi ( int)yi; // compute the pixel value using linear interpolation for (k=0;k<3;k++) double value = 0; value += (1.0 fx) (1.0 fy) (( uchar ) (image >imagedata + image >widthstep ( int)yi))[((int)xi) 3+k ] ; value += (1.0 fx) fy (( uchar ) (image >imagedata + image >widthstep ( int)(yi+1)))[((int)xi) 3+k ] ; value += fx (1.0 fy) (( uchar ) (image >imagedata + image >widthstep ( int)yi))[((int)(xi+1)) 3+k ] ; value += fx fy (( uchar ) (image >imagedata + image > widthstep ( int)(yi+1)))[((int)(xi+1)) 3+k ] ; (( uchar )(corrected image >imagedata + corrected image > widthstep j))[i 3+k] = value ; // save corrected image char new filename [128]; sprintf(new filename, %s. corrected.png, filename) ; cvsaveimage( new filename, corrected image) ; // cleanup cvreleaseimage(&image) ; cvreleaseimage(&corrected image) ; cvreleasemat(&h) ; cvreleasemat(&invh) ; cvreleasemat(&world coord) ; cvreleasemat(&image coord) ; cvreleasemat(&input coord) ; cvreleasemat(&output coord) ; return 0; 6

Figure 1: adams01 Figure 2: adams01.corrected 7

Figure 3: adams03 Figure 4: adams03.corrected 8

Figure 5: board01 Figure 6: board01.corrected 9

Figure 7: door01 Figure 8: door01.corrected 10

Figure 9: bricks Figure 10: bricks.corrected 11

Figure 11: clock Figure 12: clock.corrected 12

Figure 13: mailboxes Figure 14: mailboxes.corrected 13

Figure 15: piano Figure 16: piano.corrected 14