coversion of image from uchar to float [closed]
Hello
I m trying to convert an image from uchar to float but i get an error as Assertion failed
here is my code snippet
#include <stdio.h>
#include "opencv2/opencv.hpp"
using namespace cv;
using namespace std;
Mat img = imread(img_name);
if (img .empty())
{
cerr << "no image"; return -1;
}
height = img.rows;
width = img.cols;
size = img.rows*img.cols;
float* cpu_image = (float *)malloc((size+1) * 3 * sizeof(float));
if (!cpu_image)
{
std::cout << "ERROR: Failed to allocate memory" << std::endl;
return -1;
}
//converting image from uchar to float* pixcel-wise
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];
}
}
}
cpu_image[size] = 0;
cpu_image[size+1] = 0;
cpu_image[size+2] = 0;
//converting image back to uchar from float
Mat dest(height, width, CV_32FC3, cpu_image);
imwrite(out_name, dest);
free(cpu_image);
return 0;
}
Any advice pls...
please use Mat::convertTo() instead. also have a look at the tutorials
if you see yourself writing per-pixel loops, chances are 99% that you're doing it wrong.
then, you can't write float images using imwrite(), and no, Mat dest is still a float image
@berak, Hello again.. we had this same discussion on conversion of char to float here is the discuddion but when i do some image processing operation on that converted image i get problems
so, you unfortunately did not improve at all ;( above code is a step backwards , even.
why do you think, you need a conversion to float at all ? (it's all a bit out of context here.)
@berak, the reason i want to convert image into float is coz the processing of image will be more precise. After that previous disscusion we had on the same topic, i use the logic for conversion what we discussed then, but i t seems to be failed at the time of image processing
this is not going anywhere.
besides.. malloc() is not what you should use if your doing c++. And if you encounter problems 'when i do some image processing' its better to describe what the 'problems' and 'some image processing' are. Now we have no idea!
Sorry guys, Actually i was not getting proper output so i was doubting my image conversion from uchar to float, the logic for conversion which i arrived after a pretty long discussion with @berak is right. the problem which i rectified is with the Logic i applied for image processing @boaz001 Image processing i m doing on image is to findout minumum of RGB in an image and make it a single channel image, after this Add a patch to image with patch size of 15x15, and find minimum value of current pixcel in that patch, then move the patch further. This is the image processing Stuff i m doing. Error which I encountered during image processing is, the output should be a Dark Gray Scale image, but what I was getting is a Full Blank image as that of Blank Computer monitor after shut down