Skip to content

Instantly share code, notes, and snippets.

@awade
Created September 22, 2017 03:05
Show Gist options
  • Save awade/1d6b0e498526fdb867292653eee294b5 to your computer and use it in GitHub Desktop.
Save awade/1d6b0e498526fdb867292653eee294b5 to your computer and use it in GitHub Desktop.
This is a simple matlab script for converting WiDy .ptw raw video files to batches of .tiff files. Code is drawn from their manual, have added a bit to write out to tiff.
% Converts PTW raw video files (from WiDy software) to batches of tiff
% files sequentially numbered.
% Andrew Wade
% awade@ligo.caltech.edu
% September 2017
% Adapted from example code given on p10 of WiDy SWIR User Manual
clear all
close all
clc
% Open a Dialogbox to get the database file .ptw
[filename pathname] = uigetfile('*.ptw*', 'Choose a picture ptw file');
% Initialization of the file pointer.
fid = fopen([pathname filename],'r');
% File Header length in Bytes.
LgthFileMainHeader = 3476;
% Image Header length in Bytes.
LgthImHeader = 1016;
% Recover the number of pixels in images:
fseek(fid, 23, 'bof');
NbPixelImage = fread(fid,1,'uint32');
% Recover the total number of images:
fseek(fid, 27, 'bof');
Nbimage = fread(fid,1,'uint32');
% Recover width of images:
fseek(fid, 377, 'bof');
NbColImage = fread(fid,1,'uint16');
% Recover height of images:
fseek(fid, 379, 'bof');
NbRowImage = fread(fid,1,'uint16');
% Set the pointer at the beginning of the image header.
fseek(fid, LgthFileMainHeader, 'bof');
% Initialization of a viedo buffer.
A = zeros(NbRowImage,NbColImage,Nbimage,'uint16');
h = waitbar(0,[filename ' database importation : ' num2str(0) '/' num2str(Nbimage) ]);
% Main Loop
for i=1:Nbimage
waitbar(i/Nbimage,h,[filename ' database importation : ' num2str(i) '/' num2str(Nbimage) ]); % The file pointer fid is incremented by LgthImHeader.
fread(fid,LgthImHeader); % The image is extracted from the file.
B = fread( fid , [NbColImage,NbRowImage] , 'uint16' ); % The image is stored in raw order in the binary file.
A(:,:,i) = uint16( B' ); % The image is stored in the buffer.
end
close(h)
fclose('all');
% Now the write out routieen
h2 = waitbar(0,[filename ' export to tiff : ' num2str(0) '/' num2str(Nbimage) ]);
for ii=1:Nbimage
waitbar(ii/Nbimage,h2,[filename ' database importation : ' num2str(i) '/' num2str(Nbimage) ]); % The file pointer fid is incremented by LgthImHeader.
writeoutBuff = A(:,:,ii);
imwrite(writeoutBuff,[pathname,filename(1:end-7),'_',num2str(ii),'.tiff'])
end
close(h2)
fclose('all');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment