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 Kommentar

Jan
Jan am 6 Okt. 2017
"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.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 6 Okt. 2017
Bearbeitet: Andrei Bobrov am 6 Okt. 2017

1 Stimme

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 Kommentare

Edina Lee
Edina Lee am 6 Okt. 2017
I do not want to use the in-build function. Is there a way?
Andrei Bobrov
Andrei Bobrov am 6 Okt. 2017
Bearbeitet: Andrei Bobrov am 6 Okt. 2017
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 am 6 Okt. 2017
Bearbeitet: Edina Lee am 6 Okt. 2017
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 am 6 Okt. 2017
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 am 6 Okt. 2017
Bearbeitet: Andrei Bobrov am 6 Okt. 2017
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 am 6 Okt. 2017
Again fixed.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by