Given that the mask is hor = 0 0 0, 1 1 1, 0 0 0
I have to apply to my image. Afterwards, find the sum and divide by 3.
My code is:
clear; % Clear the workspace
A = imread('Nature.jpg');
I = imrotate(A,90,'bilinear','loose');
grey = 0.21*I(:,:,1)+0.72*I(:,:,2)+0.07*I(:,:,3);
Grey = double(grey);
HorMask = [0 0 0;1 1 1;0 0 0];
[r,c] = size(Grey);
OUT = zeros(r-3,c-3);
for i = 1:(r-3)
for j = 1:(c-3)
GreySquare = Grey(i:(i+2),j:(j+2));
res = HorMask.*GreySquare;
Divide = res./3;
OUT(i,j) = sum(Divide);
end
end
figure()
imshow(OUT);
However, I am not able to divide by 3. with error message:
Subscripted assignment dimension mismatch.
Error in HorizontalAverage (line 16)
OUT(i,j) = sum(Divide);

댓글 수: 1

Jan
Jan 2017년 10월 6일
"I am not able" does not explain, what the problem is. Do you get an error message? If so, please post it completely. It is much easier to solve a problem than to guess, what the problem is.

댓글을 달려면 로그인하십시오.

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2017년 10월 6일
편집: Andrei Bobrov 2017년 10월 6일

1 개 추천

A = imread('Nature.jpg');
I = imrotate(A,90,'bilinear','loose');
grey = 0.21*I(:,:,1)+0.72*I(:,:,2)+0.07*I(:,:,3);
Grey = double(grey);
OUT = conv2(Grey,[1,1,1]/3,'same'); % or OUT = filter2([1,1,1]/3,Grey);

댓글 수: 7

Edina Lee
Edina Lee 2017년 10월 6일
I do not want to use the in-build function. Is there a way?
Andrei Bobrov
Andrei Bobrov 2017년 10월 6일
편집: Andrei Bobrov 2017년 10월 6일
Hm! Homework?
[m,n] = size(Grey);
g = [zeros(m,1),Grey,zeros(m,1)];
ii = reshape(1:numel(g),[m, n + 2]);
OUT = mean(g(bsxfun(@plus,ii(:,1:end-2),reshape(m*(0:2),1,1,[]))),3);
Edina Lee
Edina Lee 2017년 10월 6일
편집: Edina Lee 2017년 10월 6일
There is a error, the dimension do not match. do I need to change to double for the mean and reshape?
Image Analyst
Image Analyst 2017년 10월 6일
If Andrei's new answer of not using built-in functions satisfies your definition of not using built-in functions, then please mark it as accepted to give him reputation points. Otherwise see this link.
Andrei Bobrov
Andrei Bobrov 2017년 10월 6일
편집: Andrei Bobrov 2017년 10월 6일
Thank you Image Analyst!
Hi Edina! I'm fixed my first comment.
there is still an error and it shows:
Error using sum
Trailing string input must be 'double','native', or 'default'.
Error in mean (line 82)
y = sum(x,dim,flag)/size(x,dim);
Error in Untitled2 (line 10)
OUT = mean(g(bsxfun(@plus,ii(:,1:end-2),reshape(m*(0:2),1,1,[]))),3,'omitnan');
Andrei Bobrov
Andrei Bobrov 2017년 10월 6일
Again fixed.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

질문:

2017년 10월 6일

댓글:

2017년 10월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by