Introduction to MATLAB programming - image blur problem
이전 댓글 표시
Hello all,
I am trying to work through one of the problems for the 'introduction to matlab programming' course by coursera, known as the 'image blur' problem (lesson 8 - final problems, week 9). The system keeps telling me that my values are incorrect, but I am struggling to understand why? I have attached my code below. I would be very grateful if anyone could give me a helping hand.
%The code to call the function:
img = imread('vandy.png')
output = blur(img,2) % I have specified the function 'blur' below
imshow(output);
% The function:
function[output] = blur(img,w)
here = 2*w + 1;
change = w;
%setting an epmpty matrix
emptymatrix = zeros(length(img(:,1)),length(img(1,:)));
%working out the output pixel value for each corner
added = img((1):(1+change),(1):(1+change));
addition = sum(added);
additionally = sum(addition);
mean = additionally/numel(added);
answer = uint8(mean);
emptymatrix(1,1) = answer;
i = length(img(:,1));
z = 1;
added = img((i-change):(i),(z):(z+change));
addition = sum(added);
additionally = sum(addition);
mean = additionally/numel(added);
answer = uint8(mean);
emptymatrix(i,z) = answer;
i = 1;
z = length(img(1,:));
added = img((i):(i+change),(z-change):(z));
addition = sum(added);
additionally = sum(addition);
mean = additionally/numel(added);
answer = uint8(mean);
emptymatrix(i,z) = answer;
i = length(img(:,1));
z = length(img(1,:));
added = img((i-change):(i),(z-change):(z));
addition = sum(added);
additionally = sum(addition);
mean = additionally/numel(added);
answer = uint8(mean);
emptymatrix(i,z) = answer;
output = uint8(emptymatrix);
% working out the pixel values for the inner square
for i = 1:length(img(:,1))
for z = 1:length(img(1,:))
if ((i + change) <= length(img(:,1))) && ((i-change) > 0) && ((z+change) <= length(img(1,:))) && ((z-change) > 0)
added = img((i-change):(i+change),(z-change):(z+change));
addition = sum(added);
additionally = sum(addition);
mean = additionally/(here*here);
answer = uint8(mean);
emptymatrix(i,z) = answer;
output = uint8(emptymatrix);
end
end
end
% working out the output pixel values for the outer rows and columns (excluding the corners)
for i = 2:(length(img(:,1))-1)
if (i-change) > 0 && (i+change) < length(img(:,1))
z = 1;
added = img((i-change):(i+change),(z):(z+change));
addition = sum(added);
additionally = sum(addition);
mean = additionally/numel(added);
answer = uint8(mean);
emptymatrix(i,z) = answer;
output = uint8(emptymatrix);
end
end
for i = 2:(length(img(:,1))-1)
if (i-change) > 0 && (i+change) < length(img(:,1))
z = length(img(1,:));
added = img((i-change):(i+change),(z-change):(z));
addition = sum(added);
additionally = sum(addition);
mean = additionally/numel(added);
answer = uint8(mean);
emptymatrix(i,z) = answer;
output = uint8(emptymatrix);
end
end
for i = 2:(length(img(1,:))-1)
if (i-change) > 0 && (i+change) < length(img(1,:))
z = 1;
added = img((z):(z+change),(i-change):(i+change));
addition = sum(added);
additionally = sum(addition);
mean = additionally/numel(added);
answer = uint8(mean);
emptymatrix(z,i) = answer;
output = uint8(emptymatrix);
end
end
for i = 2:(length(img(1,:))-1)
if (i-change) > 0 && (i+change) < length(img(1,:))
z = length(img(:,1));
added = img((z-change):(z),(i-change):(i+change));
addition = sum(added);
additionally = sum(addition);
mean = additionally/numel(added);
answer = uint8(mean);
emptymatrix(z,i) = answer;
output = uint8(emptymatrix);
end
end
end
댓글 수: 5
Geoff Hayes
2020년 4월 8일
ey21 - which values are considered to be incorrect? The output? The above is a lot of code to go through without knowing why it does what it does. I did try it with a jpeg and noticed that my input 260x396x3 image became a 260x1188 output image (where 1188=3*396). Are you assuming that your image inputs are 2D grayscale only? Is that true for the vandy.png that you are testing with? Or does the code need to be able to handle colour input images?
ey21
2020년 4월 8일
Geoff Hayes
2020년 4월 9일
Then there is probably something wrong with your algorithm. Can you add some comments to describe what the code is doing?
Image Analyst
2020년 4월 9일
Can you use imfilter() or conv2()? It would be a lot shorter.
채택된 답변
추가 답변 (1개)
Swapnil Yadav
2020년 6월 22일
2 개 추천
function out = blur(img,w) % convert to double for doing calculations imgD = double(img); [row, col] = size(img); out = zeros(row, col); for ii = 1:row for jj = 1:col % Get the indices for a submatrix r1 = ii-w; r2 = ii+w; c1 = jj-w; c2 = jj+w; % Test that indices are valid % If not, set to min/max that is valid if r1 < 1 r1 = 1; end if r2 > row r2 = row; end if c1 < 1 c1 = 1; end if c2 > col c2 = col; end % Get the submatrix and assign the mean to the output pixel m = imgD(r1:r2, c1:c2); out(ii,jj) = mean(m(:)); end end % convert back to uint8 out = uint8(out); end

댓글 수: 2
Aindrila Bose
2020년 8월 23일
Thanks a lot. Code was easy to understand.
Summer K.Rock
2022년 1월 24일
this answer is super easy and simple !! very good !!
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!