coversion of image from uchar to float [closed]

asked 2015-11-05 00:22:52 -0500

Dayama Pradeep gravatar image

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

edit retag flag offensive reopen merge delete

Closed for the following reason duplicate question by Dayama Pradeep
close date 2015-11-07 10:11:11.536559

Comments

3

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 gravatar imageberak ( 2015-11-05 00:49:53 -0500 )edit

@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

Dayama Pradeep gravatar imageDayama Pradeep ( 2015-11-05 00:57:36 -0500 )edit
1

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 gravatar imageberak ( 2015-11-05 01:03:40 -0500 )edit

@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

Dayama Pradeep gravatar imageDayama Pradeep ( 2015-11-05 01:14:39 -0500 )edit
2

this is not going anywhere.

berak gravatar imageberak ( 2015-11-05 01:32:26 -0500 )edit

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!

boaz001 gravatar imageboaz001 ( 2015-11-05 07:31:26 -0500 )edit

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

Dayama Pradeep gravatar imageDayama Pradeep ( 2015-11-06 04:00:28 -0500 )edit