Using OpenCV converting imread Mat's unsignedchar data to double - double

Im coing in C++.After reading an input image into OpenCV using imread, for further processing i need to convert the image data into double.
How can it be done.Is there any pre-defined CV function...?

Related

It appears imageLabeler cannot handle a transformed datastore?

The Matlab app imageLabeler is supposed to support the following format:
imageLabeler(imgStore)
I have an imgStore, defined as follows:
imds = imageDatastore(cellArrayOfImageFilenames);
imgStore = transform(imds, #(x)demosaic(x,'rggb'));
I have to do this, because my images are stored as bayer encoded images, and this is the only way I've figured out to get the imgStore to return these images as 3 channel RGB images. However, when I try and initalize imageLabeler, I get this error:
>> imageLabeler(imgStore)
Error using imageLabelerInternal
Expected input name to be one of these types:
char
Instead its type was matlab.io.datastore.TransformedDatastore.
Error in vision.internal.imageLabeler.imageLabelerInternal
Error in imageLabeler (line 58)
vision.internal.imageLabeler.imageLabelerInternal(varargin{:});
TLDR:
How do I get imageLabeler to handle my bayer encoded images?
The way to fix this, is with the imageDatastore 'ReadFcn' parameter. The documentation for imageDatastore explicity tells you to NOT do this, as it slows down Neural Network stuff. Here's the Matlab doc text:
Using ReadFcn to transform or pre-process 2-D images is not
recommended. For file formats recognized by imformats, specifying
ReadFcn slows down the performance of imageDatastore. For more
efficient ways to transform and pre-process images, see Preprocess
Images for Deep Learning (Deep Learning Toolbox).
So, all that said, here's the workaround:
imgStore = imageDatastore(cellArrayOfImageFilenames ...
, 'ReadFcn', #(x)demosaic(imread(x),'rggb')));

How to convert PPM images to JPG in Matlab?

I have some PPM images (stereo) that I read with imread() and I want to save the same images in JPEG with different Quality factors.
Here is my code.
%Read PPM image
L = imread(filename_L);
%Create JPEG Q85 from PPM
filename_L85 = strcat(filename_L,'_ppm_to_jpeg.jpg');
imwrite(L,filename_L85,'JPEG','Quality',85);
And here the error I get.
Error using imwrite>parse_inputs (line 528)
The colormap should have three columns.
Error in imwrite (line 418)
[data, map, filename, format, paramPairs] = parse_inputs(varargin{:});
Error in testFinale (line 75)
imwrite(L,filename_L85,'JPEG','Quality',85);
How can I write JPEG images previously read in PPM format?
Thanks
Could it be that is just has to do with your case of 'JPEG', the documentation of imwrite specifies parameters for file type as lowercase.
Apart from that you might not even need it as the file type is derived from the extension which in this case is set explicitly to .jpg already.
So you might either go for:
imwrite(L,filename_L85,'jpeg','Quality',85);
or perhaps even easier:
imwrite(L,filename_L85,'Quality',85);

how to convert .mat file into bitmap in Matlab

I am trying to convert a .mat file into a bitmap image in Matlab but I cannot seem to find a way to do it. This is my current code:
human_seg = load(human_img);
where the human_img is a .mat file. I need to then convert the human_seg into a bmp but when I try it I recieve the error
Conversion to logical from struct is not possible.
Try this instead
imwrite(human_seg,'Imagefile1.bmp')

Matlab convert DCM to BMP

How to convert .dcm(dicom) image to .bmp format.
I have tried this, but its not working for me.
000000.dcm is a medical lung image (size 6 MB)
dcmimg = dicomread('000000.dcm');
>> imshow(dcmimg);
DCM image read in dcmimg
imwrite(dcmimg, 'myfile.bmp');
Getting error:
Error using writebmp (line 15)
Expected X to be one of these types:
logical, uint8, single, double
Instead its type was uint16.
Error in imwrite (line 477)
feval(fmt_s.write, data, map, filename, paramPairs{:});
I am not understanding these errors, What's going wrong. Help me out, Thank You.
Dicom uses unsigned 16 bit integers. The function that writes .bmp files expects a different data type, as explained by the error message. This suggests that the following should work:
imwrite(double(dcmimg), 'myFile.bmp');
By explicitly converting the data type to double you should get rid of the error. Actually single might be enough... It too can represent 16 bit integers without loss of precision.

Is there a function in MATLAB that converts a .raw file into a matrix?

I want to import a .raw file into MATLAB as a matrix (frames x spatial x spectral). Is there a built-in function to do this?
If you're referring to a raw image file from a camera, I would check out the submission RAW Camera File Reader from Bryan White on the MathWorks File Exchange.
You can also read the file directly. But you'll need to convert it to DNG first. See this:
http://blogs.mathworks.com/steve/2011/03/08/tips-for-reading-a-camera-raw-file-into-matlab/