Saturday, February 1, 2014

Tutorial: First OpenCV program

After we installed OpenCV and configured Xcode it's time to create an easy program.

For the first program we want to refer to a OpenCV tutorial where we change the contrast and brightness of an image. But as I mentioned in my first post, this tutorial is outdated and was compiled with a older version of OpenCV. So we have to do some small changes to the code.

Edit the code

First edit the "main.cpp" file. Delete everything and copy the following code inside. You can also download it here.

1:  #include <opencv/cv.h>  
2:  #include <opencv/highgui.h>  
3:  #include <iostream>  
4:    
5:  using namespace cv;  
6:    
7:  double alpha; /**< Simple contrast control */  
8:  int beta; /**< Simple brightness control */  
9:    
10:  int main( int argc, char** argv )  
11:  {  
12:    // Read image given by user  
13:    // Change the path here to your image, look at form below  
14:    Mat image = imread( "/users/name/documents/programming/imgs/test1.jpg" );  
15:    Mat new_image = Mat::zeros( image.size(), image.type() );  
16:      
17:    // Initialize values  
18:    std::cout<<" Basic Linear Transforms "<<std::endl;  
19:    std::cout<<"-------------------------"<<std::endl;  
20:    std::cout<<"* Enter the alpha value [1.0-3.0]: ";std::cin>>alpha;  
21:    std::cout<<"* Enter the beta value [0-100]: "; std::cin>>beta;  
22:      
23:    // Do the operation new_image(i,j) = alpha*image(i,j) + beta  
24:    for( int y = 0; y < image.rows; y++ )  
25:    { for( int x = 0; x < image.cols; x++ )  
26:    { for( int c = 0; c < 3; c++ )  
27:    {  
28:      new_image.at<Vec3b>(y,x)[c] =  
29:      saturate_cast<uchar>( alpha*( image.at<Vec3b>(y,x)[c] ) + beta );  
30:    }  
31:    }  
32:    }  
33:      
34:    // Create Windows  
35:    namedWindow("Original Image", 1);  
36:    namedWindow("New Image", 1);  
37:      
38:    // Show stuff  
39:    imshow("Original Image", image);  
40:    imshow("New Image", new_image);  
41:      
42:    // Wait until user press some key  
43:    waitKey();  
44:    return 0;  
45:  }  

Note that I edited the include files in line 1 and 2. This is because of a newer version of OpenCV. If you navigate to /usr/local/include you will see that there are two folders now - opencv and opencv2. opencv contains the old header files and opencv2 the newer ones. So we just add the folder name to the path. Newer tutorials from the OpenCV website always include already the path and they use the opencv2 headers.

Run the program

Now it's time to finally compile and run the program. For this go to Product --> Run or just hit ⌘R

Down in the right corner you can see the console output where you can choose some values of the image manipulation.


Type in some numbers and hit enter. Here is the result for the values I chose.


Program versions:
OS: Mac OS X 10.9.1
Xcode: 5.0.2
OpenCV: 2.4.8.0

5 comments:

  1. Thank you very much for this tutorial and your previous tutorial for setting up OpenCV. Im using 3.0, so to get this file to compile I had to include opencv2/opencv.hpp

    ReplyDelete
    Replies
    1. Thanks! Also for the tutorial I wrote, the opencv2/opencv.hpp would have been the proper solution :)

      Delete
  2. Hello Christian.. thank you very much for this example!

    ReplyDelete
  3. Hello Christian:

    When I try to run the above sample code, I get the following error:

    dyld: Library not loaded: cv2.so
    Referenced from: /Users/dpritham/Library/Developer/Xcode/DerivedData/DP_OpenCV-bhjbbhskznatpzaobwqudsgnshfq/Build/Products/Debug/DP_OpenCV
    Reason: image not found

    Can you PLEASE help me!

    ReplyDelete