Problem in conversion from float* to cv::mat

asked 2015-10-28 01:45:28 -0500

Dayama Pradeep gravatar image

updated 2015-10-29 00:55:56 -0500

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

edit retag flag offensive close merge delete

Comments

1

please avoid those horrible per-pixel loops, it's the wrong approach.

Mat src = .....
Mat dst;
src.convertTo(dst,CV_32F);
float *data = dst.ptr<float>();
berak gravatar imageberak ( 2015-10-28 01:54:10 -0500 )edit
1

That's great..!! Its working..

But how do i convert back to cv::mat(string), such that i can print an image using imwrite

my plan was to convert an image from cv::mat to float*, do some image processing, again convert back to cv::mat and print it

for converting it to cv:mat i m using this

Mat dest(height, width, CV_32FC1, data);

is this fine?? i mean, am i heading in correct direction??

Dayama Pradeep gravatar imageDayama Pradeep ( 2015-10-28 02:28:38 -0500 )edit

cv::mat(string) - there is no such thing.

you can convertTo() back to CV_8U in the same way as above (it probably needs scale/offset then, too)

why do you think, you need float data in the 1st place ?

berak gravatar imageberak ( 2015-10-28 02:46:52 -0500 )edit

I tried converting as you said i.e. using CV_8U but i get a 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.

code snippet is here

Mat dest;
dst.convertTo(dest,CV_8U);

i have also tried with other formats like CV_8UC1, CV_8UC3 etc.. but all gives same error

Dayama Pradeep gravatar imageDayama Pradeep ( 2015-10-28 02:57:22 -0500 )edit

can you edit your question, and append the code, youre using now ?

berak gravatar imageberak ( 2015-10-28 03:05:45 -0500 )edit

Edited the question.. Please check it @berak

Dayama Pradeep gravatar imageDayama Pradeep ( 2015-10-28 04:04:42 -0500 )edit
1

for your 1st attempt: try to check, if the image was really loaded:

if (src.empty()) { cerr << "no image"; return -1;}

for the second: you can't write float data with imwrite() (unless you compiled exr/hdr support in)

berak gravatar imageberak ( 2015-10-28 04:47:12 -0500 )edit

sorry I didn't said you prior, In the above code I'm using imshow for source file, which results in displaying the image and hence the image is getting loaded properly

Dayama Pradeep gravatar imageDayama Pradeep ( 2015-10-28 04:55:53 -0500 )edit

can it be , you're linking the wrong libs ? should be opencv_world300d.lib for DEBUG (and without d for RELEASE)

berak gravatar imageberak ( 2015-10-29 01:04:27 -0500 )edit

@beark Thanks a lot... finally i m getting the output as i need.. i ll reply to my question.. i.e. i ll add the answer myself.. please check it..

Dayama Pradeep gravatar imageDayama Pradeep ( 2015-10-29 02:01:41 -0500 )edit