Machine Vision

cancel
Showing results for 
Search instead for 
Did you mean: 

How do you load signed 16 bit png file outside of labVIEW?

Solved!
Go to solution

Hey thanks so much for your solution it works perfectly!
I've updated your I16 code (the same fix could be applied to the U16 code) to make it run a lot faster in python.
Generally iterating through all of the pixels in python is slower than doing an array wide operation (there's probably some weird python magic going on under the hood). But thanks so much!

Message 11 of 14
(1,171 Views)

Thank you very much for updated your I16 code! Looks much more elegant (I'm absolute beginner with Python). Theoretically shifting can be replaced with OpenCV divide, but then small bug in NI Vision is critical, because "wrong" bits needs to be masked first.

0 Kudos
Message 12 of 14
(1,144 Views)

Hello everyone, this thread is old, but I had the same problem. A different implementation for looking for chunks was suggested at StackOverflow:

from pathlib import Path
import cv2
import numpy as np
from matplotlib import pyplot

file_path = '120130.png' 
# Load PNG as bytes and find "sBIT" chunk
png = Path(file_path).read_bytes()
sBIToffset = png.find(b'sBIT')
if sBIToffset:
    # 4 bytes before "sBIT" tell us how many values there are - could be 1-4 values
    nValues = int.from_bytes(png[sBIToffset-4:sBIToffset], byteorder="big")
    values  = list(png[sBIToffset+4:sBIToffset+4+nValues])
if len(values)==1:
    image_src=cv2.imread(file_path, cv2.IMREAD_UNCHANGED)
    shift = 16-values[0]
    if shift<16:
        temp=np.zeros(image_src.shape,dtype=np.int16)
        image=(image_src>> shift).astype(np.int16)
        print(cv2.minMaxLoc(image))
else:
    print("Seems not to be a greyscale image!")

A question about PNG particular about scAl chunk: I can't find anything about a constant offset in the PNG specification. Is the sCAL chunk meant? But it seems to have a different meaning.

0 Kudos
Message 13 of 14
(851 Views)

@p4keal wrote:

 

A question about PNG particular about scAl chunk: I can't find anything about a constant offset in the PNG specification. Is the sCAL chunk meant? But it seems to have a different meaning.


Oh, scAl is something very special. This is private Tag, not a part of standard chunks (private chunks are allowed by standard, but the names of private chunks must have a lowercase second letter, while public chunks will always be assigned names with uppercase second letters). This Tag present only in the PNGs which holding signed representation (I16) and have negative values. If you have no negative values (even in signed PNG), then this chunk is not present. This is how NI deal with signed representation in PNG. Technically PNG can store signed values, but spec says: "Values are unsigned unless otherwise noted. Values explicitly noted as signed are represented in two's complement notation." I didn't catch what means " in two's complement notation" exactly, and NI, probably didn't as well.

 

Anyway, you can do pretty simple experiment, just create few images like this:

snippet-Signed.png

and check what inside (I would like to recommend to use 010 Hex Editor from SweetScape with PNG PlugIn, then you will see parsed chunks):

Signed.png

 

Now you can see, that in the file with positive values only this chunk is omitted, but in the images where I shifted grays to -500 and -1000 grays — present.

And the values of two first bytes:

-500: FE0C; 0xFE0C = 65036 (65036-65536 = -500)

-1000: FC18; 0xFC18 = 64536 (64536-65536=-1000)

This is how it works. Also you can see NIIMAQ string in this chunk, which tell that this is NI special thing.

Online PNG inspector can also show this tag and its appeared as unknown and private:

inspector.png

Message 14 of 14
(836 Views)