GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-May-2012, 05:22
stbb24 stbb24 is offline
New Member
 
Join Date: May 2012
Posts: 14
stbb24 is on a distinguished road

OpenCv C to C++


I want to adapt the c++ interface of Opencv but find it hard converting my codes from c to c++. Does anyone know the equivalent version of cvCreateImage in c++?

Thanks
  #2  
Old 28-May-2012, 07:32
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,147
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: OpenCv C to C++


Quote:
Originally Posted by stbb24
I want to adapt the c++ interface of Opencv but find it hard converting my codes from c to c++. Does anyone know the equivalent version of cvCreateImage in c++?

Thanks
What problems are you having? The basic OpenCV stuff can be compiled into C++ programs with no special treatment. (See Footnote.)

With OpenCV version 1.0.0 on my Centos 5.8 workstation:

CPP / C++ / C Code:
//
// C++ file demonstrating some OpenCV version 1.0.0 functions.
//
// Compile with GNU g++:
//
//   g++ -W -Wall -pedantic opencv_demo.cpp -lopencv_legacy -o opencv_demo
//
// davekw7x
//
#include <iostream>

#include <opencv/cv.h>
#include <opencv/highgui.h>

using namespace std;

int main(int argc, char **argv)
{
    if (argc < 2) {
        cerr << "Must give file name of an image file." << endl;
        return 0;
    }
    const char * imageFileName = argv[1];

    IplImage *originalImage = cvLoadImage(imageFileName);
    if (!originalImage) {
        cerr << "Can't open image file " << imageFileName << endl;
        return 0;
    }
    cout << "Displaying " << imageFileName << endl;

    // Convert to monochrome
    IplImage *monoImage =
        cvCreateImage(cvSize(originalImage->width, originalImage->height), 8, 1);
    cvCvtColor(originalImage, monoImage, CV_BGR2GRAY);

    // Do an edge detection
    IplImage *edgeImage =
        cvCreateImage(cvSize(originalImage->width, originalImage->height), 8, 1);
    cvAdaptiveThreshold(monoImage, edgeImage, 255,
                        CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY,
                        15, 22);
    cvSmooth(edgeImage, edgeImage, CV_GAUSSIAN, 3, 3);

    cvNamedWindow("Original", CV_WINDOW_AUTOSIZE);
    cvMoveWindow("Original", 50, 50);

    cvNamedWindow("Monochrome", CV_WINDOW_AUTOSIZE);
    cvMoveWindow("Monochrome", 150, 150);

    cvNamedWindow("Adaptive", CV_WINDOW_AUTOSIZE);
    cvMoveWindow("Adaptive", 250, 250);

    cvShowImage("Original", originalImage);
    cvShowImage("Monochrome", monoImage);
    cvShowImage("Adaptive", edgeImage);

    cvWaitKey(0);

    cvDestroyAllWindows();

    cvReleaseImage(&originalImage);
    cvReleaseImage(&monoImage);
    cvReleaseImage(&edgeImage);
}

Compile with something like

g++ -W -Wall -pedantic opencv_demo.cpp -lopencv_legacy -o opencv_demo


Put an image file, say baboon.jpg, in the same directory, then execute

./opencv_demo baboon.jpg

Regards,

Dave

Footnote:
I have shown you mine; now show me yours.

In other words:
If you have a small C file that works for you and you have tried to convert to C++ and it didn't work, then post the code. Tell us the operating system, compiler and OpenCV versions that you are using.

Here's the thing:
For best chances of meaningful help with fewest iterations, give us complete information about your system and your requirements so that we can make a stab at trying to help you. Later versions of OpenCV have lots of C++ functionality above and beyond the C functions in version 1.0.0, but there is no way for us to guess at what your problems are.

Also, there are examples in the OpenCV distribution and there are tutorials for recent versions on the OpenCV web site opencv.org

Start at The OpenCV wiki
__________________
Sometimes I just can't help myself...
Last edited by davekw7x : 28-May-2012 at 08:55.
  #3  
Old 30-May-2012, 02:11
stbb24 stbb24 is offline
New Member
 
Join Date: May 2012
Posts: 14
stbb24 is on a distinguished road

Re: OpenCv C to C++


