returning a ublas matrix from a function

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

returning a ublas matrix from a function

sanann
hello,
I am trying to return a array of matrices from a function but when i use return there is always an error can you please help me.


typedef boost::numeric::ublas::matrix<double> BMT;

     BMT fun();
       {
       BMT mym;
       mym.resize(3,3);
       for(int i = 0; i<9;++i) mym(i/3,i%3)=i;
       std::cout << mym << std::endl;
       return mym;
       }
error: cannot convert 'BMT {aka boost::numeric::ublas::matrix<double>}' to 'int' in return
        return mym;
               ^

why do i get this error how do i overcome it.
Reply | Threaded
Open this post in threaded view
|

Re: returning a ublas matrix from a function

Oswin Krause
Hi,

the error lies in the fact that it has to be "BMT fun()" and not "BMT
fun();" and is not an ublas error.

Regards,
Oswin

On 05.08.2014 10:05, sanann wrote:

> hello,
> I am trying to return a array of matrices from a function but when i use
> return there is always an error can you please help me.
>
>
> typedef boost::numeric::ublas::matrix<double> BMT;
>
>       BMT fun();
>         {
>         BMT mym;
>         mym.resize(3,3);
>         for(int i = 0; i<9;++i) mym(i/3,i%3)=i;
>         std::cout << mym << std::endl;
>         return mym;
>         }
> error: cannot convert 'BMT {aka boost::numeric::ublas::matrix<double>}' to
> 'int' in return
>          return mym;
>                 ^
>
> why do i get this error how do i overcome it.
>
>
>
> --
> View this message in context: http://boost.2283326.n4.nabble.com/returning-a-ublas-matrix-from-a-function-tp4666050.html
> Sent from the Boost - uBLAS mailing list archive at Nabble.com.
> _______________________________________________
> ublas mailing list
> [hidden email]
> http://lists.boost.org/mailman/listinfo.cgi/ublas
> Sent to: [hidden email]

_______________________________________________
ublas mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: returning a ublas matrix from a function

sanann
Thank you very much for that.

but on the other hand i tried the same with another part of my code but i still get the same error. could you kindly help me through this too.

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    const size_t arraySize = 3;
    boost::numeric::ublas::matrix<double> M[arraySize] =
    {
        boost::numeric::ublas::scalar_matrix<double>(2, 2),
        boost::numeric::ublas::scalar_matrix<double>(3, 3),
        boost::numeric::ublas::scalar_matrix<double>(4, 4)
    };


     for (int i=0;i<4;i++)
     {
         std::cout << M[i]<< std::endl;
     }
     return M;
}

I end up with the same error.
error: invalid conversion from 'boost::numeric::ublas::matrix<double>*' to 'int' [-fpermissive]
      return M;
             ^
Reply | Threaded
Open this post in threaded view
|

Re: returning a ublas matrix from a function

PB-19
You've declared main() as returning int but you're trying to return a type that isn't an int. What do you expect to happen? The compiler error is pretty clear here IMO.


On Tue, Aug 5, 2014 at 9:27 AM, sanann <[hidden email]> wrote:
Thank you very much for that.

but on the other hand i tried the same with another part of my code but i
still get the same error. could you kindly help me through this too.

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    const size_t arraySize = 3;
    boost::numeric::ublas::matrix<double> M[arraySize] =
    {
        boost::numeric::ublas::scalar_matrix<double>(2, 2),
        boost::numeric::ublas::scalar_matrix<double>(3, 3),
        boost::numeric::ublas::scalar_matrix<double>(4, 4)
    };


     for (int i=0;i<4;i++)
     {
         std::cout << M[i]<< std::endl;
     }
     return M;
}

I end up with the same error.
error: invalid conversion from 'boost::numeric::ublas::matrix<double>*' to
'int' [-fpermissive]
      return M;
             ^



--
View this message in context: http://boost.2283326.n4.nabble.com/returning-a-ublas-matrix-from-a-function-tp4666050p4666054.html
Sent from the Boost - uBLAS mailing list archive at Nabble.com.
_______________________________________________
ublas mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: [hidden email]


_______________________________________________
ublas mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: returning a ublas matrix from a function

