Implement average filter without using built-in functions
22 ビュー (過去 30 日間)
古いコメントを表示
I am trying to blur the image, but I did not succeed. I am keep getting almost black image? What am I missing here?
clc;
clear all;
img = imread("Q3_Input", "tif");
imshow(img);
[M, N] = size(img);
filter = averageFilter(img, M, N);
%blurredImage = conv2(single(img), filter, 'full');
figure;
imshow(filter, []);
%{
since the filter 3x3
i == 1 & j == 1 or
i == 1 & (N - j) == 0 or
(M - i) & j == 1 or
(M - i) & (N - j) == 0
covers the corner areas
x = covered areas
__________
|_x_|__|_x_|
|__|__|__|
|_x_|__|_x_|
the other coverts the middle of the area
|__|_x_|__|
|_x_|_x_|_x_|
|__|_x_|__|
%}
function img = averageFilter(image, M, N)
newImg = zeros(M, N);
for i = 1: M
for j = 1: N
if i == 1
if j == 1
summation = 0;
for k = i: i + 1
for l = j: j + 1
summation = summation + image(k,l);
end
end
newImg(i, j) = ceil(summation / 4.0);
elseif (N - j) == 0
for k = i: i + 1
for l = j - 1: j
summation = summation + image(k,l);
end
end
newImg(i, j) = ceil(summation / 4.0);
else
for k = i: i + 1
for l = j - 1: j + 1
summation = summation + image(k,l);
end
end
newImg(i, j) = ceil(summation / 6.0);
end
elseif (M - i) == 0
if j == 1
for k = i - 1: i
for l = j: j + 1
summation = summation + image(k,l);
end
end
newImg(i, j) = ceil(summation / 4.0);
elseif (N - j) == 0
for k = i -1: i
for l = j -1: j
summation = summation + image(k,l);
end
end
newImg(i, j) = ceil(summation / 4.0);
else
for k = i - 1: i
for l = j - 1: j + 1
summation = summation + image(k,l);
end
end
newImg(i, j) = ceil(summation / 6.0);
end
else
if j == 1
for k = i - 1: i + 1
for l = j: j + 1
summation = summation + image(k,l);
end
end
newImg(i, j) = ceil(summation / 6.0);
elseif (N - j) == 0
for k = i - 1: i + 1
for l = j - 1: j
summation = summation + image(k,l);
end
end
newImg(i, j) = ceil(summation / 6.0);
else
for k = i - 1: i + 1
for l = j - 1: j + 1
summation = summation + image(k,l);
end
end
newImg(i, j) = ceil(summation / 9.0);
end
end
end
end
img = newImg;
end


original image
0 件のコメント
採用された回答
Matt J
2019 年 10 月 21 日
編集済み: Matt J
2019 年 10 月 21 日
In all likelihood, you have not converted your image to floating point
img = im2double( imread("Q3_Input", "tif") );
2 件のコメント
Image Analyst
2019 年 10 月 21 日
No. Computing the mean in a window inherently gives you a floating point number. However, if you want, you can cast the final floating point image into uint8 after the whole window scanning process has finished.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Image Processing Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
