Overlay gradual circular blur/fade over image
6 views (last 30 days)
Show older comments
After spending two hours going through various threads on here I couldn't find what I'm looking for, so decided to ask the question directly. If there is a thread or if there is even a name for what I want to do, a handy function, tutorials... please let me know, I don't think I have the right keywords to search with yet. Certainly this must be a common question?
My goal: Gradually and circularly fading a rectangular image into white (or any color/graytone), as below. Essentially I want to apply a mask that leaves the center of the image intact but gradually fades into a color in the periphery, given a certain radius and steepness of the evelope.
Any help would be much appreciated!
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/342499/image.png)
0 Comments
Accepted Answer
Bjorn Gustavsson
on 6 Aug 2020
Edited: Bjorn Gustavsson
on 6 Aug 2020
That seems like a reasonably simple task to do, if I've understood what you want to get done that is...
Maybe something like this would solve your problem:
Im = imread('your_image.png'); % I'll assume that it is a gray-scale image between zero and one
fade_img = 0.8*ones(size(Im));
[x1,x2] = meshgrid(1:size(Im,2),1:size(Im,1));
x1_0 = size(Im,2)/2;
x2_0 = size(Im,1)/2;
dx1 = size(Im,2)/4;
dx2 = size(Im,1)/4;
weightsROI = exp(-((x1-x1_0)/dx1).^4-((x2-x2_0)/dx2).^4);
ImROI = Im.*weightsROI + fade_img.*(1-weightsROI);
subplot(1,3,1)
imagesc(Im)
subplot(1,3,3)
imagesc(weightsROI)
subplot(1,3,2)
imagesc(ImROI)
To me it seems that a simple weighted average of your initial image and a flat image at your selected gray-level with a spatially varying weighting between them should do something like what you're asking for?
You can obviously modify the weighting-factors to change the shape of the "window" and the sharpness of the transition to suit your case, and extend it to handle RGB-images too (even possible to have different windows in different colours, and possibly even do that kind of windowing in HSV-space or some other colour-space...).
HTH
7 Comments
Bjorn Gustavsson
on 7 Aug 2020
OK.
% the next 6 lines are very mysterious to me
x1_0 = size(im,2)/2; % Centre of image-region-of-interest in horizontal direction
x2_0 = size(im,1)/2; % Centre of image-ROI in vertical direction
dx1 = size(im,2)/4; % horizontal full-width to 1/e of the window/filter
dx2 = size(im,1)/4; % vertical full-width to 1/e
% how can I make the transition more gradual?
% ANS: You can make any type of weighting filter using all of the functions
% available in matlab, your imagination is the limit. Make something that has a
% more gradual transition.
% how to make the window more circular?
% ANS: replace the power of 4 to power of 2. That would give you a normal
% 2-D Gaussian window.
% does this function have a name?
% AND: Lets make one up: power-4 superelliptical Gaussian
weightsROI = exp(-((x1-x1_0)/dx1).^4-((x2-x2_0)/dx2).^4);
weightsROI = double(weightsROI);
Also when trying code from others test what happens when you modify different parameters - run the code line-by-line and inspect/plot the variable, change them a bit and see how that modifies the results.
HTH
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!