# MATLAB help 2D convolution

4 views (last 30 days)
Yigit Goktas on 28 Mar 2023
Commented: Walter Roberson on 28 Mar 2023
I have a task to make a 2D convolution function that I have an image and a filter will be applied that should give the output result of image filtering in spatial domain. Also, during the edge detection(to handle boundaries) I have to use sobel and prewitt operator. I wrote a convolution code but I didn't quite get what is sobel and how to apply it. I got confused a bit and my code also gave error didn't get the reason and how to fix it?
The error I get:
Function padarray expected A (argument 1) to be numeric, logical or categorical for constant padding.
Error in Q4>my_convolution (line 15)
Error in Q4 (line 2)
outpu_image1 = my_convolution('Figure4.jpg',filter_1);
THE CODE:
filter_1 = [-1 0 1; -1 0 1; -1 0 1];
outpu_image1 = my_convolution('Figure4.jpg',filter_1);
function output_image = my_convolution(image, filter)
[im_height, im_width] = size(image);
[filter_height, filter_width] = size(filter);
output_image = zeros(im_height, im_width);
% Perform convolution
for i = 1:im_height
for j = 1:im_width
% Extract the local region of the padded image
% Compute the dot product between the local region and the filter
output_pixel = sum(sum(local_region .* filter));
% Assign the output pixel value to the output image
output_image(i, j) = output_pixel;
end
end
end

Walter Roberson on 28 Mar 2023
outpu_image1 = my_convolution('Figure4.jpg',filter_1);
You are passing in the character vector 'Figure4.jpg' not the content of the image.
function output_image = my_convolution(image, filter)
We recommend against naming a variable image as image is one of the important graphics functions; people are likely to get confused.
##### 2 CommentsShow 1 older commentHide 1 older comment
Walter Roberson on 28 Mar 2023
Pass in double(image1)

Image Analyst on 28 Mar 2023
Try this:
function output_image = my_convolution(image1, filter)
output_image = GMag;

### Categories

Find more on Image Filtering in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by