Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to convert cv::Mat to float* in vs2013

Hello I m using VS2013 and openvc 3.0

I'm trying to convert a image from CV::mat to float* I have found some code snippet but couldn't understand anything.. Here is the code snippet

  `
   #include<iostream>
    #include<opencv2/opencv.hpp>

   using namespace std;
   using namespace cv;

   int main()
  {
        Mat img = imread("foggyHouse.jpg");
        int height = img.rows;
        int width = img.cols;
     //conversion from cv::mat to float*
      float* cpu_image = (float *)malloc((size+1) * 3 * sizeof(float));
       for (int i = 0; i < height; i++)
           {
          for (int j = 0; j < width; j++)
           {
                for (int k = 0; k < 3; k++)
                 {
                   cpu_image[(i * width + j) * 3 + k] = img.at<Vec<float, 3> >(i, j)[k];
                  }
            }
            }

    //converting back from float* cv::mat
    Mat dest(height, width, CV_32FC1, cpu_image);
imwrite("output.jpg",dest);
    return 0;

} ` the problem is I couldn't understand the logic behind the conversion from CV:MAT to float* n vice versa Can anyone explain me??

How to convert cv::Mat to Problem in conversion from float* in vs2013to cv::mat

Hello I m I'm using VS2013 and openvc 3.0

Here I'm trying to read an image, convert a image from CV::mat to float* I have found it to float* from CV::MAT, do some code snippet but couldn't understand anything.. processing, and again convert it back to CV::MAT from float* Here is the code snippet

  `
   #include<iostream>
    #include<opencv2/opencv.hpp>

   using namespace std;
   using namespace cv;

   int main()
  {
        Mat img { 
        //read the image
        Mat src = imread("foggyHouse.jpg");
        int height = img.rows;
        int width = img.cols;
     //conversion from cv::mat to float*
      float* cpu_image = (float *)malloc((size+1) * 3 * sizeof(float));
       for (int i = 0; i < height; i++)
           {
          for (int j = 0; j < width; j++)
           {
                for (int k = 0; k < 3; k++)
                 {
                   cpu_image[(i * width + j) * 3 + k] = img.at<Vec<float, 3> >(i, j)[k];
                  }
            }
            }

    //converting 
       //convert to float*
        Mat dst;
    src.convertTo(dst, CV_32F);
        float *data = dst.ptr<float>();

       //convert back from float* cv::mat
    Mat dest(height, width, CV_32FC1, cpu_image);
imwrite("output.jpg",dest);
to CV::MAT from float*
       Mat dest;
   dst.convertTo(dest,CV_8U);

      //print image
       imwrite("IMg.jpg", dest);

    return 0;

} ` the problem is I couldn't understand the logic behind the conversion I'm getting an error saying

First-chance exception at 0x528E0A14 (opencv_world300.dll) in Project3.exe: 0xC0000005: Access violation reading location 0x00137000. Unhandled exception at 0x528E0A14 (opencv_world300.dll) in Project3.exe: 0xC0000005: Access violation reading location 0x00137000.

i tried other method to convert from CV:MAT to float* here is the code

int height = src.rows;
int width = src.cols;

   //convert from CV::MAT to float*
Mat dst;
src.convertTo(dst, CV_32F);
float *data = dst.ptr<float>();

    //convert from float* n vice versa
Can anyone explain me?? to CV::MAT
    Mat dest(height, width, CV_32FC1, data);
imwrite("IMg.jpg", dest);

this is also giving the same error

Any suggestion or help will be appreciated.

Thanks

Problem in conversion from float* to cv::mat

Hello I'm using VS2013 and openvc 3.0

Here I'm trying to read an image, convert it to float* from CV::MAT, do some processing, and again convert it back to CV::MAT from float* Here is the code snippet

  `
   #include<iostream>
    #include<opencv2/opencv.hpp>

   using namespace std;
   using namespace cv;

   int main()
  { 
        //read the image
        Mat src = imread("foggyHouse.jpg");

       //convert to float*
        Mat dst;
    src.convertTo(dst, CV_32F);
        float *data = dst.ptr<float>();

       //convert back to CV::MAT from float*
       Mat dest;
   dst.convertTo(dest,CV_8U);

      //print image
       imwrite("IMg.jpg", dest);

    return 0;
 }

} ` the problem is I'm getting an error saying

First-chance exception at 0x528E0A14 (opencv_world300.dll) in Project3.exe: 0xC0000005: Access violation reading location 0x00137000. Unhandled exception at 0x528E0A14 (opencv_world300.dll) in Project3.exe: 0xC0000005: Access violation reading location 0x00137000.

i tried other method to convert from CV:MAT to float* here is the code

int height = src.rows;
int width = src.cols;

   //convert from CV::MAT to float*
Mat dst;
src.convertTo(dst, CV_32F);
float *data = dst.ptr<float>();

    //convert from float*  to CV::MAT
    Mat dest(height, width, CV_32FC1, data);
imwrite("IMg.jpg", dest);

this is also giving the same error

Any suggestion or help will be appreciated.

Thanks

Problem in conversion from float* to cv::mat

Hello I'm using VS2013 and openvc 3.0

Here I'm trying to read an image, convert it to float* from CV::MAT, do some processing, and again convert it back to CV::MAT from float* Here is the code snippet

  `
   #include<iostream>
    #include<opencv2/opencv.hpp>

   using namespace std;
   using namespace cv;

   int main()
  { 
        //read the image
        Mat src = imread("foggyHouse.jpg");

       //convert to float*
        Mat dst;
    src.convertTo(dst, CV_32F);
        float *data = dst.ptr<float>();

       //convert back to CV::MAT from float*
       Mat dest;
   dst.convertTo(dest,CV_8U);

      //print image
       imwrite("IMg.jpg", dest);

    return 0;
 }

` the problem is I'm getting an error saying

First-chance exception at 0x528E0A14 (opencv_world300.dll) in Project3.exe: 0xC0000005: Access violation reading location 0x00137000. Unhandled exception at 0x528E0A14 (opencv_world300.dll) in Project3.exe: 0xC0000005: Access violation reading location 0x00137000.

i tried other method to convert from CV:MAT to float* here is the code

int height = src.rows;
int width = src.cols;

   //convert from CV::MAT to float*
Mat dst;
src.convertTo(dst, CV_32F);
float *data = dst.ptr<float>();

    //convert from float*  to CV::MAT
    Mat dest(height, width, CV_32FC1, data);
imwrite("IMg.jpg", dest);

this is also giving the same error

Any suggestion or help will be appreciated.

Thanks