Don't use jpeg images for precise visual stimulus storage [Was: Anti aliasing not working when drawing dots]


I am simply trying to create multiple images with different numbers of black dots on a grey background. I have the code working, yet there are some aliasing effects around the edges of the dots I just can’t get rid of. I have set the multisampling parameter to 8, but this has no observable effect. I have also selected dot types 2 and 3, neither of which help. Is this a limitation of my hardware? I should add, I am only creating these stimuli to save to file - I’m not presenting them in PTB. Perhaps this allows for an alternative resolution?

I am using Mac OS X Catalina with the inbuilt Intel Iris Plus Graphics 645 1536 MB graphics. I’m using MATLAB 2020a. Here are the relevant parts of my code:

    % Open screen with grey background
    [w, rect] = Screen('OpenWindow', screenNumber,...
        [255/2,255/2,255/2],[], [], 2,[],6);

    % Set useful parameters
    PsychVideoDelayLoop('SetAbortKeys', KbName('Escape'));
    Screen('BlendFunction', w, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    for i = 0:4

    	% Define the numeral used in the cue
    	numeral = i;
    	for j = 1:20 % 20 images of each dot number
    		% Create dot cue
    		[dots(i+1, j),centeredRect] = dotCues(numeral,rect,w);
    		% Draw dot cue
    		if numeral > 0
    			Screen('DrawDots', w, dots(i+1, j).position, dots(i+1, j).size,...
    				dots(i+1, j).colour, [], 2);
    		Screen('Flip', w);

    		% Save dot cue
    		im = Screen('GetImage', w, centeredRect);
    		imwrite(im, fullfile(imDir, sprintf("Dots%d_%d.jpg", i, j)));

Code looks ok to me as far as dot drawing is concerned, although other aspects could be improved. E.g., AssertOpenGL must be the first PTB command, it is useless after you’ve called Screen already. And you can replace both KbName(‘UnifyKeynames’) and AssertOpenGL by a PsychDefaultSetup(1); at the beginning - it inclues those commands. Also [255/2 255/2 255/2] can be written as simply 255/2 for grayscale images. And the ‘2’ for specifycing double-buffering is redundant, as that is the [] default.

Does DotDemo work? What’s its output in the Matlab window? MSAA (6) and round dot drawing are orthogonal solutions to the anti-aliasing problems. Only using one of them is worth a try.

This could be yet another macOS graphics driver bug.

Does a dot_type parameter of 3 work as a workaround?

If you appreciate this help, please consider buying priority support to support PTB’s upkeep.


[Work time spent so far: 20 Minutes]

Thank you for the tips! I’ve added those changes. Trying either mulstisampling 6 or round dot drawing separately did not solve the issue. DotDemo seems to work perfectly, and using dot_type = 3 makes no difference unfortunately.

If its only to save to file, try to make a big matrix (say 8 or 16 times larger than your target image), place dots in that (generate dots using the function Circle(), and then scale down the whole image using ShrinkMatrix.

If DotDemo works fine then it is most likely a bug in your code and you’ll have to compare what you do with what it does. DotDemo shows the standard method of using alpha-blending + dot_type smoothing. dot_type=3 manually enables a workaround for graphics drivers which don’t support smoothing but also lie about their ability to do so, iow. OS bugs.

Or follow Diederick’s workaround. Or buy priority support, so i can spend more time working with you through it.


Ah I misread the email. If DotDemo works fine, indeed see if you can follow what is shown there as closely as possible. Certainly better than all the hand-work i am suggesting.

I believe actually Ive made a mistake and what I’m seeing are not aliasing artifacts but edge artifacts. I’ve an attached an example for clarity. As you can see, there is blurring around the edge of the dots. Is there any way around this that hasn’t been mentioned already?


That looks like a jpeg artifact. Is that what you used? Try png.

1 Like

i hate myself. thank you - problem solved