# Simultaneous movie and image presentation

**URL:** <https://psychtoolbox.discourse.group/t/simultaneous-movie-and-image-presentation/4313>\
**Category:** Programming Help\
**Tags:** windows\
**Created:** [March 17, 2022, 8:43am UTC](https://psychtoolbox.discourse.group/t/simultaneous-movie-and-image-presentation/4313 "2022-03-17T08:43:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxed](https://avatars.discourse-cdn.com/v4/letter/m/a587f6/32.png) [@maxed](https://psychtoolbox.discourse.group/u/maxed)\
**Post date:** [March 17, 2022, 8:43am UTC](https://psychtoolbox.discourse.group/t/simultaneous-movie-and-image-presentation/4313/1 "2022-03-17T08:43:43Z")

</div>

Hi all,

I have a question regarding presentation of stimuli but I couldn’t find an answer online or in the Psychtoolbox Demos.

I am using Matlab R2021a and PTB 3.0.17 on a Windows 10 operating system.

What I want to achieve is to present a video that runs for a specific time and simultaneously flash additional static pictures but only for shorter durations. So for example I show a video for 30 seconds and additionally in the periods from 3-5sec., 8-11sec., 15-17sec. etc. I want to present images in addition to the actual movie.

You can also think about it as having two layers of display. On the lower layer (=background) the video runs while on the higher layer the images are shown in addition to the video.

I can show images and show a movie using Screen(DrawTexture) but I can’t show them at the same time. The code I have so far is the following:

```matlab
 while ~KbCheck
        % Wait for next movie frame, retrieve texture handle to it
        tex = Screen('GetMovieImage', win, movie);
        
        % Valid texture returned? A negative value means end of movie reached:
        if tex<=0
            % We're done, break out of loop:
            break;
        end
        
        % Draw the new texture immediately to screen:
        Screen('DrawTexture', win, tex);
        
       % this would be a static image for example
        curr.image = imread('myimage.bmp');
        curr.texture = Screen('MakeTexture', win, curr.image);  
        Screen('DrawTexture', win, curr.texture, [], []);
        
        
        curr.image = imread('pain.bmp');
        curr.texture = Screen('MakeTexture', win, curr.image);  
        Screen('DrawTexture', win, curr.texture, [], []);        
        %Screen('Flip', win);
        %WaitSecs(5);
        
        % Update display:
        Screen('Flip', win);
        
        % Release texture:
        Screen('Close', tex);
    end

```

What this code does at the moment is show the video and from the beginning also show the image overlayed. But I can’t add more pictures or create a timeline for when those pictures need to be shown.

I would very much appreciate your help!

Thanks a lot

---

<div class="post-metadata">

**Author:** ![Ian-Max-Andolina](https://yyz2.discourse-cdn.com/free1/user_avatar/psychtoolbox.discourse.group/ian-max-andolina/32/4_2.png) [@Ian-Max-Andolina](https://psychtoolbox.discourse.group/u/Ian-Max-Andolina)\
**Post date:** [March 17, 2022, 9:40am UTC](https://psychtoolbox.discourse.group/t/simultaneous-movie-and-image-presentation/4313/2 "2022-03-17T09:40:07Z")

</div>

By default, if you are using `Screen('GetMovieImage')` then this will “block” waiting for the next frame, and your timing will be dictated by the frame rate of the movie. You could make your `Flip` times a multiple of the movie frame rate. But another solution requires not using blocking mode and using a second buffer texture. If `Screen('GetMovieImage')` returns a new frame, show that, if not, show the previous frame.

My [movieStimulus class uses this technique](https://github.com/iandol/opticka/blob/master/stimuli/movieStimulus.m#L243) (this is an OOP class and so `me` refers to itself):

```matlab
me.blocking = false;
me.texture = Screen('GetMovieImage', me.sM.win, me.movie, me.blocking);
if me.texture > 0
	if ~isempty(me.buffertex) && ...
			me.buffertex > 0 && ...
			me.buffertex ~= me.texture && Screen(me.buffertex,'WindowKind') == -1
		try Screen('Close', me.buffertex); end
		me.buffertex=[]; 
	end
	Screen('DrawTexture', me.sM.win, me.texture, [], me.mvRect,...
		angle,[],[],[],me.shader);
	me.buffertex = me.texture; %copy new texture to buffer
elseif me.buffertex > 0
	Screen('DrawTexture', me.sM.win, me.buffertex, [], me.mvRect,...
		angle,[],[],[],me.shader)
end

```

Roughly it checks if `texture` is \> 0 (a new frame): if so check if a previous `buffertex` is present and close it, then draw the new `texture` and assign it to `buffertex` | otherwise just draw `buffertex`. Now the timing is only dictated by `Screen('Flip')`, and so your other animations will work fine.You can see this in action easily with my toolbox:

```auto
m = metaStimulus;
m{1} = dotsStimulus('xPosition', 4, 'mask', true);
m{2} = movieStimulus('blocking', 0);
m.run; %test run of the stimuli

```

This creates a movie and an RDS stimulus, then runs them together at the FPS of the monitor display; the movie does not hold up the timing of the RDS…

---

<div class="post-metadata">

**Author:** ![maxed](https://avatars.discourse-cdn.com/v4/letter/m/a587f6/32.png) [@maxed](https://psychtoolbox.discourse.group/u/maxed)\
**Post date:** [March 17, 2022, 10:32am UTC](https://psychtoolbox.discourse.group/t/simultaneous-movie-and-image-presentation/4313/3 "2022-03-17T10:32:01Z")

</div>

Thank you for this very fast answer. I am not sure if I already get it but will try it out.

Another related question:

Is there any way to start a movie and let it run for a specific duration and while the movie is running present additional stimuli?

I did a similar thing using sounds which play in the background and programmed it that the sound should play until my n-th trial is finished. So it seems like the sound gets started and plays until my condition is reached. I would love to find a similar solution for my movies but I can’t.

Thanks a lot!
