How to read a int16 PNG image saved using Labview in Matlab?
15 views (last 30 days)
Show older comments
Hello everyone,
I have been given a grayscale int16 image that was saved as a png file using Labview. Since I'm not familiar with Labview, I wanted to read and process the image using Matlab. Do I have to do a big/little endian conversion to the grayscale image? I tried using fread but this does not produce the correct image.
Any help would be greatly appreciated. Thanks!
1 Comment
Phillip
on 11 Apr 2013
There are several posts on the NI LabVIEW forums that discuss the issues of interpreting PNG files in LabVIEW and Matlab.
This thread may be of interest to you...
Answers (2)
Image Analyst
on 7 Apr 2013
I don't know why you tried fread(). Did you try using imread(), like I would have tried first:
rgbImage = imread(fullFileName);
imshow(rgbImage);
2 Comments
Image Analyst
on 7 Apr 2013
Run this code. It reads in an 8 bit image, converts it to a 16 bit image with values in the 16 bit range, then saves it and recalls it and compared what was saved with the original image before saving. I'm showing no difference. Do you show a difference?
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 15;
% Read in a standard MATLAB demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'coins.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage16bit = uint16(imread(fullFileName));
% Multiple it by 100 to go from range of 0-255 to 0-25,500
% so we have actual 16 bit numbers beyond the uint8 range.
grayImage16bit = 100 * grayImage16bit;
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage16bit, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(grayImage16bit);
subplot(2, 2, 2);
bar(grayLevels, pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Save it out as a PNG image
fullFileName = fullfile(pwd, 'gray image 16 bit.png');
imwrite(grayImage16bit, fullFileName);
% Recall it
grayImage16bitRecalled = uint16(imread(fullFileName));
% Subtract them to see the difference
diffImage = double(grayImage16bitRecalled) - double(grayImage16bitRecalled);
% Find the max
maxDifference = max(diffImage(:))
% Delete the file
delete(fullFileName);
If it runs fine for you then you'll have to show me your code. I won't be able to debug further unless you share it. Also upload your image.
xiaowei wang
on 21 Nov 2019
The data is scaled by a certain factor when Labview saves the data as explained in the link ("http://www.libpng.org/pub/png/spec/1.2/PNG-Encoders.html"). The scaling is done by simply left shiftting the original data by some certain number of bits, which is actually recorded in the sBIT chunk of the PNG file. So we can shift the data back. Assume the sBIT data is stored in the 42nd byte (you can easily find it with a binary file viewer), try the following code:
Data = imread(FileName);
fid1 = fopen(FileName);
fseek(fid1,41,'bof');
offset = 16-fread(fid1,1);
Data = bitshift(Data,-offset);
0 Comments
See Also
Categories
Find more on Data Import and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!