sanann
Thank you. I am just a beginner, i am more used to matlab due to which i get confused quite quickly. However i slowly came over the problem. but i now face a new problem if possible for you can explain with a little patience it would be of great help.


#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>

using namespace boost;
using namespace boost::numeric::ublas;


typedef boost::numeric::ublas::matrix<double> BMT;

BMT fun(int w,int h)
  {

    BMT M[3]=
          {
        boost::numeric::ublas::scalar_matrix<double>(w, h),
        boost::numeric::ublas::scalar_matrix<double>(w, h),
        boost::numeric::ublas::scalar_matrix<double>(w, h)
    };

  return M[2];
  }

int main()
{

    unsigned w,h;
    w = 2;
    h = 2;
    boost::numeric::ublas::scalar_matrix<double> Z[3];
    Z[3]=fun(w,h);

    std::cout<<fun<<std::endl;
    return 0;
}

and I end up with error: no match for 'operator=' (operand types are 'boost::numeric::ublas::scalar_matrix<double>' and 'BMT {aka boost::numeric::ublas::matrix<double>}')
     Z[3]=fun(w,h);
         ^

is my approach correct or have I misunderstood something?
thanks in advance
Reply | Threaded
Open this post in threaded view
|

Re: returning a ublas matrix from a function

sanann
In reply to this post by PB-19
The basic thing that i want to do is
1. have a bunch of matrices either in a function or in a class.
2. these would be manipulated and the output needs to be returned inorder to use it for further manipulation.
3. so assign 3 matrices into an array.
4. now th problem i face is using this array in an external code.

for example.

#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>

using namespace boost;
using namespace boost::numeric::ublas;


typedef boost::numeric::ublas::matrix<double> BMT;

BMT fun(int w,int h)
  {

    BMT M[3]=
          {
        scalar_matrix<double>(w, h),
        scalar_matrix<double>(w, h),
        scalar_matrix<double>(w, h)
    };
    std::cout<<M[0]<<std::endl;
  return M[0];
  }

here i want to have all the matrices stored in array M how do i do that?

int main()
{

    unsigned w,h;
    w = 3;
    h = 3;

    fun(w,h);

    std::cout<<fun<<std::endl;
    return 0;
}

it would be of great help if you can guide me
Reply | Threaded
Open this post in threaded view
|

Re: returning a ublas matrix from a function

Nasos Iliopoulos
Sannan,
prior to diving in ublas I would suggest spending some time learning
C++, since it is not a trivial language. As long as you have some
programming experience a good introductory book would be "Accelerated
C++ by Koening and Moo".

Anyway If you are using a C++11 compiler this will work and is
preferable from the C array (please note that there are a few
performance improvements that might take place here):
[for gcc compile with: g++ main.cpp -o myprog -std=c++11]

#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <array>

using namespace boost;
using namespace boost::numeric::ublas;

typedef boost::numeric::ublas::matrix<double> BMT;

std::array<BMT, 3> fun(int w,int h)
{
     std::array<BMT, 3> M ={
         scalar_matrix<double>(w, h),
         scalar_matrix<double>(2*w, h),
         scalar_matrix<double>(3*w, h)
     };

     return M;
}

int main()
{

     unsigned w,h;
     w = 3;
     h = 3;

     auto matrices = fun( w, h);

     std::cout << matrices[0] << std::endl;
     std::cout << matrices[1] << std::endl;
     std::cout << matrices[2] << std::endl;

     return 0;
}

Best,
Nasos

On 08/05/2014 09:00 AM, sanann wrote:

