0

I am trying to do some basic algebraic operations on matrices. I would like to use the class Mat from openCV. I used the following simple code which wouldn't work:

void main()
{
  float data[2][5] = {{1,2,3,4,5},{7,8,9,10,11}}; 
  Mat H = Mat(2, 5, CV_8UC1, data); 
  cout << H.at<float>(0,0);
  //OR:
  cout << H;
}

Now I have already encountered a similar problem in loading an image by the imread function. I've overcomed it by starting from C and then pass to C++:

IplImage* Csrc = cvLoadImage("D:/picture.jpg");
Mat src(Csrc);

which did work. Could anyone help with the scalars matrices? How could I print the entries for example ? Thank you.

  • 1
    How does it not work? Bear in mind that `CV_8UC1` means 8 bit unsigned integer types. – juanchopanza May 05 '13 at 08:08
  • It would also help if you clarified what you mean by "wouldn't work", and provide some self-contained code that replicates the problem. – juanchopanza May 05 '13 at 08:57
  • O.K. let us concentrate on the first suggestion, namely: float data[2][5] = {{1,2,3,4,5},{7,8,9,10,11}}; Mat H=Mat(2,5,CV_32FC1,data); cout << H; The error message after getting to this line is: Unhandled exception at 0x6913DE19 (msvcp100.dll) in openCV 3.exe: 0xC0000005: Access violation reading location 0x00000000. – user2351428 May 05 '13 at 09:16

1 Answers1

1

Main problem of your code is that data[2][5] is a float matrix and H is a matrix of unsined character.

Declare the matrix H as -

Mat H=Mat(2,5,CV_32FC1,data);

Your second problem is very simple

To read a image as a cvMat object and display it, just do -

Mat M = imread("/home/Pictures/image.png",1);
imshow("IMAGE",M);
waitKey(0);
Saikat
  • 1,209
  • 3
  • 16
  • 30
  • Maybe it comes from a bad configuration, but I have seen that other users had encoutered the same problems... – user2351428 May 05 '13 at 08:32
  • what do you meant by "bad configuration " ? – Saikat May 05 '13 at 08:37
  • I have tried for days to follow all kinds of instructions in the WEB while installing the openCV - adding including to different directories and libraries, also with the extensions ending with d (debug) and the imread just wouldn't work. Untill I saw many users had the same problem which could be solved as written (by passing from C to C++). – user2351428 May 05 '13 at 08:42
  • In that case ,before adding any more comment, install OpenCV properly in your system. – Saikat May 05 '13 at 08:48
  • This exactely the problem, and aparently I am not the only one having it - try to google "error imread" and read about so many users that share the same problem. Believe me I tried to install it so many times in so many ways. It just wouldn't work. So I adapted the solution written above (from C to C++). – user2351428 May 05 '13 at 09:21
  • Which version of opencv you are using ? Can you give the full code you are trying to compile ? – Saikat May 05 '13 at 11:48