0

I have a single channel of 64-bit floats image that I am trying to transform into an unsigned char using OpenCV. I can successfully visualize the image and resize it as it is too big. However when I am trying to transform the resized image into an unsigned char I don't see anything. I am doing the transformation using the following function as advised here.

I initially tried const uchar* inBuffer = desc.data; to transform it but according to the same source it seems to be unsafe and therefore opted for a recasting method. That also didn't work but that it seemed at my best understanding the best choice. The code is below:

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    String imageName( "/home/to/Desktop/Myexample.tif" );

    if( argc > 1)
    {
        imageName = argv[1];
    }
    Mat image;
    Mat outImage;
    Mat corrected;
    // Read the file
    image = cv::imread( imageName, IMREAD_UNCHANGED );
    // Check for invalid input
    if(image.empty())
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }
    cv::resize(image, outImage, Size(800,800));
    cv::namedWindow("Resized", WINDOW_AUTOSIZE);
    cv::imshow("Resized", outImage+220);

    // Transformation of the resized image into a unsigned char for better visualization
    cv::resize(outImage, corrected, Size(800,800));
    cv::namedWindow("Corrected", WINDOW_AUTOSIZE);

    // From here nothing is showing up
    unsigned char const* inBuffer = reinterpret_cast<unsigned char const*>(outImage.data);
    cv::imshow("Corrected", *inBuffer);
    cv::waitKey(0);
    return 0;
}

Another thing I thought could have been useful is from the following source where it was advised to use a double conversion. I understand that it is fast in terms of computation but at the same time this didn't give me any useful result.

Thank you in advance for shedding light on this matter.

Emanuele
  • 2,194
  • 6
  • 32
  • 71
  • I don't think this `reinterpret_cast` does what you think it does. It doesn't convert anything, in fact it does the opposite, it forces a different interpretation on the data **without** converting it. Newbies often miunderstand casting, thinking that it does more than it actually does. If you want to convert the data you have to do some work, in your case I would guess (I'm not an OpenCV expert) that means multiplying each float value by 255.0 and then assigning the resulting value to a `unsigned char`. – john Jul 16 '19 at 20:58
  • You already have a corrected image, why not `cv::imshow("Corrected", corrected)`? – Tony J Jul 16 '19 at 21:07
  • What do you want to achieve by `transform into an unsigned char`? Do you mean RGBA where each channel is the size of 1 byte and can be represented by a unsigned char? https://stackoverflow.com/questions/10265125/opencv-2-3-convert-mat-to-rgba-pixel-array – Tony J Jul 16 '19 at 21:13
  • Thank you all for the fast answers. @john, thanks for the explanation about the `reinterpret cast` function. @Tony J, I already tried it and I only see a black image – Emanuele Jul 16 '19 at 21:29
  • @Tony J, the resized image I see does not carry enough particulars and therefore I need to read the resized image as `unsigned char` to be able to see more particulars. – Emanuele Jul 16 '19 at 21:31
  • @Emanuele, `cv::show` takes a InputArray as second parameter, `*inBuffer` the star deference the array, which will only be a unsigned char, so it won't work. `cv::imshow("Resized", outImage+220);` I'm not sure what +220 would do, so maybe that's why you are seeing black image. – Tony J Jul 16 '19 at 21:44
  • @TonyJ, the statement `cv::imshow("Resized", outImage+220);` works well, in fact I can see the resized image. What is not working is the following: `cv::imshow("Corrected", *inBuffer);` that is the statement responsible for the `unsigned char` transformation – Emanuele Jul 16 '19 at 22:01
  • Before showing it I am trying to `recast` the image using :`unsigned char const* inBuffer = reinterpret_cast(outImage.data);` – Emanuele Jul 16 '19 at 22:04

0 Answers0