This is the original c code that I want to convert to c++

CPP / C++ / C Code:

#include <iostream>
#include "cv.h"
#include "highgui.h"
using namespace std;

void binary(IplImage* img, IplImage* dst)
{
	int minThreshold = 60;
	int maxThreshold = 255;
	
	IplImage* r = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1 );
	IplImage* g = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1 );
	IplImage* b = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1 );
	
	cvSplit( img, r, g, b, NULL );
	
	IplImage* s = cvCreateImage( cvGetSize(img), IPL_DEPTH_8U, 1 );
	
	cvAdd(r,g,r);
	cvAdd(r,b,s);
	
	cvThreshold( s, dst, minThreshold, maxThreshold, CV_THRESH_BINARY );
	
	cvReleaseImage(&r);
	cvReleaseImage(&g);
	cvReleaseImage(&b);
	cvReleaseImage(&s);
}
int main(int argc, char** argv)
{
	IplImage* img = cvLoadImage(argv[1], 1);
	IplImage* dst = cvCreateImage( cvGetSize(img), img->depth, 1);
	binary(img, dst);
	cvNamedWindow( argv[1], CV_WINDOW_AUTOSIZE );
	cvShowImage( argv[1], dst );
	cvSaveImage("binaryOutput.jpg",dst);
	cvWaitKey(0);
	cvDestroyWindow( argv[1] );
	cvReleaseImage( &img );
	cvReleaseImage( &dst );

	return 0;
}

The code above converts a colored image into binary

Here the c++ code that I got so far

CPP / C++ / C Code:

#include <iostream>
#include <cv.h>
#include <highgui.h>
using namespace std;
using namespace cv;


void binary(Mat img, Mat dst)
{
	int minThreshold = 60;
	int maxThreshold = 255;
	
	Mat r = cvCreateMat(img.size().width, img.size().height, CV_8UC1);
	Mat g = cvCreateMat(img.size().width, img.size().height, CV_8UC1);
	Mat b = cvCreateMat(img.size().width, img.size().height, CV_8UC1);

	split( img, dst );
	
	Mat s = cvCreateMat( img.size().width, img.size().height, CV_8UC1);
	
	//cvAdd(r,g,r);
	//cvAdd(r,b,s);
	
	threshold( s, dst, minThreshold, maxThreshold, CV_THRESH_BINARY );
	
	r.release();
	g.release();
	b.release();
	s.release();
}


int main(int argc, char** argv)
{
	Mat img = imread(argv[1], 1);

	Mat dst = cvCreateMat(img.size().width, img.size().height, CV_8UC1);
	binary(img,dst);
	
	namedWindow( argv[1], CV_WINDOW_AUTOSIZE );
	imshow( argv[1], img );
	imwrite( "../../Output.jpg", dst );
	waitKey(0);
	destroyWindow( argv[1] );
	img.release();
	dst.release();
	
	return 0;
}

And here's the error from the code above

Code:
binaryC++.cpp: In function ‘void binary(cv::Mat, cv::Mat)’: binaryC++.cpp:19:18: error: no matching function for call to ‘split(cv::Mat&, cv::Mat&)’ binaryC++.cpp:19:18: note: candidates are: /usr/include/opencv2/core/core.hpp:2042:17: note: void cv::split(const cv::Mat&, cv::Mat*) /usr/include/opencv2/core/core.hpp:2042:17: note: no known conversion for argument 2 from ‘cv::Mat’ to ‘cv::Mat*’ /usr/include/opencv2/core/core.hpp:2044:19: note: void cv::split(const cv::Mat&, std::vector<cv::Mat>&) /usr/include/opencv2/core/core.hpp:2044:19: note: no known conversion for argument 2 from ‘cv::Mat’ to ‘std::vector<cv::Mat>&’ /usr/include/opencv2/core/mat.hpp:1626:29: note: template<class _Tp> void cv::split(const cv::Mat&, std::vector<cv::Mat_<_Tp> >&)

Specifications:
Ubuntu 12.04
OpenCv 2.3
GNU g++
Last edited by admin : 30-May-2012 at 22:28. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #4  
Old 30-May-2012, 05:52
Maximlis Maximlis is offline
New Member
 
