Wednesday, July 6, 2011

OPENCV2 C++ basics (OOP) old style vs new style

Version 2.2 of the opencv library has divided its library into modules.
These library modules have their own associated header files which is required therefore any code you see on the internet that looks like this:
#include "cv.h"
#include <cv.h> and so on
is done using the old style, C or C++. However after the restructuring of the opencv library into modules, not all functionality was ported to the new style:
#include <opencv2/core/core.hpp>

one example of such method is the LOGPOLAR transform.
Another thing to take note here is, the old deprecated IplImage is changed to the matrix cv::Mat. therefore you should avoid using such unless your using the old style.

For the Log-Polar Transform i will be using this header file:
#include <opencv2/imgproc/imgproc_c.h>

Also to make things easy, ive created a class to convert between cv::Mat and IplImage
class atsoldtonew
    IplImage convertOld(cv::Mat MatImage)
        IplImage img =MatImage;
        return img;
    cv::Mat convertNew(IplImage *iplImage)
        cv::Mat imgMat(iplImage);
        return imgMat;

So the Log-Polar Class is the following:
class atslogPolar
    void convert(cv::Mat image)
        IplImage src;
        IplImage img =image;
        IplImage* newsrc = &img;
        IplImage* dst = cvCreateImage( cvGetSize(newsrc), 8, 3  );
        IplImage* dst2 = cvCreateImage( cvGetSize(newsrc), 8, 3  );

        cvLogPolar( newsrc, dst, cvPoint2D32f(image.size().width/2,image.size().height/2), 40,
        cvLogPolar( dst, dst2, cvPoint2D32f(image.size().width/2,image.size().height/2), 40,

        newIm = dst;
        newImage = dst2;
    void view()
        cvShowImage( "inverse log-polar", newIm );
        cvShowImage( "reinverse log-polar", newImage );
    IplImage* logpolar()
        return newIm; //return the local variable
    IplImage* Inverselogpolar()
        return newImage; //return the local variable
    IplImage* newIm; //keep a local variable
    IplImage* newImage; //keep a local variable

This Class uses the old style however by converting to and from cv::Mat to IplImage your main function can be written as the following

#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
int main()
    atscameraCapture mine; //initialize
        cv::Mat image = mine.ats_getImage();
        cv::imshow("image",image); //CAMERA IMAGE
        atslogPolar pl;
        pl.view();   //show using the old style
        atsoldtonew conv;
        cv::imshow("image logpolar",conv.convertNew(pl.logpolar()));  //show using the new style

        if(cv::waitKey(30) >= 0) break;
    return 0;
You can easily add the class to my previous Post code to add the new added functionality

No comments:

Post a Comment