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
please avoid those horrible per-pixel loops, it's the wrong approach.
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
is this fine?? i mean, am i heading in correct direction??
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 ?
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
i have also tried with other formats like CV_8UC1, CV_8UC3 etc.. but all gives same error
can you edit your question, and append the code, youre using now ?
Edited the question.. Please check it @berak
for your 1st attempt: try to check, if the image was really loaded:
for the second: you can't write float data with imwrite() (unless you compiled exr/hdr support in)
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
can it be , you're linking the wrong libs ? should be
opencv_world300d.lib
forDEBUG
(and without d for RELEASE)@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..