Join Date: May 2012
Location: Pune
Posts: 10
Maximlis is an unknown quantity at this point

Re: OpenCv C to C++


Its a really tough platform. the program which can be solved in 4 to 5 line, That can be solved in 100 lines mostly.
  #5  
Old 30-May-2012, 09:32
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,147
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: OpenCv C to C++


Quote:
Originally Posted by stbb24
This is the original c code that I want to convert to c++

CPP / C++ / C Code:

#include <iostream>
.
.
That is already C++. It uses the old OpenCV API. (GNU g++ compiles it and it executes with OpenCV version 1.0.0 on my Centos 5.8 Linux system and also with OpenCV 2.4.0).

Getting satisfactory results may require tweaking the threshold limits (depending on the image), but it can work.

I hate to repeat myself, but it already is C++.

Quote:
Originally Posted by stbb24
Here the c++ code that I got so far
The problem isn't about C++. It is that you are not using the new API properly. (I get similar error messages when I compile it with OpenCV 2.4.0 on my system).

As far as the error messages, themselves:
Code:
binaryC++.cpp: In function ‘void binary(cv::Mat, cv::Mat)’: binaryC++.cpp:19:18: error: no matching function for call to ‘split(cv::Mat&, cv::Mat&)’ binaryC++.cpp:19:18: note: candidates are: /usr/include/opencv2/core/core.hpp:2042:17: note: void cv::split(const cv::Mat&, cv::Mat*) /usr/include/opencv2/core/core.hpp:2042:17: note: no known conversion for argument 2 from ‘cv::Mat’ to ‘cv::Mat*’
The split function takes two arguments: The first is a const reference to a Mat object and the second is a pointer to a Mat object.

Now, you can eliminate the error message by simply changing the call to
CPP / C++ / C Code:
    split(img, &dst);

However...there are still major problems that cause a malfunction. Namely. split is going to split the image into three planes, so the second argument must point to an array of three Mat objects, not just one object.

(Note that another form of split takes a vector of Mat objects for its second argument.)


Bottom line: If I were writing it, it might look the following. (But: see Footnote.)
CPP / C++ / C Code:
//
// Tested with OpenCV version 2.4.0 on Centos 5.8 Linux
// GNU g++ version 4.1.2
//
// davekw7x

#include <iostream>
#include <cv.h>
#include <highgui.h>

using namespace std;
using namespace cv;


//
// Note that since dst back in the calling program needs to
// be changed, the second parameter must be a reference.
//
void convertImage(const Mat & img, Mat & dst);


const char * DEFAULT_IMAGE_FILE_NAME =  "baboon.jpg";

int main(int argc, char ** argv)
{
    const char * fileName = DEFAULT_IMAGE_FILE_NAME;
    if (argc > 1) {
        fileName = argv[1];
    }

    Mat img = imread(fileName, 1);

    if (img.empty() || (img.data == NULL)) {
        cout << "Can't load image from " << fileName << endl;
        return EXIT_FAILURE;
    }
    cout << "Loaded "
         << img.rows << 'x' << img.cols
         << " image from " << fileName << endl;


    Mat dst = cvCreateMat(img.size().width, img.size().height, CV_8UC1);
    convertImage(img, dst);
    
    namedWindow("Original", CV_WINDOW_AUTOSIZE);
    moveWindow("Original", 50, 50);
    imshow("Original", img);

    namedWindow("Transformed", CV_WINDOW_AUTOSIZE);
    moveWindow("Transformed", img.size().width+60, 50);
    imshow("Transformed", dst);

    imwrite("NewImage.jpg", dst);

    waitKey(0);

    destroyWindow("Original");
    destroyWindow("Transformed");
    img.release();
    dst.release();
    
    return 0;
}

void convertImage(const Mat & img, Mat & dst)
{
    int minThreshold = 60;
    int maxThreshold = 255;
    
    // A matrix to hold the converted image
    Mat s = cvCreateMat(img.size().width, img.size().height, CV_8UC1);

    // Could write Mat planes[3];
    vector<Mat> planes;
    
    // Separate the image into its r, g, b components
    split(img, planes);

    Mat r = planes[0];
    Mat g = planes[1];
    Mat b = planes[2];
    
    // Make s = r+g+b
    add(r, g, r);
    add(r, b, s);
    
    threshold(s, dst, minThreshold, maxThreshold, CV_THRESH_BINARY);
    
    r.release();
    g.release();
    b.release();
    s.release();
}


