0

我需要使用该Screen('DrawTexture')功能在屏幕上同时显示两个图像。一个图像是场景图像,第二个是对象,其中背景是透明的。我想在场景图像的顶部显示对象。但是,当我尝试此操作时,该对象似乎有黑色背景。

对象图像绝对没有问题;当用 调用时[object,map,alpha] = imread(objectimage.png),alpha 值返回一个适当大小的矩阵。我还使用 Python 成功地显示了这些图像,一个在另一个之上。但是,由于各种研究原因,这个项目不能用 Python 编写。

我尝试寻找解决方案,但我能找到的唯一解决方案与图形或绘图有关,而不是Screen. 我怀疑我需要对 alpha 混合做一些事情(可能是一些非常基本的事情),但我找不到任何适合初学者的指南。

我的测试代码目前如下所示:

% screen setup
PsychDefaultSetup(2); Screen('Preference', 'SkipSyncTests', 1);
screenNum = max(Screen('Screens')); % set screen 
Screen('Preference','VisualDebugLevel',3);
[w,rect] = Screen('OpenWindow',screenNum);
% Activate for alpha
Screen('BlendFunction', w, 'GL_SRC_ALPHA', 'GL_ONE_MINUS_SRC_ALPHA');

% image presentation rectangles
bigImSq = [0 0 500 500];
[bigIm, xOffsetsigB, yOffsetsigB] = CenterRect(bigImSq, rect);
smImSq = [0 0 250 250];
[smallIm, xOffsetsigS, yOffsetsigS] = CenterRect(smImSq, rect);

% IMAGES
sceneIm = 'scene.png'; 
objIm = 'object.png';
sceneLoad = imread(sceneIm); 
[objLoad,map,alpha] = imread(objIm);

% final textures for display
scene = Screen('MakeTexture',w,sceneLoad); 
object = Screen('MakeTexture',w,objLoad); 

% Image presentation
grey = [100 100 100];

Screen('FillRect',w,grey); 
Screen('Flip',w); 
WaitSecs(0.5);

Screen('FillRect',w,grey);
Screen('DrawTexture', w, scene,[],bigIm); % draw the scene 
Screen('DrawTexture', w, object,[],smallIm); % draw the object 
Screen('Flip',w); 
WaitSecs(3);

Screen('CloseAll');

任何意见,将不胜感激!

4

1 回答 1

3

我认为您需要做的就是在MakeTexture.

% slightly modified boilerplate -- non-fullscreen by default,
% and set the background color (no need for all the FillRects)
PsychDefaultSetup(2); 
Screen('Preference', 'SkipSyncTests', 1);
screenNum = max(Screen('Screens')); % set screen 
Screen('Preference', 'VisualDebugLevel', 3);
[w, rect] = Screen('OpenWindow', screenNum, [100 100 100], [0 0 400 400]);

Screen('BlendFunction', w, 'GL_SRC_ALPHA', 'GL_ONE_MINUS_SRC_ALPHA');

% image presentation rectangles
smImSq = [0 0 250 250];
[smallIm, xOffsetsigS, yOffsetsigS] = CenterRect(smImSq, rect);

这是具有透明背景的图像(用于重现性)。

% from http://pngimg.com/upload/cat_PNG100.png
[img, ~, alpha] = imread('cat.png');
size(img)
%% 2557 x 1993 x 3 (rgb)

我们将制作一个没有 Alpha 通道的纹理,以及一个带有 Alpha 通道的纹理。

texture1 = Screen('MakeTexture', w, img);
img(:, :, 4) = alpha;
texture2 = Screen('MakeTexture', w, img);

首先,没有 alpha 通道的图像。

Screen('DrawTexture', w, texture1, [], smallIm);
Screen('Flip', w);

然后,RGBA 纹理。

Screen('DrawTexture', w, texture2, [], smallIm);
Screen('Flip', w);
sca;
于 2016-11-02T14:57:33.030 回答