// ================= // || Worksheet 1 // || In this example, we are going read and display images // ================= // for another (less detailed example) see, // http://docs.opencv.org/doc/tutorials/introduction/display_image/display_image.html // here we include two libraries of OpenCV. #include "opencv2/imgproc/imgproc.hpp" // imgproc is the standard image processing library of open CV. // it contains a lot of definitions of things like cv::Mat (the CV matrix class) #include "opencv2/highgui/highgui.hpp" // highgui is the (HIGH)-level (G)raphical (U)ser (I)nterface library. // it has methods like 'namedWindow', 'imShow' and 'waitKey' that we use in the program. // this is the header that allows us to work with the console command line. // gives us access to the objects std::cout ("console out") and std::cin ("console in") #include // the header that has all our custon method declarations in it. #include "program.h" // here we use the 'using namespace' keywords so we dont have to keep writing cv:: or std:: using namespace std; using namespace cv; // however, for this example i will leave in all the 'cv::' throughout the code, so we can // easily see what methods come from the openCV libraries. // this is the standard 'main' method for any c++ program. // it indicates where your program starts. // (for now we are calling it worksheetone, since each program can only have one 'main') // it takes two arguments. // 1) int argc ('argument count') - this tell me how many arguments were passed to my program // in c-style programs, arguemnts are passes as string "words" which // are arrays of characters (see definition later) // 2) char** argv ('argument values') - this looks like a weird kind of object, what is a char**? // well, we already decided that we are calling an array of chars a 'string'. thus, we shoudl thing of this // of char** = (char*)* = (string)* // thus char** looks like a pointer to a string, in fact, // it is a pointer to the first entry on the heap of an 'array of string' // that array is of lenth 'argc', and it each entry is that array contains a different argument that was passed to this method. // So we should think of char** as representing a data type like // { "string_1", "string_2", "blah", ... } // By default. the FIRST argument passed is always the name of program. // i.e. argc is always at least 1, // and argv = {"program_name", ..., ...} int worksheetone( int argc, char** argv ) { // this variable is declared to be an array of char's. // a char stands for 'character' it is represented by 8 bits, i.e. 0-255 = 2^8-1. // thus an array of char's represents a string of characters and forms a work. // i.e. the string "aabbcc" looks like // { 97,97,98,98,99,99 } // see the list here http://www.asciitable.com/ char my_window_name[] = "My Image"; // here i create a basic cv::Mat matrix. // shortly im going to read an image file and store it in this object. // i just create an empty (0 by 0) matrix here, it will be initialized with data soon. cv::Mat src; // check if i have passed in any arguments. // if argc >1 , then i have. if( argc > 1) { // here we retrieve a pointer to the SECOND argument that was passed. we access the second arguement of // argv by writing argv[1]. (argv[0] would be the string that is the name of this program.) char* first_file_passed_in = argv[1]; // Load the source image,and store it in our matrix src. // the arguments are // 1) the name of the file, (this should be a string = char*) // 2) an openCV keyword indicating that we want to load the image in color. src = cv::imread( first_file_passed_in, CV_LOAD_IMAGE_COLOR ); } else { // no arguments were passed in return -1; } // here i create a window, in which i can display images. // the two arguments for this are // 1) the name of the window we are creating. We need to be careful to remember // this name as we are going to use it to refer to this window later, // that is why we stored the name in the 'my_window_name' static variable // 2) the second argument is optional, but here we stick in an OpenCV keyword // that tells this window to automatically resize to to the size of what ever // image i display in it. cv::namedWindow( my_window_name, WINDOW_AUTOSIZE ); // now we can display our loaded image into the window we just created. // the two arguments here are // 1) the name of the window that we want to show our image (this window must be created prior to this call) // 2) the cv::Mat containing the data of the image we are to show. cv::imshow( my_window_name, src); // here i use the highgui method waitKey(); which tells the program // to wait until the user presses any key on the keyboard. // this gives us a chance to look at the image, before the program exits. cv::waitKey(); // notice that the method signature for the 'main' method : // int main( ... , ... ) // says that that 'main' has to return an 'int' (integer) when at the end of it. // here we return the value '0', which indicates that this method has returned sucessfully. // the usual convention is 0="success", and anything other than 0 is "failure" // a 'return' call should always be the last line of any method. return 0; }