如何在具有透明背景的 Psychtoolbox 中显示 PNG 文件?
How do I make a PNG file display in Psychtoolbox with a transparent background?
我有两张图片需要同时显示在屏幕上,使用 Screen('DrawTexture')
函数。一个图像是场景图像,第二个是对象,其中背景是透明的。我想在场景图像的顶部显示对象。但是,当我尝试这样做时,对象似乎有黑色背景。
实物图绝对没有问题;当使用 [object,map,alpha] = imread(objectimage.png)
调用时,alpha 值 returns 是一个适当大小的矩阵。我还使用 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');
如有任何建议,我们将不胜感激!
我认为您需要做的就是在 MakeTexture
期间在图像中包含 alpha 通道。
% 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;
我有两张图片需要同时显示在屏幕上,使用 Screen('DrawTexture')
函数。一个图像是场景图像,第二个是对象,其中背景是透明的。我想在场景图像的顶部显示对象。但是,当我尝试这样做时,对象似乎有黑色背景。
实物图绝对没有问题;当使用 [object,map,alpha] = imread(objectimage.png)
调用时,alpha 值 returns 是一个适当大小的矩阵。我还使用 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');
如有任何建议,我们将不胜感激!
我认为您需要做的就是在 MakeTexture
期间在图像中包含 alpha 通道。
% 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;