MATLAB: I am trying to use the edge function on an image after I turned it to a grey scale. However I am having the following error

edge

Here is my code:
% Uploading Image
Img = imread('image1.jpeg')
figure
imshow(Img)
title('This is the original image')
grayImg = rgb2gray(Img)
figure
imshow(grayImg);
title('gray image of original')
% Turning Image into binary gradient mask
threshold = edge(grayImg, 'sobel');
factor = 0.5; %here 0.5 is a random number, i will try other numbers
BGMI = edge(Img,'sobel', threshold*factor);
figure
imshow(BGMI)
title('Binary Gradient Mask Version of Image')
error Message:
Error using edge
Expected input number 1, I, to be two-dimensional.
Error in edge>parse_inputs (line 461)
validateattributes(I,{'numeric','logical'},{'real','nonsparse','2d'},mfilename,'I',1);
Error in edge (line 189)
[a,method,thresh,sigma,thinning,H,kx,ky] = parse_inputs(varargin{:});
Error in ImageSegmentation (line 18)
BGMI = edge(Img,'sobel', threshold*factor);
Any advise/help is highly appreciated!

Best Answer

Your BGMI is trying to apply edge to your rgb image instead of your gray image