Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

coversion of image from uchar to float

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...