> The basic thing that i want to do is
> 1. have a bunch of matrices either in a function or in a class.
> 2. these would be manipulated and the output needs to be returned inorder to
> use it for further manipulation.
> 3. so assign 3 matrices into an array.
> 4. now th problem i face is using this array in an external code.
>
> for example.
>
> #include <boost/numeric/ublas/matrix.hpp>
> #include <boost/numeric/ublas/io.hpp>
>
> using namespace boost;
> using namespace boost::numeric::ublas;
>
>
> typedef boost::numeric::ublas::matrix<double> BMT;
>
> BMT fun(int w,int h)
>    {
>
>      BMT M[3]=
>            {
>          scalar_matrix<double>(w, h),
>          scalar_matrix<double>(w, h),
>          scalar_matrix<double>(w, h)
>      };
>      std::cout<<M[0]<<std::endl;
>    return M[0];
>    }
>
> here i want to have all the matrices stored in array M how do i do that?
>
> int main()
> {
>
>      unsigned w,h;
>      w = 3;
>      h = 3;
>
>      fun(w,h);
>
>      std::cout<<fun<<std::endl;
>      return 0;
> }
>
> it would be of great help if you can guide me
>
>
>
> --
> View this message in context: http://boost.2283326.n4.nabble.com/returning-a-ublas-matrix-from-a-function-tp4666050p4666070.html
> Sent from the Boost - uBLAS mailing list archive at Nabble.com.
> _______________________________________________
> ublas mailing list
> [hidden email]
> http://lists.boost.org/mailman/listinfo.cgi/ublas
> Sent to: [hidden email]

_______________________________________________
ublas mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: returning a ublas matrix from a function

sanann
Thank you for your reply it has helped me a lot. But further down the line i have encountered another problem. may be it is a small one or may be it is me who is understanding it wrong.
the situation i now face is

"boost::array<BMT,8> z;
    width = 3;
    height = 3;
    z=filter(width,height);
   
    w = w2(width,height,z);"

So basically what i have is a boost::array z which contains 8 different arrays now I want to send these eight arrays to another function which would use these arrays to perform more manipulations.

What would be the most ideal way to do this i went through pointers but i think i am making a mistake somewhere .

#ifndef W_H
#define W_H

#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <boost/numeric/ublas/vector.hpp>

typedef boost::numeric::ublas::matrix<double> w;

boost::array<w,8> w2(int width, int height,int z);

#endif // W_H

i used this method to define my new fucntion w. The error i end up with is that w2 is not defined for this scope. is there any way out of this ?

Reply | Threaded
Open this post in threaded view
|

Re: returning a ublas matrix from a function

sanann
In reply to this post by Nasos Iliopoulos
I am using boost for image processing, I found the boost::gil a bit too complicated for a newbie.. so instead i tried using other available libraries in boost to start off with.

So basically what i want to do is
1. Read a gray scale image.

2. Load the pixel values into a boost matrix.

3. Apply a random filter to the matrix.

4. convert the matrix back into and image for display.

so far what I have done is

   < QPixmap pixmap("lena.bmp");

   pixmap = pixmap.copy(512,512,128,128);
   pixmap = pixmap.scaled(128,128);
   QImage image = pixmap.toImage();
   QRgb col;

   int g;
   int width = pixmap.width();
   int height = pixmap.height();
   matrix<double> m(width,height);

   for (int j = 0; j < m.size2(); j++)
   {
       for (int i = 0; i < m.size1(); i++)
       {
            m(i,j) = qGray(image.pixel(i,j));
       }
   }>

so basically i guess the pixel values are being saved into a matrix 'm' now i would like to display this matrix as an image is there someway that i can do this?

thanks in advance
Reply | Threaded
Open this post in threaded view
|

Re: returning a ublas matrix from a function

sanann
In reply to this post by PB-19
I am using boost for image processing, I found the boost::gil a bit too complicated for a newbie.. so instead i tried using other available libraries in boost to start off with.

So basically what i want to do is
1. Read a gray scale image.

2. Load the pixel values into a boost matrix.

3. Apply a random filter to the matrix.

4. convert the matrix back into and image for display.

so far what I have done is

   < QPixmap pixmap("lena.bmp");

   pixmap = pixmap.copy(512,512,128,128);
   pixmap = pixmap.scaled(128,128);
   QImage image = pixmap.toImage();
   QRgb col;

   int g;
   int width = pixmap.width();
   int height = pixmap.height();
   matrix<double> m(width,height);

   for (int j = 0; j < m.size2(); j++)
   {
       for (int i = 0; i < m.size1(); i++)
       {
            m(i,j) = qGray(image.pixel(i,j));
       }
   }>

so basically i guess the pixel values are being saved into a matrix 'm' now i would like to display this matrix as an image is there someway that i can do this?

thanks in advance
Reply | Threaded
Open this post in threaded view
|

Re: returning a ublas matrix from a function

Joaquim Duran
Ublas is a library for algebra, so the matrices or vectors could not
be displayed as an image. IMO, you should:

