Skip to content

Commit

Permalink
Added bug fix for Optronis
Browse files Browse the repository at this point in the history
 cameras that skip the first frame. What a pain in the ***...
  • Loading branch information
Shrediquette authored Oct 25, 2024
1 parent b4707bc commit b49e861
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 21 deletions.
73 changes: 54 additions & 19 deletions PIVlab_capture_resources/PIVlab_capture_OPTRONIS_save.m
Original file line number Diff line number Diff line change
@@ -1,31 +1,66 @@
function [OutputError] = PIVlab_capture_OPTRONIS_save(OPTRONIS_vid,nr_of_images,ImagePath,frame_nr_display,bitmode)
warning('off','imaq:gentl:hardwareTriggerTriggerModeOff')
fix_Optronis_skipped_frame=0;
if bitmode==8
bitmultiplicator=1;
bitmultiplicator=1;
elseif bitmode==10
bitmultiplicator = 32; %bring 10bit data to 16 bits full histogram, otherwise images outside Matlab are not displayed correctly (too dark).
disp(mfilename)
disp('needs testing')
bitmultiplicator = 32; %bring 10bit data to 16 bits full histogram, otherwise images outside Matlab are not displayed correctly (too dark).
disp(mfilename)
disp('needs testing')
end
OPTRONIS_settings = get(OPTRONIS_vid);
OPTRONIS_settings.Source.EnableFan = 'On';
hgui=getappdata(0,'hgui');
OutputError=0;
OPTRONIS_frames_to_capture = nr_of_images*2+fix_Optronis_skipped_frame;
if getappdata(hgui,'cancel_capture') ~=1 %capture was not cancelled --> save images from RAM to disk
OPTRONIS_data = getdata(OPTRONIS_vid,OPTRONIS_frames_to_capture);
cntr=0;
starttime=tic;
for image_save_number=(1+fix_Optronis_skipped_frame) : 2 : size(OPTRONIS_data,4)
if getappdata(hgui,'cancel_capture') ~=1
imgA_path=fullfile(ImagePath,['PIVlab_' sprintf('%4.4d',cntr) '_A.tif']);
imgB_path=fullfile(ImagePath,['PIVlab_' sprintf('%4.4d',cntr) '_B.tif']);
imwrite(OPTRONIS_data(:,:,:,image_save_number)*bitmultiplicator,imgA_path,'compression','none'); %tif file saving seems to be the fastest method for saving data...
imwrite(OPTRONIS_data(:,:,:,image_save_number+1)*bitmultiplicator,imgB_path,'compression','none');
cntr=cntr+1;
set(frame_nr_display,'String',['Saving images to disk: Image pair ' num2str(cntr) ' of ' num2str(size(OPTRONIS_data,4)/2)]);
drawnow limitrate;
end
end
disp([num2str(toc(starttime)/cntr *1000) ' ms/image'])
OPTRONIS_data = getdata(OPTRONIS_vid,OPTRONIS_frames_to_capture+2);
%% Detect if first frame is empty
% There are a number of bugs with the OPTRONIS cameras and Matlabs IMAQ
% toolbox. I had discussion with both parties, noone feels responsible.
% E.g. the Optronis returns color formats that Matlab does not understand,
% and Matlab cannot configure the Optronis to run on external trigger. This
% is EXTREMELY annoying to me, as I can't do anything.
%These lines of code check if the first frame is substantially darker than
%the following frames. If it is, then it is likely, that the camera didn't
%start properly, then we have to remove the first frame.
bug_fix_skipped_frame=0;
max_imgs=50;
if size(OPTRONIS_data,4) >= 10
if size(OPTRONIS_data,4) < max_imgs
max_imgs=size(OPTRONIS_data,4);
end
sum_img=zeros(1,max_imgs);
for i=1:max_imgs
sum_img(i)=sum(OPTRONIS_data(:,:,:,i),'all');
end
mean_img=mean((sum_img(:,2:end)));
stdev_img=5*std(sum_img(:,2:end)); %5x stdev is allowed
if (sum_img(1) < (mean_img-stdev_img))
%disp('bild skipped')
%disp(['value: ' num2str(sum_img(1))])
%disp(['lower bound: ' num2str(mean_img-stdev_img) '| upper bound: ' num2str(mean_img+stdev_img)])
disp('First frame removed. Bug fix for OPTRONIS and Mathworks (not for PIVlab...!)')
bug_fix_skipped_frame=1;
else
%disp('OK')
end
else
disp('Automatic bug fix for skipped frames could not be run, needs to have at least 5 image pairs')
end
%%
cntr=0;
starttime=tic;
for image_save_number=bug_fix_skipped_frame + (1+fix_Optronis_skipped_frame) : 2 : OPTRONIS_frames_to_capture
if getappdata(hgui,'cancel_capture') ~=1
imgA_path=fullfile(ImagePath,['PIVlab_' sprintf('%4.4d',cntr) '_A.tif']);
imgB_path=fullfile(ImagePath,['PIVlab_' sprintf('%4.4d',cntr) '_B.tif']);
imwrite(OPTRONIS_data(:,:,:,image_save_number)*bitmultiplicator,imgA_path,'compression','none'); %tif file saving seems to be the fastest method for saving data...
imwrite(OPTRONIS_data(:,:,:,image_save_number+1)*bitmultiplicator,imgB_path,'compression','none');
cntr=cntr+1;
set(frame_nr_display,'String',['Saving images to disk: Image pair ' num2str(cntr) ' of ' num2str(OPTRONIS_frames_to_capture/2)]);
drawnow limitrate;
end
end
disp([num2str(toc(starttime)/cntr *1000) ' ms/image'])
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

%% capture data

while OPTRONIS_vid.FramesAcquired < (OPTRONIS_frames_to_capture) && getappdata(hgui,'cancel_capture') ~=1
while OPTRONIS_vid.FramesAcquired < (OPTRONIS_frames_to_capture+2) && getappdata(hgui,'cancel_capture') ~=1
ima = image_handle_OPTRONIS.CData;
if ~isinf(OPTRONIS_frames_to_capture)
set(frame_nr_display,'String',['Image nr.: ' int2str(round(OPTRONIS_vid.FramesAcquired/2))]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@

%% start acqusition (waiting for trigger)
OPTRONIS_frames_to_capture = nr_of_images*2+fix_Optronis_skipped_frame;
OPTRONIS_vid.FramesPerTrigger = OPTRONIS_frames_to_capture;
OPTRONIS_vid.FramesPerTrigger = OPTRONIS_frames_to_capture+2;
if ~isinf(nr_of_images) %only start capturing if save box is ticked.
flushdata(OPTRONIS_vid);
OPTRONIS_vid.ErrorFcn = @CustomIMAQErrorFcn;
Expand Down

0 comments on commit b49e861

Please sign in to comment.