MATLAB: How to apply custom filter to a grayscale image

customfilterfilterfilter2

I need to have grayscale image and develop a program that applies the filters given below to that grayscale image. I implemented following code but not sure if it is ok or not?
grayImage = imread("cameramanImage.tiff");
h = [ 2 -1 -1
-1 2 -1
-1 -1 2 ];
im_outFirst = filter2(h, grayImage);

Best Answer

I'd do this, using imfilter():
grayImage = imread("cameraman.tif");
h = [ 2 -1 -1
-1 2 -1
-1 -1 2 ];
im_outFirst = imfilter(grayImage, h);
I'm not sure what filter2 does or how it differs from imfilter().