Image Blur using mean of nearby pixels

3 次查看(过去 30 天)
I have a problem function that Blurs image with the following output
output = blur(img,w);
where w is a parameter used for averaging. The output pixel value is the mean of the pixels in a square submatrix of size 2w+1 where the given pixel sits in the center.
After looking on the internet I tried this
im1 = imread(img);
kernel = ones(2*n+1)/(2*n+1)^2;
output = imfilter(im1,kernel);
but it gives errors
Error using imread>parse_inputs (line 502)
The file name or URL argument must be a character vector or string scalar.
Error in imread (line 342)
[source, fmt_s, extraArgs, was_cached_fmt_used] = parse_inputs(cached_fmt, varargin{:});
Error in blur (line 2)
rgbImage = imread(Y); % Sample image.
Honestly I don't even understand what it is saying.

回答(1 个)

Shunichi Kusano
Shunichi Kusano 2019-2-8
The error comes from "imread". The function expects "The file name or URL argument" as input, but "img" seems not so. "img" can be used directly in imfilter in this case. please try:
output = imfilter(img,kernel);
hope his helps.
  2 个评论
Perturabo
Perturabo 2019-2-8
If I use
output = imfilter(img,kernel);
then what would be the purpose of
im1 = imread(img);
besides using this produced an error
Undefined function 'imfilter' for input arguments of type 'uint8'.
Error in blur (line 3)
output = imfilter(img,kernel);
Shunichi Kusano
Shunichi Kusano 2019-2-12
I misunderstood what you want to do.
The function you made works. Note that "img" must be the filename of your image.
function output = blur(img, w);
im1 = imread(img);
kernel = ones(2*w+1)/(2*w+1)^2;
output = imfilter(im1,kernel);

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

产品


版本

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by