Title: Prewitt filter in Matlab
Tags: matlab;gradient
Question: I am trying to take a gradient of an image using the Prewitt filter. Can you tell me if this approach is correct?

I = imread('image.jpg')
Gx = [-1 0 1; -1 0 1; -1 0 1];
Gy = [1 1 1; 0 0 0; 1 1 1];
D = conv2(conv2(I, Gx), Gy)
imshow(D)

Is that correct? Is there a cleaner way to do it (no need to call conv2 twice)? Is conv2(I, Gx) the same as conv2(Gx, I)? (i.e. commutative?)

Thanks.



Here is another answer: Judging by my wikipedia-ing it looks like what you should do is:

```I = imread('image.jpg')
Gx = [-1 0 1; -1 0 1; -1 0 1];
Gy = [1 1 1; 0 0 0; -1 -1 -1];

A = sqrt( conv2(I,Gx).^2 + conv2(I,Gy).^2 );
imshow(A);
```

Link to Wikipedia Article


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:03/28/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<matlab><gradient>', 'Title': 'Prewitt filter in Matlab', 'ViewCount': 4208, 'id': 9900536, 'user_AboutMe': "<p>I'm a software engineer and computer scientist.</p>\n", 'user_AccountId': 250529.0, 'user_DisplayName': 'ktm5124', 'user_Reputation': 11428.0, 'user_Views': 974.0, 'user_id': 527312.0}}