Write a function called blur that blurs the input image. The function is to be called like this:
古いコメントを表示
Write a function called blur that blurs the input image. The function is to be called like this:
output = blur(img,w);
where img, the input image is a two-dimensional matrix of grayscale pixel values between 0 and 255. Blurring is to be carried out by averaging the pixel values in the vicinity of every pixel. Specifically, 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. For example, if w is 1, then we use a 3x3 matrix, that is, we average all the neighboring pixels of the given pixel and itself. Only use valid pixels when portions of the blurring matrix fall outside the image. For example, the blurred value corresponding to w = 1 at index (1,1) would be the mean of of elements (1,1), (1, 2), (2,1) and (2, 2). Both input img and output output are of type uint8.
You can download the test image here
Opens in new tab
to use in MATLAB.
Code to call your function
img = imread('vandy.png');
output = blur(img,2);
imshow(output);
Can anyone please tell me where I am going wrong with the following code for this question? The code doesnot blur the image and just shows me the image.
function output = blur(img,w)
B=double(img);
[row col] = size(B);
k=2*w+1;
for r=2:row-k
for c=2:col-k
MeanPixelVal=0;
for r1=r:(r+k-1)
for c1=c:(c+k-1)
MeanPixelVal = MeanPixelVal + double(img(r1,c1));
end
end
MeanPixelVal = MeanPixelVal/(k*k);
blurimg(r,c) = uint8(MeanPixelVal);
end
end
subplot(1,2,1); imshow(img); title('gray image, image intensity 0 - 255');
subplot(1,2,2); imshow(blurimg); str = strcat('blur image, w = ',num2str(w)); title(str);
output = uint8(B);
end
1 件のコメント
Walter Roberson
2019 年 7 月 21 日
[Merged from https://www.mathworks.com/matlabcentral/answers/472573-write-a-function-called-blur-that-blurs-the-input-image ]
- Simple testVariable output has an incorrect value. Tested with a 5x5 uint8 matrix with all 255-s in the first and last rows and columns and zeros everywhere else and w = 1
- Assessment result: incorrectUsing image fileVariable output has an incorrect value. Tested with the vandy.png file and w = 1
回答 (2 件)
Image Analyst
2019 年 7 月 21 日
0 投票
You never assign blurimg to B, so B remains the input image that you assigned it to way up at the top.
6 件のコメント
Namarta Kapil
2019 年 7 月 21 日
Image Analyst
2019 年 7 月 21 日
Show me exeactly what you did. Attach the script.
Namarta Kapil
2019 年 7 月 21 日
Walter Roberson
2019 年 7 月 21 日
Where is B(1,1) accessed?
Namarta Kapil
2019 年 7 月 21 日
Walter Roberson
2019 年 7 月 21 日
imshow() has nothing to do with where you are access B(1,1) .
r starts from 2. r1 starts from r and so has a minimum of 2. You access img(r1,c1) so you never access img(1, anything) . Therefore you cannot possibly be bluring correctly for the first w+1 rows.
Rohit Patil
2020 年 5 月 1 日
編集済み: Walter Roberson
2020 年 5 月 7 日
function output = blur(img,w)
blurimg=img;
B=blurimg;
[row col] = size(B);
k=2*w+1;
for r=2:row-k
for c=2:col-k
MeanPixelVal=0;
for r1=r:(r+k-1)
for c1=c:(c+k-1)
MeanPixelVal = MeanPixelVal + double(img(r1,c1));
end
end
MeanPixelVal = MeanPixelVal/(k*k);
blurimg(r,c) = uint8(MeanPixelVal);
end
end
subplot(1,2,1); imshow(img); title('gray image, image intensity 0 - 255');
subplot(1,2,2); imshow(blurimg); str = strcat('blur image, w = ',num2str(w)); title(str);
output = uint8(B);
en
d
4 件のコメント
Neha Prajapati
2020 年 9 月 20 日
This code is not working
Image Analyst
2020 年 9 月 20 日
The d should be attached to the en so the last line should be "end".
end
Sana Hafeez
2021 年 6 月 4 日
Image Analyst
2021 年 6 月 4 日
Sana, attach your code, or use my attached code, or use the built-in imfilter() function.
カテゴリ
ヘルプ センター および File Exchange で Image Processing Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!