Im4Java (ImageMagick) convert 8 bit png to 24 bit png - image

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);

Related

Need to read in 16 bit unit gray scale not png or jpg data

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.

ImageIO.read is sporadically reading an image file

I am using the following Java code to read a Dicom image, trying to convert it later to JPEG file. When the reading happens in the line
tempImage = ImageIO.read(dicomFile);
, the returned image either has an image type of 10 or something else, like 0 or 11. The problem here is that the reading happens sporadically. Sometimes the returned image type is 10, and sometimes it is not.
When the returned image type is 10, the writing of the converted JPEG file succeeds and returns true and I get my JPEG file. However, when the returned image type is not 10, the writing fails and returns false, and doesn't produce any file. This is the statement I am using for writing:
writerReturn = ImageIO.write(image, "jpeg", new File(tempLocation + studyId + File.separator + seriesUID + File.separator + objectId + thumbnail+ ".jpeg"));
I have spent long time trying to figure out why this sporadic behaviour is happening but couldn't reach to anything. Could you please help?
I am guessing the issue is that your input image is 16bits while I am sure your code only accept 8bits input. You cannot write out using the so-called usual JPEG 8bits lossy format unless you transform your 16bits input.
On my box here is what I see:
$ gdcminfo 1.2.840.113619.2.67.2200970061.29232060605151433.387
MediaStorage is 1.2.840.10008.5.1.4.1.1.1.1 [Digital X-Ray Image Storage - For Presentation]
TransferSyntax is 1.2.840.10008.1.2.4.90 [JPEG 2000 Image Compression (Lossless Only)]
NumberOfDimensions: 2
Dimensions: (1887,1859,1)
SamplesPerPixel :1
BitsAllocated :16
BitsStored :14
HighBit :13
PixelRepresentation:0
ScalarType found :UINT16
PhotometricInterpretation: MONOCHROME2
PlanarConfiguration: 0
TransferSyntax: 1.2.840.10008.1.2.4.90
Group 0x6000
Rows 1859
Columns 1887
NumberOfFrames 0
Description
Type G
Origin[2] 1,1
FrameOrigin 0
BitsAllocated 1
BitPosition 0
Origin: (0,0,0)
Spacing: (0.187429,0.187429,1)
DirectionCosines: (1,0,0,0,1,0)
Rescale Intercept/Slope: (0,1)
Orientation Label: AXIAL
So if you want to convince yourself you could extract the encapsulated JPEG 2000 bytestream:
$ gdcmraw 1.2.840.113619.2.67.2200970061.29232060605151433.387 bug.j2k
$ file bug.j2k
bug.j2k: JPEG 2000 codestream
I was able to open the generated bug.j2k using either IrfanView and kdu_show, but as you can see the image is very dark (only the lower bits are read).
From extra information in the comments, we have discovered that the application is running in a Glassfish server, and that there are two ImageIO plugins installed, both capable of reading DICOM images. The problem is not really related to reading, but sometimes writing the decoded image to JPEG fails.
The service providers for mentioned plugins are org.dcm4cheri.imageio.plugins.DcmImageReaderSpi and
org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReaderSpi, but only the latter (DicomImageReaderSpi) seems to work. This is because it produces an 8 bits per sample BufferedImage, which is what the JPEGImageWriter is able to write (the DcmImageReaderSpi creates a 16 bit per sample image, which can't be written as a JFIF JPEG and thus unsupported by the JPEGImageWriter).
Because of the (by default) unspecified (read: unpredictable) order of ImageIO plugins, the result is that sometimes you get the 8 bps version and sometimes the 16 bit version of the image, and the end result is that sometimes the conversion won't work.
Now, the good news is that we can set an explicit order of ImageIO plugins, or we can unregister plugins at runtime, to get a stable predictable result. What is the better of these options, depends on wether there are other code on your server that depends on the undesired plugin or not. If you don't need it, unregister it.
The code below shows both of the above options:
// Get the global registry
IIORegistry registry = IIORegistry.getDefaultInstance();
// Lookup the known providers
ImageReaderSpi goodProvider = lookupProviderByName(registry, "org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReaderSpi");
ImageReaderSpi badProvider = lookupProviderByName(registry, "org.dcm4cheri.imageio.plugins.DcmImageReaderSpi");
if (goodProvider != null && badProvider != null) {
// If both are found, EITHER
// order the good provider BEFORE the bad one
registry.setOrdering(ImageReaderSpi.class, goodProvider, badProvider);
// OR
// un-register the bad provider
registry.deregisterServiceProvider(badProvider);
}
// New and improved (shorter) version. :-)
private static <T> T lookupProviderByName(final ServiceRegistry registry, final String providerClassName) {
try {
return (T) registry.getServiceProviderByClass(Class.forName(providerClassName));
}
catch (ClassNotFoundException ignore) {
return null;
}
}
You should also make sure you run this code only once, for a container-based application, a good time is at application context start-up.
With the above solution, ImageIO.read(...) will always use the good plugin, and ImageIO.write(...) will work as expected.

Write a uint16 image

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');

How to convert imageSet to idx3-ubyte format, using mnisten,

I am trying to use a CNN code to train 10 images stored in an imageSet. The CNN code rather uses the idx3-ubyte format.
I want to know how to convert from my imageSet data to idx3-ubyte format.
I came across the mnisten command below, but I don't have any Idea how to use it.
Please help.
%Here is my imageSet code that I want to convert to idx3-ubyte format.
%% Load image dataset
imgFolder1 = fullfile('C:\Users\Jay\Desktop\practical-cnn-2015a\NairaNotes');
trainingSet = imageSet(imgFolder1, 'recursive');
%%
for digit = 1:numel(trainingSet)
numImages = trainingSet(digit).Count;
for i = 1:numImages
img = read(trainingSet(digit), i);
im = rgb2gray(im2single(read(trainingSet(digit), i)));
end
end
%% here is the mnisten command I got, but I don't have an idea how to use it
mnisten -d my_image_files_directory_name -o my_prefix -s 32x32
Since I assume you're on Windows, check out this guide.

ruby base64 decode function limited 4KB in Windows

I want to write png file from A xml including encoded png image by base64. First I tried like this
if (captureImageB64.length > 1)
pngFile = File.new("xxx.png", "wb")
captureImageB64.unpack('m').first
pngFile.close
end
but this code makes 4KB png image. captureImageB64.unpack('m').first.length is 4096. so.. next version is
if (captureImageB64.length > 1)
pngFile = File.new("xxx.png", "wb")
i = 0
while i < captureImageB64.length
pngFile.write(captureImageB64.slice(i, 12).unpack('m').first)
i += 12
end
pngFile.close
end
it makes broken png file. until 4096 is fine. How can I write the right file ?
I'm working on Windows7, ruby 2.0(x86).
Update:
I found padding string(==) in image file encoded base64. So it was encoded each 4096 byte! No one told me about that... I'll make a new encoder component.

Resources