Ask Your Question
1

using cout with Mat object

asked 2013-01-15 08:28:58 -0600

bee. gravatar image

updated 2013-01-15 08:29:27 -0600

Hi.. I needed to print my Mat object and the programm throws an exception... The project is really simply: creating Mat object and printing by using cout - just like in OpenCV tutorial:

#include <core/core.hpp>
#include <highgui/highgui.hpp>

#include <iostream>

using namespace std;
using namespace cv;

int main(int argc, char *argv[])
{
    Mat O = Mat::ones(2, 2, CV_32F);
    cout << "O = " << endl << " " << O << endl << endl;

    // Point2f P(5, 1);
    // cout << "Point (2D) = " << P << endl << endl;

return 0;
}

The exception says: Unhandled exception at 0x59430671 (msvcp100d.dll) in printingTest.exe: 0xC0000005: Access violation reading location 0x00000000. Console shows only

O = [

Precisely it stops on "operations.hpp" at:

static inline std::ostream& operator << (std::ostream& out, const Mat& mtx) { Formatter::get()->write(out, mtx); return out; } It seems "out" to be empty, but does someone know why? Tutorial says it should work...

I had earlier similar problem with throwing exception and I solved it here:

http://answers.opencv.org/question/5113/problem-with-reading-image-to-mat/

Is it possible that there is another environmental variable conflict? or maybe a collision 'cause I'm using VS2012 and there's OpenCV only for v10?

The thing with Point2f which is commented works normally.

edit retag flag offensive close merge delete

Comments

1

Ok, it's working. For Visual Studio 2012 you have to make a build of OpenCV in CMake and change of course the path to the libs in Linker properties and also change the environmental variable PATH.

bee. gravatar imagebee. ( 2013-01-17 05:13:59 -0600 )edit

What version of OpenCV was this with? I have this problem with OpenCV 2.4.4 and it seems it does support VS2012 (it has stuff in a vc11 folder at least and everything else works). Maybe I whould rebuild opencv?

ocvusr gravatar imageocvusr ( 2013-05-18 07:08:08 -0600 )edit

I have pointed the path variable to vc10 currently. Should I make a build of OpenCV 2.4.7 (version I am using) for VS2012 and point the path to it. I am curious as how you figured out what was causing the problem.

kalyanramu gravatar imagekalyanramu ( 2014-01-25 00:11:38 -0600 )edit

4 answers

Sort by ยป oldest newest most voted
0

answered 2013-10-30 15:31:51 -0600

amjg92 gravatar image

updated 2013-10-30 15:32:55 -0600

I have met the same problem, I am using opencv 2.4.6.0 and visual studio professional 2012 The only solution I found for my code is if you have an CV_32FC1 type use warp_mat = Mat::zeros(2, 3, CV_32FC1);

    cout << "Warp_mat = " <<endl;
    for ( int i = 0; i <3; i++)
    {
        cout<<" " << warp_mat.at<float>(0, i) ;
    }
    cout << " " <<endl;
    for ( int i = 0; i <3; i++)
    {
            cout<<" " << warp_mat.at<float>(1, i) ;
    }
    cout << " " <<endl;

That was at the initialization stage, later after getting the affine the function changed it to a cv_64F therefore you cannot use that code but instead you have to use the following

    cout << "Warp_mat = " <<endl;
    for ( int i = 0; i <3; i++)
    {
            cout<<" " << warp_mat.at<double>(0, i) ;
    }
    cout << " " <<endl;
    for ( int i = 0; i <3; i++)
    {
        cout<<" " << warp_mat.at<double>(1, i) ;
    }
    cout << " " <<endl;
edit flag offensive delete link more
0

answered 2013-01-15 09:11:01 -0600

unxnut gravatar image

You are trying to print a pointer to mat instead of the mat. Replace the cout line with

cout << "O = " << endl << " " << O.at<float>(0,0) << endl << endl;

and it works. Notice the use of at<float>.

edit flag offensive delete link more

Comments

Actually he is trying to print whole image, not a single pixel. This is perfectly legal, I am using this myself. But for some unknown reason it didn't work on his system.

Michael Burdinov gravatar imageMichael Burdinov ( 2013-01-15 11:07:56 -0600 )edit

I tried it as well and it did not work for me. I can see in the tutorial as well that it should work but could not make it. Now, I am curious as well about the cause.

unxnut gravatar imageunxnut ( 2013-01-15 12:51:23 -0600 )edit

So still waiting, maybe someone will know what to do..

bee. gravatar imagebee. ( 2013-01-16 01:36:38 -0600 )edit
0

answered 2013-01-15 12:19:01 -0600

There is C++ I/O Mat example cout_mat.cpp. You can use it as reference.

edit flag offensive delete link more

Comments

But it's still the same as in the tutorial and still the same exception...

bee. gravatar imagebee. ( 2013-01-16 01:35:35 -0600 )edit
0

answered 2016-02-05 06:49:45 -0600

goldroses gravatar image

just to add: I print every 3 seconds one line of my cv::Mat encoded as 16UC1 as my video-feed has @30fps

    int count; cv::Mat dImg;
    if(count == 90){
        for(int i = 0; i<dImg.cols;i++){
            cout<<" "<<unsigned(dImg.at<uchar>(240,i));
        }
        count = 0; 
    }   
    count++;
edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-01-15 08:28:58 -0600

Seen: 7,983 times

Last updated: Feb 05 '16