Is there an easy way to apply a 2d gaussian filter in a circular image area (by easy i mean a ready matlab function) or does one have to implement this on his own ??

有帮助吗?

解决方案

If you want to apply any filter to a selected portion of the image, one option is to use a binary mask.

Let img be your image, set the position and the radius of the circular mask as well as the dimension of the filter:

centre=[50 50];
radius=20;
n=5;

Then create the mask:

Mask=zeros(size(img));
Disk = fspecial('disk',radius)==0;
Mask(centre(1)-radius:centre(1)-radius+size(Disk,1)-1, centre(2)-radius:centre(2)-radius+size(Disk,2)-1)=double(~Disk);

Apply filtering as suggested by @Gacek:

h = fspecial('gaussian', n);
Filtered=filter2(h, img);

Combine the filtered area with the original image and show the result:

Result=img.*uint8(~Mask)+uint8(Filtered.*Mask);
imshow(Result)

Example result:

filtered lion

Notes: 1. change the class uint8 to the appropriate class of your original image. 2. The example image is in the public domain, source: en.wikipedia.org/wiki/File:Phase_correlation.png.

其他提示

I would use fspecial. Given img is your image, then:

h = fspecial('gaussian', n);
filter2(h, img);

where n is the size of your filter mask. So it will create nxn Gaussian filter mask.

The documentation also says you can set the standard deviation for your Gaussian mask with another parameter:

h = fspecial('gaussian', n, std_sigma);

Use fspecial with imfilter -

h = fspecial('gaussian', hsize, sigma);
filteredIMG = imfilter(originalIMG, h);

Example

originalIMG = imread('cameraman.tif');
h = fspecial('gaussian', 21, 0.9);
filteredIMG = imfilter(originalIMG, h);

figure,
subplot(211),imshow(originalIMG)
subplot(212),imshow(filteredIMG)

enter image description here

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top