Regards,

Dave

Footnote:
Your use of split() followed by the calls to add() can result in limiting of the sum of r+g+b. Threshold will be very critical to prevent washout. If you just want to convert rgb to grayscale, you can use convertColor().

Then the entire function can be simplified:
CPP / C++ / C Code:
void convertImage(const Mat & img, Mat & dst)
{
    // In general, wouldn't you want to make the threshold
    // values parameters so that the calling function can
    // specify the threshold?  It makes experimentation
    // easier, since main() could prompt the user for threshold
    // values and inspect the results interactively.
    //
    int minThreshold = 60;
    int maxThreshold = 255;
    
    // A matrix to hold the converted image
    Mat s = cvCreateMat(img.size().width, img.size().height, CV_8UC1);
    cvtColor(img, s, CV_BGR2GRAY);
    threshold(s, dst, minThreshold, maxThreshold, CV_THRESH_BINARY);
    
    s.release();
}

Also, sometimes adaptiveThreshold(), rather than plain vanilla threshold(), might be appropriate. (Your Mileage May Vary.)

On the other hand, if you had done it like this you might not have learned about the new split(), which is very useful for other transformations...
__________________
Sometimes I just can't help myself...
  #6  
Old 30-May-2012, 11:11
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,147
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: OpenCv C to C++


Quote:
Originally Posted by davekw7x
...

Bottom line: If I were writing it, it might look the following.

Actually, there is a further refinement. Back in olden times, when OpenCV stuff was written in C, things like cvCreateMat() were required to generate storage for cvMat "objects."

Nowadays, we can just use the Mat constructor. (There is no need for the application program to release the storage, since the Mat destructor takes care of it when the object goes out of scope.)
CPP / C++ / C Code:
//
// Tested with OpenCV version 2.4.0 on Centos 5.8 Linux
// GNU g++ version 4.1.2
//
// davekw7x

#include <iostream>
#include <cv.h>
#include <highgui.h>

using namespace std;
using namespace cv;


//
// Note that since dst back in the calling program needs to
// be changed, its parameter must be a reference.
//
void convertImage(const Mat & img, Mat & dst);


const char * DEFAULT_IMAGE_FILE_NAME =  "baboon.jpg";

int main(int argc, char** argv)
{
    const char * fileName = DEFAULT_IMAGE_FILE_NAME;
    if (argc > 1) {
        fileName = argv[1];
    }

    Mat img = imread(fileName, 1);

    if (img.empty() || (img.data == NULL)) {
        cout << "Can't load image from " << fileName << endl;
        return EXIT_FAILURE;
    }
    cout << "Loaded "
         << img.rows << 'x' << img.cols
         << " image from " << fileName << endl;


    // Create a dst Mat object by invoking the Mat constructor
    Mat dst(img.size().width, img.size().height, CV_8UC1);

    convertImage(img, dst);

    // Now display results and write converted image to a file    
    namedWindow("Original", CV_WINDOW_AUTOSIZE);
    moveWindow("Original", 50, 50);
    imshow("Original", img);

    namedWindow("Transformed", CV_WINDOW_AUTOSIZE);
    moveWindow("Transformed", img.size().width+60, 50);
    imshow("Transformed", dst);

    imwrite("NewImage.jpg", dst);

    waitKey(0);

    destroyWindow(fileName);
    destroyWindow("Transformed");
    
    return 0;
}

void convertImage(const Mat & img, Mat & dst)
{
    // In general, wouldn't you want to make the threshold
    // values parameters so that the calling function can
    // specify the threshold?  It makes experimentation
    // easier, since main() can prompt the user for threshold
    // values and inspect the results interactively.
    //
    int minThreshold = 60;
    int maxThreshold = 255;
    
    // Create a matrix to hold the converted image

    Mat s(img.size().width, img.size().height, CV_8UC1);

    // Convert to grayscale
    cvtColor(img, s, CV_BGR2GRAY);

    // Do the thresholding thing
    threshold(s, dst, minThreshold, maxThreshold, CV_THRESH_BINARY);
}




