How can I write an image of datatype uint16 to a file in MATLAB? I try to write to the file using the following command, but it gives me an error
imwrite(pimg, 'h44', 'jpg')
Error using writejpg>set_jpeg_props (line 183)
UINT16 image data requires bitdepth specifically set to
either 12 or 16.
Error in writejpg (line 49)
props = set_jpeg_props(data,varargin{:});
Error in imwrite (line 472)
feval(fmt_s.write, data, map, filename,
paramPairs{:});
Error in image16bit (line 666)
imwrite(imgnew1,'h44','jpg' );
You must specify the BitDepth option when saving JPEG images with more than 8-bits:
% note that 16-bit only accepts grayscale images
img = imread('peppers.png');
imwrite(im2uint16(img), '12bit.jpg', 'BitDepth',12);
imwrite(rgb2gray(im2uint16(img)), '16bit.jpg', 'BitDepth',16);
Unfortunately, not many programs have support for 12-bit/16-bit JPEG images, so you might not be able to open it externally. You could use the JPEG2000 format instead:
imwrite(im2uint16(img), 'out.jp2');
Related
In Tensorflow decode_png and decode_jpg are used to decode data read in from a source (file, url,…).
I have medical type images in a 8192x8192 gray scale 16 bit uint format and don’t need png or jpg decoding. These medical images are external files.
Over the last week I’ve tried numerous approaches, sifted through stack overflow, Github and Tensorflow web pages but have not been able to resolve the issue of not being able to modify the code to read in non png or jpg data. I’m modifying the pix2pix.py code:
input_paths = glob.glob(os.path.join(a.input_dir, "*.jpg"))
decode = tf.image.decode_jpeg
if len(input_paths) == 0:
input_paths = glob.glob(os.path.join(a.input_dir, "*.png"))
decode = tf.image.decode_png
path_queue = tf.train.string_input_producer(input_paths, shuffle=a.mode == "train")
reader = tf.WholeFileReader()
paths, contents = reader.read(path_queue)
raw_input = decode(contents) #
raw_input = tf.image.convert_image_dtype(raw_input, dtype=tf.float32)
I’ve tried numerous approaches to get through/by/around: “raw_input = decode(contents)“
Thanks in advance for any help/pointers.
I want to save a DisplacementFieldTransform as an image in SimpleITK.
The documentation says:
A displacement field, such as one stored in a DisplacementFieldTransform object, can also be saved as an image (.nrrd, .nhdr, .mha, .mhd, .nii, .nii.gz).
But when I do
demons = sitk.FastSymmetricForcesDemonsRegistrationFilter()
demons.SetNumberOfIterations(15)
# Standard deviation for Gaussian smoothing of displacement field
demons.SetStandardDeviations(1.0)
demons.AddCommand( sitk.sitkIterationEvent, lambda: command_iteration(demons) )
displacementField = demons.Execute(followup, baseline)
deformation_field = sitk.DisplacementFieldTransform(displacementField)
deformationFieldPath = os.path.join(destinationDataPath, str(patientId) + '_deformationField.nrrd')
sitk.WriteTransform(deformation_field, deformationFieldPath)
I get the following error:
RuntimeError: Exception thrown in SimpleITK WriteTransform: /tmp/SimpleITK-build/ITK/Modules/IO/TransformBase/include/itkTransformFileWriter.hxx:167:
itk::ERROR: TransformFileWriterTemplate(0x555a72b3e700): Could not create Transform IO object for writing file /home/amasson/fastai/EMISEP-differences/6_deformationField.nrrd
Tried to create one of the following:
HDF5TransformIOTemplate
HDF5TransformIOTemplate
MatlabTransformIOTemplate
MatlabTransformIOTemplate
TxtTransformIOTemplate
TxtTransformIOTemplate
You probably failed to set a file suffix, or
set the suffix to an unsupported type.
How can I save my DisplacementFieldTransform as an image?
The DisplacementFieldTransform has a GetDisplacementField (and GetInverseDisplacementField) method that returns an Image. With that Image object you can call WriteImage and export it to whichever image format you prefer.
Here's the documentation for the DisplacementFieldTransform class: https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1DisplacementFieldTransform.html
I need to convert a 8 bit png image into 24 or 32 bit png.
I understand the corresponding image magic command to convert this is:
convert test.png PNG24:test2.png
What ImageOperation property should be used to pass PNG24 argument to convert the image to 24 bit.
I have the current java code snippet something like below:
IMOperation op = new IMOperation();
op.addImage();
op.background("none");
op.autoOrient();
op.addImage();
//What should I add for converting it to a PNG24 format???
convert.run(op,sourceFile,destFile);
The input image is a 8 bit png.
After some research this is what I did to fix it.
IMOperation op = new IMOperation();
op.addImage();
op.background("none");
op.autoOrient();
op.addImage();
//Added the following line to fix it
destFile = "png32:"+destFile;
convert.run(op,sourceFile,destFile);
I'm having a lot of difficulty working with PHP's Imagick class, it doesn't seem to want to read images regardless to the method I use.
Method 1
$handle = fopen('http://ima.gs/Placeholder-400x200.png', 'rb'); // Sample image
$img = new Imagick();
$img->readImageFile($handle);
$img->resizeImage(128, 128, 0, 0);
$img->writeImage(ROOT . DS . 'foo.png');
This gives me the Internal Error "Unable to read image from the filehandle"
Method 2 (Ideal Method)
$img = new Imagick(ROOT . DS . '00053141.jpg'); // Image does exist
$img->resizeImage(128, 128, 0, 0);
$img->writeImage(ROOT . DS . 'foo.jpg');
This gives me the Internal Error "no decode delegate for this image format `D:\Work\DittoCake\00053141.jpg' # error/constitute.c/ReadImage/550"
Configure List Results
You can see my delegates and configuration details here: http://cl.ly/image/1j2z1H072K41/Image%202014-10-22%20at%209.35.28%20AM.png
Command line convert results
convert 00053141.jpg -set colorspace RGB 00053141_rgb.jpg
This worked successfully and can confirm that the image mode was changed from CMKY to RGB when checking in Photoshop. (My last resort is to use shell_exec but I'd prefer not to)
I have a feeling this may be because I'm running it on Windows, my main goal in using this is just to convert any image from CMKY to RGB, resizing / changing image type is already covered in my application.
I am attempting to write a function that stack up series of images into image stack and converting it into a gdf file. I don't really know much about GDF files, so please help me out.
X=[];
for i=1:10
if numel(num2str(i))==1
X{i}=imread(strcat('0000',num2str(i),'.tif'));
elseif numel(num2str(i))==2
X{i}=imread(strcat('000',num2str(i),'.tif'));
end
end
myImage=cat(3,X{1:10});
s=write_gdf('stack.gdf',myImage);
Above is to read my images labeled 00001 to 00010, all in grayscale. Everything is fine except in the last line
s=write_gdf('stack.gdf',myImage);
as when I run it, I receive an error:
Data type uint8 not supported
Any help on what this means? Should I convert it to some other colour format?
Thank you in advance!
I would write the code rather like this (I do not have write_gdf function so I can not properly test the code):
NumberOfFiles = 10;
X={}; % preallocate CELL array
for n=1:NumberOfFiles % do not use "i" as your varable because it is imaginary unit in MatLab
FileName = sprintf('%05d.tif',n);
img = imread(FileName); % load image
X{i} = double(img); % and convert to desired format
end
myImage = cat(3,X{1:NumberOfFiles});
s = write_gdf('stack.gdf',myImage);
Keep in mind that
double(img); % and convert to desired format
will not change data range. Your image even in double format will have data range from 0 to 255 if it was in uint8 format on disk. If you need to normalize your data to 0..1 range you should do
X{i} = double(img)/255;
or in more unversal form
X{i} = double(img) / intmax(class(img));