Page 1 of 2

How to convert 16bit signed, little endian RAW to PNG

Posted: 2020-03-03T00:12:58-07:00
by Ash7890
I'm new to Imagemagick and straggling to convert RAW to PNG.

My client gave me a RAW data which contains multiple images. The size is 512*512, 16bit signed and little endian.
So, I cropped one of those with:

Code: Select all

convert -size 512x512 -depth 16 UYVY:original.raw result.raw
This is the corpped RAW:
https://drive.google.com/file/d/1V1c-yt ... sp=sharing

It looks like this on ImageJ (with 16bit signed, little endian option):

https://drive.google.com/file/d/1ziTATE ... i7Xft/view

I want a PNG looks same as above image. However, I have no idea the options for signed and little endian. One of many commands I tried is like:

Code: Select all

convert -size 512x512 -depth 16 -define png:swap-bytes=on -define signed uyvy:result.raw result.png
It generates weird greenish image. My client told me the RAW is 1ch(mono), so should be converted to 3ch(rgb) gray and then convert to PNG.

Re: How to convert 16bit signed, little endian RAW to PNG

Posted: 2020-03-03T09:58:20-07:00
by snibgo
Please paste a link to original.raw.

Re: How to convert 16bit signed, little endian RAW to PNG

Posted: 2020-03-03T17:57:36-07:00
by Ash7890
snibgo wrote:
2020-03-03T09:58:20-07:00
Please paste a link to original.raw.
Oops, forgot to provide it! I pasted it on the original post under "This is the corpped RAW:"

Re: How to convert 16bit signed, little endian RAW to PNG

Posted: 2020-03-03T17:59:02-07:00
by fmw42
Why do you say it is cropped? I see no cropping in your command. All it seem, to try to do is convert from raw to png.

Re: How to convert 16bit signed, little endian RAW to PNG

Posted: 2020-03-03T18:42:38-07:00
by Ash7890
fmw42 wrote:
2020-03-03T17:59:02-07:00
Why do you say it is cropped? I see no cropping in your command. All it seem, to try to do is convert from raw to png.
Probably because of my luck of knowledge of raw data. As I explained above, the original raw contains more than 1000 of 512*512 raw image data. The link to raw I provided above is one of those. I wanted to get just one of them for testing. Im calling it "cropped". Sorry for your confusion.

Re: How to convert 16bit signed, little endian RAW to PNG

Posted: 2020-03-03T19:58:56-07:00
by fmw42
No problem. Just wanted to be sure you had not attempted to process the raw data in any way that might have corrupted it.

Re: How to convert 16bit signed, little endian RAW to PNG

Posted: 2020-03-03T21:17:20-07:00
by Ash7890
I think it is not corrupted as it can be opened with ImageJ without any error and it looks correct.

Re: How to convert 16bit signed, little endian RAW to PNG

Posted: 2020-03-03T21:43:58-07:00
by snibgo
To get just one image, you read your client's file, original.raw with prefix UYVY: and saved the result as result.raw with no prefix. When I try that, I get an error: "no encode delegate for this image format `RAW'". What format do you think result.raw is?

I am confused "My client told me the RAW is 1ch(mono)". If there is only one channel, it is "gray:" not "uyvy:".
Ash7890 wrote:-define png:swap-bytes=on
I suggest "-endian" instead. See https://www.imagemagick.org/script/comm ... php#endian

Re: How to convert 16bit signed, little endian RAW to PNG

Posted: 2020-03-03T22:36:30-07:00
by Ash7890
I am confused "My client told me the RAW is 1ch(mono)". If there is only one channel, it is "gray:" not "uyvy:".
I thought the same, and tried prefix "gray", however I got an error:

Code: Select all

convert -size 512x512 -depth 16 gray:original.raw result.raw
convert: UnableToAcquireString `Cannot allocate memory' @ fatal/string.c/AcquireString/135.
Image info of the original.raw on ImageJ is below. It is saying Unit: "Gray Value".
Title: original.raw
Width: 512 pixels
Height: 512 pixels
Depth: 10 pixels
Size: 5MB
Voxel size: 1x1x1 pixel^3
ID: -4
Bits per pixel: 16 (signed)
Display range: -2048 - 1940
Image: 1/1000
No threshold
ScaleToFit: false

Calibration function: y = a+bx
a: -32768.000000
b: 1.000000
Unit: "Gray Value"
What format do you think result.raw is?
I thought it was the same as the original.raw.
result.raw on ImageJ is saying Unit: "Gray Value" too.

I also tried -endian. PNG was generated but it was the wrong color.

Code: Select all

convert -size 512x512 -depth 16 -endian MSB -define signed gray:result.raw result.png

Re: How to convert 16bit signed, little endian RAW to PNG

Posted: 2020-03-04T02:52:40-07:00
by snibgo
Ash7890 wrote: convert -size 512x512 -depth 16 gray:original.raw result.raw
convert: UnableToAcquireString `Cannot allocate memory' @ fatal/string.c/AcquireString/135.
Your original.raw is very large, isn't it? Perhaps IM tries to read it all into memory.

Your convert command will change the storage format, so I think this is a bad way of getting just the first of 1000 images. If you are sure each pixel takes 2 bytes, you want to make a new file from the first 512*512*2 bytes, 524288 bytes. The unix tool "head" can do that:

Code: Select all

head --bytes 524288 original.raw >firstimage.raw
When you have done that, please paste a link to firstimage.raw

(When you want the second or third image, you can use "head" then "tail".)

Re: How to convert 16bit signed, little endian RAW to PNG

Posted: 2020-03-04T03:48:36-07:00
by Ash7890
Yes, original.raw is large. Here is firstimage.raw generated from "head":

https://drive.google.com/file/d/1CmKPdw ... sp=sharing

Re: How to convert 16bit signed, little endian RAW to PNG

Posted: 2020-03-04T04:26:48-07:00
by magick
Try this command:

Code: Select all

convert -size 512x512 -depth 16 -endian LSB -define quantum:format=signed gray:result.raw -auto-level result.png

Re: How to convert 16bit signed, little endian RAW to PNG

Posted: 2020-03-04T06:14:04-07:00
by Ash7890
Mmm it still looks different. I tried both result.raw and firstimage.raw (which is made from "head" command). Both look the same.

https://drive.google.com/file/d/1YaGmII ... sp=sharing

If you actually tried that command and it worked, then the problem might be my environment??

Re: How to convert 16bit signed, little endian RAW to PNG

Posted: 2020-03-04T07:09:39-07:00
by snibgo
Okay, good. That file firtimage.raw is identical to the original.raw you uploaded previously. Nevertheless, I suggest you use "head" and "tail" for future image extraction.

I think "-define quantum:format=signed" is new to IM, possibly newer than your version.

To get the same result as you got from ImageJ:

Code: Select all

magick -size 512x512 -depth 16 gray:firstimage.raw -evaluate AddModulus 50% -auto-level x.png
If using Windows BAT scripts, double the % to %%.

EDIT: Corrected typo: no space in "AddModulus".

Re: How to convert 16bit signed, little endian RAW to PNG

Posted: 2020-03-04T07:48:18-07:00
by magick
The quantum:format=signed define has been around several years. The results we obtained from the proposed command matched the expected results. Not sure why its failing for you. @snibgo's command works as well except its AddModulus (no space).