Regards,

Dave
__________________
Sometimes I just can't help myself...
  #7  
Old 30-May-2012, 18:14
stbb24 stbb24 is offline
New Member
 
Join Date: May 2012
Posts: 14
stbb24 is on a distinguished road

Re: OpenCv C to C++


Thanks for your comments appreciate it.

And one more thing if I include
Code:
using namespace cv;
in the header
then instead of doing this
Code:
cvAdd() // I can just simply right it as add()

Is it the same thing or completely different things. I'm a total newbie when it comes to using opencv that's why I'm asking.

Thanks
  #8  
Old 30-May-2012, 19:10
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,147
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: OpenCv C to C++


Quote:
Originally Posted by stbb24
...if I include
Code:
using namespace cv;
in the header
then instead of doing this
Code:
cvAdd() // I can just simply right it as add()
That is what I did in my first listing in post number 5 of this thread. (So the answer is yes.)
In the Footnote of that same post (and in the complete program of my last post) I showed how to convert to grayscale without adding r+g+b, but sometimes you do want to add image components, and the add() function is a way to do it.

Bottom line: All of the cvXXX functions are carryovers from older versions. Even though most of these "legacy" functions are more-or-less compatible, with more recent versions of OpenCv, in my opinion it's much better to learn how to use the cv namespace and use modern equivalents of those functions.

Some of the functions don't have (and don't need) modern equivalents, as I mentioned in my last post. You don't need a function like cvCreateMat, since the constructor of the Mat class does the deed. You don't need anything like cvReleaseImage, since the Mat class destructor cleans up after itself. (Huzzah!)

Here's the thing:

OpenCV has never been particularly "easy" for (most) people to learn, but if you have needs for even a small part of the functionality, you might find it well worth the effort.

My suggestion: Start with some explanations, tutorials and examples on the OpenCV web site. (There are also some examples in the OpenCV source download tree.)

So, look at things like this on their web site: OpenCV Introduction

Then...

Click on the tutorials link on The OpenCV Documentation Page


Regards,

Dave
__________________
Sometimes I just can't help myself...
  #9  
Old 30-May-2012, 21:18
stbb24 stbb24 is offline
New Member
 
Join Date: May 2012
Posts: 14
stbb24 is on a distinguished road

Re: OpenCv C to C++


Quote:
Originally Posted by davekw7x
That is what I did in my first listing in post number 5 of this thread. (So the answer is yes.)
In the Footnote of that same post (and in the complete program of my last post) I showed how to convert to grayscale without adding r+g+b, but sometimes you do want to add image components, and the add() function is a way to do it.

Bottom line: All of the cvXXX functions are carryovers from older versions. Even though most of these "legacy" functions are more-or-less compatible, with more recent versions of OpenCv, in my opinion it's much better to learn how to use the cv namespace and use modern equivalents of those functions.

Some of the functions don't have (and don't need) modern equivalents, as I mentioned in my last post. You don't need a function like cvCreateMat, since the constructor of the Mat class does the deed. You don't need anything like cvReleaseImage, since the Mat class destructor cleans up after itself. (Huzzah!)

Here's the thing:

OpenCV has never been particularly "easy" for (most) people to learn, but if you have needs for even a small part of the functionality, you might find it well worth the effort.

My suggestion: Start with some explanations, tutorials and examples on the OpenCV web site. (There are also some examples in the OpenCV source download tree.)

So, look at things like this on their web site: http://opencv.willowgarage.com/documentation/cpp/introduction.html

Then...

Click on the http://docs.opencv.org/doc/tutorials/tutorials.html link on http://docs.opencv.org/


Regards,

Dave
Thanks davekw7x!!!
Thanks for your comment
 
 

Recent GIDBlogRunning Linux Programs at Boot Time by gidnetwork

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Silhouette extraction algorithm from depth video chezmark Java Forum 1 05-Dec-2011 00:40
OpenCV and problems with matrices ipunished C++ Forum 2 19-Jul-2011 10:58

Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:31.


vBulletin, Copyright © 2000 - 2013, Jelsoft Enterprises Ltd.