フィルターのクリア

How to implement a function to apply the 3x3 average filter to a gray-scale image.

31 ビュー (過去 30 日間)
amy elmossly
amy elmossly 2012 年 11 月 14 日
編集済み: DGM 2022 年 11 月 27 日
Here is my code
function AverageFilter(image)
I = imread(image);
[x,y] = size(I);
for i = 2:x-1
for j = 2:y-1
sum = 0;
for ii = i-1:i+1
for jj = j-1:j+1
sum = sum + I(ii,jj);
end
end
I2(i,j) = ceil(sum/9);
end
end
imshow(I2);
it keeps giving me the following error: Warning: Image is too big to fit on screen; displaying at 67% > In imuitools\private\initSize at 73 In imshow at 264 In AverageFilter at 18
and the resulting image is very dark..
I want to know what did i do wrong in my code and to fix this error?!!!
  7 件のコメント
justin gat
justin gat 2021 年 5 月 30 日
ok sir now i will tag u and will write a question
thank you i need it as soon as possible

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2012 年 11 月 14 日
編集済み: Image Analyst 2012 年 11 月 14 日
Don't worry about the warning. Your code can be done like this:
blurredImage = conv2(single(yourGrayScaleImage), ones(3)/9, 'same');
imshow(blurredImage, []); % or imshow(uint8(blurredImage), [0 255]);
  12 件のコメント
Franzi
Franzi 2020 年 6 月 7 日
Hello? Are you still there? Do you know the function reshape and how to use it?
Image Analyst
Image Analyst 2020 年 6 月 7 日
You'd have to reshape the image into a column of 3 pixels. Then use repelem() to repeat each column because the windows overlap don't they? You don't want them to jump, right. Then use a for loop to go down the columns. You'd have to think about it. It gets tricky because you'll have to have each pixel in there 9 times so it's tricky to figure out how to do that with repelem() and reshape(). It's much more difficult when they don't let you do it the simple and obvious ways. It's MUCH easier if they let you move the window in jumps of 3 pixels over and down rather than 1 pixel over and down.

サインインしてコメントする。

その他の回答 (2 件)

sateesh kumar
sateesh kumar 2020 年 4 月 16 日
Dont worry about Warning/Display Warning of too big size, you can simply write warning off at the starting of program.

Nitishselvakumar Nadar
Nitishselvakumar Nadar 2021 年 4 月 22 日
img = imread("Tulip.jpg");
[row ,col] = size(img);
for i = 2:1:row-1
for j = 2:1:col-1
x= img(i+1:i-1,j+1:j-1);
C=x(:)';
C=sort(C);
avg = sum(C)/9;
img(i,j) = avg;
end
end
imshow(img);
  4 件のコメント
mohammad sheikholmolok
mohammad sheikholmolok 2022 年 11 月 27 日
移動済み: DGM 2022 年 11 月 27 日
I write for you the correct code
DGM
DGM 2022 年 11 月 27 日
編集済み: DGM 2022 年 11 月 27 日
If you're going to offer an improved bit of code, explain why it's better.
This is still a lot of room for improvement.
% read an image
inpict = imread('cameraman.tif'); % use unambiguous variable names
% what stops this from breaking if the image is RGB?
[row,col,~] = size(inpict); % always discard the last output from size()
outpict = zeros(row,col,class(inpict)); % preallocate to appropriate class
for i = 2:1:row-1 % avoiding the edges is a simple strategy, but not very good
for j = 2:1:col-1 % a better way would be edge padding/replication
% get a sample
sampleblock = inpict(i-1:i+1,j-1:j+1);
% don't need a bunch of temporary variables or unused lines
% RHS is uint8-scale double
% LHS is uint8
% the assignment implicitly uses round() when casting
outpict(i,j) = mean(sampleblock(:));
end
end
% don't need a bunch of redundant images
montage({inpict outpict})
Communication is the purpose of a forum, and comments (code comments and external commentary) are part of that process.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImage Data Workflows についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by