Gradient of Image using window size

1 回表示 (過去 30 日間)
Carnez
Carnez 2015 年 5 月 1 日
回答済み: Image Analyst 2015 年 5 月 1 日
I am new to MATLAB and trying to calculate the magnitude of the gradient using a specified window size but I receive the error: Attempted to access img(0,5); index must be a positive integer or logical. I am not sure how to solve this. Does anyone have any suggestions?
img = imread('image.png');
img = im2double(img);
[row, col] = size(img);
windowSize = 5;
for x = 1:row
for y = 1:col
if (x >= windowSize && x < row - windowSize && y >= windowSize && y < col - windowSize)
gradient(x,y) = abs(img(x+windowSize,y) - img(x-windowSize,y)) + abs(img(x,y+windowSize) -img(x,y-windowSize));
else
gradient(x,y) = 0;
end end end

回答 (1 件)

Image Analyst
Image Analyst 2015 年 5 月 1 日
Just use imgradient(img). Why go to all the trouble of doing it yourself when there's a built-in function that does it for you?

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by