# Apply Gaussian blur to &#39;DrawOval&#39;?

**URL:** <https://psychtoolbox.discourse.group/t/apply-gaussian-blur-to-39-drawoval-39/20>\
**Category:** Uncategorized\
**Created:** [September 7, 2019, 8:58pm UTC](https://psychtoolbox.discourse.group/t/apply-gaussian-blur-to-39-drawoval-39/20 "2019-09-07T20:58:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![elric.elias](https://avatars.discourse-cdn.com/v4/letter/e/7bcc69/32.png) [@elric.elias](https://psychtoolbox.discourse.group/u/elric.elias)\
**Post date:** [September 7, 2019, 8:58pm UTC](https://psychtoolbox.discourse.group/t/apply-gaussian-blur-to-39-drawoval-39/20/1 "2019-09-07T20:58:44Z")

</div>

Hi, I am drawing ellipses to a display monitor. Currently I am using Screen('DrawOval'...) to accomplish this.

That part works fine, but I'd like to apply a Gaussian blur to the ellipses, so that they are displayed with soft, rather than hard, edges. &nbsp;Code is below, and can be run to display a hard-edged oval. All I need is to soften the dang edges, ideally with a Gaussian blur!

Any ideas? Thank you!

%% Prep Screen

gray = [171, 171, 171];

screens=Screen('Screens');

whichScreen=max(Screen('Screens'));

Screen('Preference', 'SkipSyncTests', 0);

[theWindow,theRect]=Screen('OpenWindow',whichScreen, gray, [0 0 800 600]);

&nbsp;

%% Screen Locations

wid = 120;

heit = 120;

halfwid = .5\*wid;

halfheit = .5\*heit;

[XC, YC] = RectCenter(theRect);

Loc1 = [XC-halfwid, YC - halfheit, XC + halfwid, YC + halfheit];

&nbsp;

%% Display Shape

Screen('FrameOval', theWindow, [130 130 130], [Loc1], 10);&nbsp;% This is the shape I want to have soft edges

Screen('Flip', theWindow);

&nbsp;

&nbsp;

WaitSecs(3);

close 'all';

sca

---

<div class="post-metadata">

**Author:** ![elric.elias](https://avatars.discourse-cdn.com/v4/letter/e/7bcc69/32.png) [@elric.elias](https://psychtoolbox.discourse.group/u/elric.elias)\
**Post date:** [September 7, 2019, 9:07pm UTC](https://psychtoolbox.discourse.group/t/apply-gaussian-blur-to-39-drawoval-39/20/2 "2019-09-07T21:07:32Z")

</div>

I'm using Screen('DrawOval'...) to show ellipses on a display monitor.
  

But, I'd like to apply a Gaussian blur to the edges of the ellipse, so that the edges are soft, rather than hard. I have posted code below that give the idea for how I'm drawing ellipses to the display monitor. All I need now is to soften the dang edges of the ellipse, ideally with a Gaussian blur!

  

Any ideas? Thank you! Also note that I'm open to something other than Screen('DrawOval'..), if that would work better.

  

  

%% Prep Screen

gray = [171, 171, 171];

screens=Screen('Screens');

whichScreen=max(Screen('Screens'));

Screen('Preference', 'SkipSyncTests', 0);

[theWindow,theRect]=Screen('OpenWindow',whichScreen, gray, [0 0 800 600]);

%% Screen Locations

wid = 120;

heit = 120;

halfwid = .5\*wid;

halfheit = .5\*heit;

[XC, YC] = RectCenter(theRect);

Loc1 = [XC-halfwid, YC - halfheit, XC + halfwid, YC + halfheit];

%% Display Shape

Screen('FrameOval', theWindow, [130 130 130], [Loc1], 10); % This is the ellipse I want to give soft edges to

Screen('Flip', theWindow);

WaitSecs(3);

close 'all';

sca

---

<div class="post-metadata">

**Author:** ![xrli2002](https://avatars.discourse-cdn.com/v4/letter/x/48db29/32.png) [@xrli2002](https://psychtoolbox.discourse.group/u/xrli2002)\
**Post date:** [September 8, 2019, 11:27pm UTC](https://psychtoolbox.discourse.group/t/apply-gaussian-blur-to-39-drawoval-39/20/3 "2019-09-08T23:27:11Z")

</div>

> > This is not exactly Gaussian blur (too narrow), but some kind of smoothing edge. You can play the numbers to create mask to make the edge you like. -Xiangrui
> > 
> >   
> > 
> > outerRad = wid/2-10;
> > 
> > innerRad = outerRad - 20;
> > 
> > [x, y] = ndgrid((1:wid)-halfwid, (1:heit)-halfheit);
> > 
> > r = sqrt(x.^2 + y.^2);
> > 
> > mask = exp(-((r-(outerRad+innerRad)/2)/(outerRad-innerRad)\*2).^2); % 0 to 1
> > 
> > img&nbsp;= mask \* (130-gray(1)) + gray(1); % take care of range and background
> > 
> > tex = Screen('MakeTexture', theWindow, img);
> > 
> > Screen('DrawTexture', theWindow, tex);

---

<div class="post-metadata">

**Author:** ![ianzolina](https://avatars.discourse-cdn.com/v4/letter/i/da6949/32.png) [@ianzolina](https://psychtoolbox.discourse.group/u/ianzolina)\
**Post date:** [September 9, 2019, 12:53am UTC](https://psychtoolbox.discourse.group/t/apply-gaussian-blur-to-39-drawoval-39/20/4 "2019-09-09T00:53:25Z")

</div>

I contributed procedural smoothed discs to PTB recently, check out&nbsp;ProceduralSmoothedDiscsDemo.m and&nbsp;&nbsp;ProceduralSmoothedDiscMaskDemo.m -- they utilise smoothing generated on the GPU, and alpha channel so they blend to the background. The mask demo demonstrates you can "invert" them to create circular masks.

---

<div class="post-metadata">

**Author:** ![elric.elias](https://avatars.discourse-cdn.com/v4/letter/e/7bcc69/32.png) [@elric.elias](https://psychtoolbox.discourse.group/u/elric.elias)\
**Post date:** [September 9, 2019, 7:17pm UTC](https://psychtoolbox.discourse.group/t/apply-gaussian-blur-to-39-drawoval-39/20/5 "2019-09-09T19:17:06Z")

</div>

You said: "If you need full control over filter  
applied you can draw your oval into a offscreen window, then use our  
image processing functions to apply a convolution kernel of your  
choice, e.g., gaussian to that offscreen window before drawing it to  
the actual onscreen window." Can you tell me how to do this? Speak to me like I was a child :)

---

<div class="post-metadata">

**Author:** ![ianzolina](https://avatars.discourse-cdn.com/v4/letter/i/da6949/32.png) [@ianzolina](https://psychtoolbox.discourse.group/u/ianzolina)\
**Post date:** [September 11, 2019, 5:39am UTC](https://psychtoolbox.discourse.group/t/apply-gaussian-blur-to-39-drawoval-39/20/6 "2019-09-11T05:39:03Z")

</div>

Minified demo:

  

PsychDefaultSetup(2);  
screenid = max(Screen('Screens'));  
PsychImaging('PrepareConfiguration');  
[win, winRect] = PsychImaging('OpenWindow', screenid, 0.5);  
Screen('BlendFunction', win, 'GL\_SRC\_ALPHA', 'GL\_ONE\_MINUS\_SRC\_ALPHA');  
vsize = 500;  
radius = vsize / 2;  
sigma = 85;  
colour = [0.3 0 0 1];  
disctexture = CreateProceduralSmoothedDisc(win, vsize,...  
&nbsp;&nbsp;&nbsp; vsize, [], radius, sigma, true, 1);  
while ~KbCheck  
&nbsp;&nbsp;&nbsp; Screen('DrawTextures', win, disctexture, [], [], [], [], [], colour);  
&nbsp;&nbsp;&nbsp; Screen('Flip', win);  
end  
sca;

---

<div class="post-metadata">

**Author:** ![elric.elias](https://avatars.discourse-cdn.com/v4/letter/e/7bcc69/32.png) [@elric.elias](https://psychtoolbox.discourse.group/u/elric.elias)\
**Post date:** [September 12, 2019, 5:19pm UTC](https://psychtoolbox.discourse.group/t/apply-gaussian-blur-to-39-drawoval-39/20/7 "2019-09-12T17:19:51Z")

</div>

You're the best. Thank you.