1 - Copy the contents of a GIL image to a matrix.

2 - Manipulate the contents of the matrix: filter,....

3 - Copy the matrix to a GIL image

4 - Show the contents of the new image.

Joaquim Duran


2014-08-22 9:08 GMT+02:00 sanann <[hidden email]>:

> I am using boost for image processing, I found the boost::gil a bit too
> complicated for a newbie.. so instead i tried using other available
> libraries in boost to start off with.
>
> So basically what i want to do is
> 1. Read a gray scale image.
>
> 2. Load the pixel values into a boost matrix.
>
> 3. Apply a random filter to the matrix.
>
> 4. convert the matrix back into and image for display.
>
> so far what I have done is
>
>    < QPixmap pixmap("lena.bmp");
>
>    pixmap = pixmap.copy(512,512,128,128);
>    pixmap = pixmap.scaled(128,128);
>    QImage image = pixmap.toImage();
>    QRgb col;
>
>    int g;
>    int width = pixmap.width();
>    int height = pixmap.height();
>    matrix<double> m(width,height);
>
>    for (int j = 0; j < m.size2(); j++)
>    {
>        for (int i = 0; i < m.size1(); i++)
>        {
>             m(i,j) = qGray(image.pixel(i,j));
>        }
>    }>
>
> so basically i guess the pixel values are being saved into a matrix 'm' now
> i would like to display this matrix as an image is there someway that i can
> do this?
>
> thanks in advance
>
>
>
> --
> View this message in context: http://boost.2283326.n4.nabble.com/returning-a-ublas-matrix-from-a-function-tp4666050p4666752.html
> Sent from the Boost - uBLAS mailing list archive at Nabble.com.
> _______________________________________________
> ublas mailing list
> [hidden email]
> http://lists.boost.org/mailman/listinfo.cgi/ublas
> Sent to: [hidden email]
_______________________________________________
ublas mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: returning a ublas matrix from a function

sanann
Thank you Joaquim.

i thought of the same when i first started too.

so i used

    rgb8_image_t img;
    jpeg_read_image ("lena.jpg",img);

statements to load my image. but after this i didnot know how i can copy the contents of the image into a matrix or how to copy the matrix to a gil image. then i ended up trying to do it the way i did it before.

could you kindly shed some light on this? i went through the documentation and just got confused so i had to opt a simpler option. It would be really great if you have some kind of sample code for this as the samples in the documentation are too complicated.
 thank you once again
Reply | Threaded
Open this post in threaded view
|

Re: returning a ublas matrix from a function

Joaquim Duran
I don't have experience with GIL. However if you've experience with
matlab and you're interested in image processing in c++, I suggest to
change to OpenCV (a c++ library) because it has a matlab interface.

http://www.mathworks.es/discovery/matlab-opencv.html

Joaquim Duran


2014-08-22 9:39 GMT+02:00 sanann <[hidden email]>:

> Thank you Joaquim.
>
> i thought of the same when i first started too.
>
> so i used
>
>     rgb8_image_t img;
>     jpeg_read_image ("lena.jpg",img);
>
> statements to load my image. but after this i didnot know how i can copy the
> contents of the image into a matrix or how to copy the matrix to a gil
> image. then i ended up trying to do it the way i did it before.
>
> could you kindly shed some light on this? i went through the documentation
> and just got confused so i had to opt a simpler option. It would be really
> great if you have some kind of sample code for this as the samples in the
> documentation are too complicated.
>  thank you once again
>
>
>
> --
> View this message in context: http://boost.2283326.n4.nabble.com/returning-a-ublas-matrix-from-a-function-tp4666050p4666754.html
> Sent from the Boost - uBLAS mailing list archive at Nabble.com.
> _______________________________________________
> ublas mailing list
> [hidden email]
> http://lists.boost.org/mailman/listinfo.cgi/ublas
> Sent to: [hidden email]
_______________________________________________
ublas mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: returning a ublas matrix from a function

sanann
Hello , thank you very much for your previous response.It helped a lot.

I use the ublas matrix and would like to resize the matrix according to a new size. I see this statement in the documentation but donot understand how to use can you please shed some light on it or some kind of sample code or something.

void resize (size_type size1, size_type size2, bool preserve = true)