ReadRawImage function listing

The following MATLAB function will read the raw images in this archive, at least on a Macintosh computer.


function x = ReadRawImage(filename, width, height)
% x = ReadRawImage(filename, width, height)
%
% Read a RAW DATA format image written by IPLAB.
% The raw image data is written as a series of signed
% short integers in row first order.
%
% 2/24/97  jdt Wrote it.
% 12/29/15 dhb Change short to ushort in the fread.
%              This seems to be needed now.
% 12/05/20 dhb Change ushort to uint16, which seems more explicit,
%              and be explicit about big endian byte order in fopen.
 
% Examples:
%{
    % Download (e.g.) the BearFruitGrayB image set and unpack.
    % Change variable theImageDir to wherever you unpacked, and
    % put this function into the same directory.  Then execute this
    % example to read a single image plane.  Data come back as unsigned
    % 16-bit integers. The image size differs for some of the other 
    % hyperspectral images.
    theImageDir = '/Users/dhb/Desktop/BearFruitGrayB';
    cd(theImageDir);
    imSize = 2020;
    imagePlane = ReadRawImage('400',imSize,imSize);
    figure; clf; imshow(imagePlane/max(imagePlane(:)));
%}

% Open file
id = fopen(filename, 'r', 'b');

% Read in the data.
x = fread(id, [width,height], 'uint16')';

% Close file
fclose(id);

 
Author: David Brainard, brainard@psych.upenn.edu
Last Modified: December 29, 2015
Modified By: David Brainard